I have a df containing three columns:
-"sample" ~containing the sample names -"group" ~containing the group (first up, second up, remain up, remain down, second down, first down) -"value" ~containing the number in each group.
The samples I have ordered in my desired order in sorder
, and applied as factor to order the ticks on the x-axis.
In similar fashion I ordered the groups in gorder
and applied them as factor to order stacks in the graph.
I am happy with the order of the groups (and their colors) in the legend, and would like the stacks to be ordered the same. I have tried re-ordering the factors, but short of picking the colors by hand I have not managed to get the stacks in the same order as the legend. Do you have any suggestions?
require(dplyr)
require(ggplot2)
gorder<-c("first up", "second up", "remain up", "remain down", "second down", "first down")
sorder<-c("55_NST", "40_NST","25_NST","ad_NST", "RH_NST", "FT_ST", "55_ST", "25_ST")
set.seed(1)
df<-data.frame(
"sample" =rep(sorder, each=6),
"group"=rep(gorder, times=8),
"value"=c(abs(rnorm(48,mean=3000, sd=500))))
df<-df%>%mutate(value =case_when(group %in% c("remain down", "second down", "first down") ~ value *(-1),
!group %in% c("remain down", "second down", "first down") ~ value))
df$sample<-factor(df$sample, levels = sorder)
df$group<-factor(df$group, levels = gorder)
ggplot(df, aes(fill=group, y=value, x=sample)) +
geom_bar(position="stack", stat="identity") +
theme_bw()+
scale_x_discrete(breaks=sorder, labels=c("55", "40", "25", "AD", "RH", "FT (ST)", "55 (ST)", "25 (ST)"))+
scale_y_continuous(breaks = seq(from = -12000,to = 12000, by = 2000))+
labs(y="number of genes", x="RWC")+
scale_fill_brewer(type = "div", palette = "RdYlGn",direction = -1)
Example graph, I would like the stacks in the same order as the legend colors
scale_x_discrete(breaks = sorder...
where issorder
assigned? If it is a typo and should begorder
there still is a problem:Error:
breaks` andlabels
must have the same length`. Have a look at this link for making a reproducible question: speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5. – Peterset.seed("a_number")
to ensure consistent output? – Peter