2
Hello. I’m trying to use ggplot2 to make a graph on R. This graph is a bar graph that deals with various concentrations (each concentration has a bar) on the X axis, which is associated with a dependent variable on the Y axis which is parasitic growth inhibition (%).
However, I found a problem: It puts the concentrations out of order (The right one would be 13.07, then 6.53, then 3.26 etc).
The code I used
library(ggplot2)
comp_1_concentrations = c('13.07','6.53','3.26','1.63','0.81','0.4','0.2')
comp_1_inhibition = c(78,50,28,15,10,2,1.5)
comp_1_desv_pad = c(10,1,10,1.5,10,1,0.2)
data <- data.frame(
name=comp_1_conc,
value=comp_1_inhib,
sd=comp_1_desv
)
ggplot(data) +
geom_bar( aes(x=name, y=value), stat="identity", fill="black", alpha=1.7,color='black') +
geom_errorbar( aes(x=name, ymin=value, ymax=value+sd), width=0.5, colour="black", alpha=0.9, size=0.8)+theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+labs(x = 'Concentrations (µM)')+labs(y = 'Growth inhibition (%)')+labs(title = 'Compound 1')
Thank you. Is there still a way to let the order decrease? I tried to use the decreasing argument = TRUE but there is no such function.
– Guilherme Matos Passarini
You can i) invert the vector with the function
rev()
or ii) reorder by-vetor
(reorder(x, -y)
) - a little hack– Tomás Barcellos
Where do I put this command? Where do I put it specifically? If I put it in the definition of vectors (first lines) it doesn’t change
– Guilherme Matos Passarini
In the first geometry.
p + geom_bar(aes(x = reorder(name, -as.numeric(name)), y = value), ...)
– Tomás Barcellos