I want to make a group bar plot with ggplot. but i want to reorder the bar according to the specie, for example first bars belong to sp1 , sp2 and then sp3 should be shown in a decsending order in the graph. So far I am able to plot the graph like this but genotype belong to sp1 should come first sp2 second and then genotype belong to sp3. I am using this code
ggplot(data=data, aes(x=reorder(Genotype, -var) , y=var, fill=Treatment)) +
geom_bar(stat="identity", position=position_dodge(width=0.85)) +
scale_fill_manual(values = c('#4850CE','#D37A3E'),
labels = c("Control Temperature (CT)","Heat tress (HS)")) +
geom_errorbar(aes(ymin = var, ymax = var+e,colour=Treatment),
width=0.75, position = position_dodge(0.85))+
scale_colour_manual(values=c('#4850CE','#D37A3E'))+
theme_bw() + labs(x=NULL, y=expression("a. weight"^~g))+
scale_y_continuous(breaks = seq(0, max(data$var+data$e), length.out = 6),
labels = number_format(accuracy = 0.01))+
geom_text(aes(label = Genotype, y = 0),angle=90, hjust=0, size=3)+
theme(panel.grid.minor = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(colour = "black", size=8, face="bold"),
axis.ticks.y = element_line(colour= "black"),
axis.text.y =element_text(colour="black", size = 8),
legend.position = c(0.8, 0.92), legend.background = element_blank(),
legend.key.size = unit(4, 'mm'),
legend.title = element_blank(),
legend.text = element_text(size = 8,face="bold"))+
guides(colour = FALSE)
Genotype | Specie | Treatment | var | e |
---|---|---|---|---|
BGB094 | sp1 | CT | 185.9406 | 44.7377 |
BGB095 | sp1 | CT | 248.2271 | 44.7377 |
BGB097 | sp2 | CT | 162.4696 | 44.7377 |
BGB001 | sp2 | CT | 284.5522 | 44.7377 |
BGB011 | sp3 | CT | 185.5353 | 44.7377 |
BGB048 | sp3 | CT | 219.0846 | 44.7377 |
BGB094 | sp1 | HS | 150.9137 | 44.7377 |
BGB095 | sp1 | HS | 211.05 | 44.7377 |
BGB097 | sp2 | HS | 123.8366 | 44.7377 |
BGB001 | sp2 | HS | 247.2338 | 44.7377 |
BGB011 | sp3 | HS | 149.6919 | 44.7377 |
BGB048 | sp3 | HS | 184.7978 | 44.7377 |
The basic code you need is:
library(tidyverse)
df %>%
arrange(Specie, desc(var)) %>%
mutate(Genotype = factor(Genotype, unique(Genotype))) %>%
ggplot(aes(Genotype, var, fill = Treatment)) +
geom_col(position = 'dodge')
Then add the other codes