Change order of groups in stacked bar chart AND legend in R?

大城市里の小女人 提交于 2019-12-12 03:57:36

问题


I know that this kind of questions have already been asked, but I have already tried to apply it to my dataset for hours, but without success.

Here is my dataframe

> df
        prot    X nr
1 unmodified same 68
2       1 Ac same 14
3       2 Ac same  7
4       3 Ac same  4
5       4 Ac same  3
6       5 Ac same  2
7       6 Ac same  1
8       7 Ac same  2

I tried two ways of adjusting the order of the groups, but in both cases the legend is exactly in the reverse order. How can I match the order of the groups in the plot with the one in the legend? way1:

y <- read.csv("df.csv",sep=",") #sep=";"
df <- data.frame(y)

df$prot <- factor(df$prot, levels = rev(df$prot))   #order groups

p<-ggplot(df, aes(x=X, y=nr, fill=prot)) +
geom_bar(stat="identity", position=position_stack(), width=0.2, colour="gray", aes(fill = prot))+ #  stacked, bar-width, outline-colour = colour
theme(panel.background = element_rect(fill = "ivory" , colour="ivory"))+
coord_flip()+
theme(legend.position="bottom") +
guides(fill = guide_legend(nrow = 1)) +
geom_text(aes(label=nr), color="black",vjust = -3, position = position_stack())   # add labels (%) non-overlapping

p

giving me this result:

way2:

y <- read.csv("df.csv",sep=",") #sep=";"
df <- data.frame(y)

df$prot <- factor(df$prot, levels = c("7 Ac", "6 Ac", "5 Ac", "4 Ac", "3 Ac", "2 Ac", "1 Ac","unmodified"))   #order groups

df <- ddply(df, .(X),
        transform, pos = cumsum(nr) - (0.5 * nr)) #adjust the position of the data labels:create new variable at the centre of each bar 

p<-ggplot(data=df, aes(x=X, y=nr, fill=prot)) +
geom_bar(stat="identity", position=position_stack(), width=0.2, colour="gray")+ #  stacked, bar-width, outline-colour = colour
theme(panel.background = element_rect(fill = "ivory" , colour="ivory"))+ #backgroundcolor
coord_flip()+
theme(legend.position="bottom") +
guides(fill = guide_legend(nrow = 1)) +
geom_text(data=df, aes(x = X, y = pos, label = paste0(nr,"%")),
        size=4)

p

p

giving me this result:


回答1:


You just have to use the reverse parameter in guide_legend and set it to TRUE, such as:

df<- data.frame(prot = c("unmodified", paste(1:7, "AC")), X = rep("same", 8),
                   nr = c(68,14,7,4,3,2,1,2))

library(ggplot2)

df$prot <- factor(df$prot, levels = rev(df$prot))   #order groups

p<-ggplot(df, aes(x=X, y=nr, fill=prot)) +
        geom_bar(stat="identity", position=position_stack(), width=0.2, colour="gray", aes(fill = prot))+ #  stacked, bar-width, outline-colour = colour
        theme(panel.background = element_rect(fill = "ivory" , colour="ivory"))+
        coord_flip()+
        theme(legend.position="bottom") +
        # to change ordering of legend, set reverse = TRUE
        guides(fill = guide_legend(nrow = 1, reverse = TRUE)) +
        geom_text(aes(label=nr), color="black",vjust = -3, position = position_stack())   # add labels (%) non-overlapping

p


来源:https://stackoverflow.com/questions/42370195/change-order-of-groups-in-stacked-bar-chart-and-legend-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!