0

I am trying to make specific X axis order within each Y variable on geom_col graph. I have found solutions with factor and it's levels, but it doesn't seem to work for me.

Here is data frame 'df_full':

df_full<-data.frame(conc=c(NK, 1 µM X, 1 µM, 10 µM,100 µM,  
                           NK, 1 µM X, 1 µM, 10 µM,100 µM, 
                           NK, 1 µM X, 1 µM, 10 µM,100 µM), 
                    mean=c( 0.9008428,0.8278645,0.7890388,0.9541905, 0.8537885, 0.8212504,1.3828724,0.7165685, 0.7985398, 1.124524, 0.744428,0.835645,0.562878,0.476905, 0.632885), 
                    Treatment=c("A","A","A","A","A", "B", "B", "B","B","B","C","C","C","C", "C"),
                    upper =c(1.0990144, 0.9505348, 0.8273494, 1.0389074, 0.9227461, 
                             0.9657371, 1.6864420, 0.7401891, 0.9046951, 1.0990144, 
                             0.9505348, 0.8273494, 1.0389074,0.7401891, 0.9046951),
                    lower=c(0.7026713, 0.7051941, 0.7507282, 0.9528077, 0.7848309, 
                            0.6767638, 1.0793029, 0.6929479, 0.6923846, 0.7051941,
                            0.6767638, 1.0793029, 0.6929479, 0.7051941, 0.7507282),
                    p.value=c(0.0003, 0.6500, 1,0.02,0.0400, 
                              0.3301,0.100,0.023, 0.05, 0.05,
                              0.0002, 0.86, 0.46, 0.021, 0.001))

I made a conc variable as factor, and then it does make only two columns for drug (picture 1 attached). If I don't set conc as factor, I don't get the right order which i coded in conc_factor (picture 2 attached)

My current ggplot code:

legend_title <- "conc"
breaks_y =c(0, 0.25, 0.5, 0.75, 1, 1.25, 1.5)

df_full$Label <- NA
df_full$Label[df_full$p.value<0.001]<-'***'
df_full$Label[df_full$p.value<0.01 & is.na(df_full$Label)]<-'**'
df_full$Label[df_full$p.value<0.05 & is.na(df_full$Label)]<-'*'

conc_factor  = factor(df_full$conc, levels=c("NK", "10 μM X", "1 μM", "10 μM", "100 μM"))

plot <- 
ggplot(df_full, aes(x = factor(Treatment), y = mean, fill = conc_factor)) +
geom_col(color = "black",  position = position_dodge(0.8), width = 0.7) +
geom_errorbar(aes(ymax = upper, ymin = lower), width = 0.27, position = position_dodge(0.8), color = "black", size = 0.7) +
geom_text(aes(label = Label, group = conc_factor),size = 3.7, position = position_dodge(width =0.8), color = "black", vjust =-1.8) +
labs(x = "drug", y = "OD", title = "viability", color = "conc", fill = "conc") +
scale_y_continuous(limits = c(0, 1.5), breaks = breaks_y) +
  theme_bw() +
  theme(axis.text = element_text(size = 12, face = "bold"),
    axis.title.y = element_text(size = 12, face ="bold"),
    axis.title.x = element_text(size = 12, face ="bold")) 

plot + scale_fill_manual(values =  wes_palette("Darjeeling1", n = 5))

Picture 1 Picture 2

How can I set specific "NK", "10 μM X", "1 μM", "10 μM", "100 μM" X axis levels within each Y variable in geom_col?

Eddit

I solved this problem my changing μ to coded version \U00B5 with my current code

Simona
  • 87
  • 2
  • 8
  • cannot use df_full. please rather use dput to tranport data or fix the issue. And better use the reprex package to avoid such errors – mnist Sep 20 '21 at 20:46
  • Your levels argument is wrong, use `conc_factor <- factor(df_full$conc, levels=unique(df_full$conc))`. That's it. – Rui Barradas Sep 20 '21 at 21:12
  • @mnist Corrected now, see the edit. – Rui Barradas Sep 20 '21 at 21:13
  • thank you for your answer. But I see this doesn't specify the order of conc variables which I need? I need specificaly the columns to go as follows "NK", "10 μM X", "1 μM", "10 μM", "100 μM" – Simona Sep 21 '21 at 07:02

1 Answers1

0

Maybe set x as conc_factor and add facet_grid(~factor(Treatment))

enter image description here

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
  • Thanks, I am aware of facets, but I specifically need columns to be ordered as follows "NK", "10 μM X", "1 μM", "10 μM", "100 μM" on X scale as Treatment – Simona Sep 21 '21 at 15:33