本文介绍了更改堆叠条形图中的组的顺序和R中的图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我知道这类问题已经被问到,但我已经试过将它应用到我的数据集上几个小时,但没有成功。 这里是我的数据框 > df prot X nr 1未修改相同68 2 1 Ac相同14 3 2 Ac相同7 4 3相同4 5 4 Ac相同3 6 5 Ac相同2 7 6相同1 8 7相同2 我尝试了两种方法来调整组的顺序,但在这两种情况下,图例完全是相反的顺序。我怎样才能将情节中的群体的顺序与传说中的群体的顺序相匹配? way1: y df< - data.frame(y) df $ prot< - factor(df $ prot,levels = rev(df $ prot))#order groups p geom_bar(stat =identity,position = position_stack(),width = 0.2,color =gray,aes(fill = prot))+#stacked,bar-width,outline-color = color theme(panel.background = element_rect(fill =ivory,color =ivory)) + coord_flip()+ 主题(legend.position =bottom)+ guides(fill = guide_legend(nrow = 1))+ geom_text(aes(label = nr),color =black,vjust = -3,position = position_stack())#添加标签(%)不重叠 给我这个结果: way2: y df df $ prot df transform,pos = cumsum(nr) - (0.5 * nr))#调整数据标签的位置:在每个栏的中心创建新变量 p geom_bar(stat =identity,position = position_stack(),width = 0.2,color =gray) +#堆叠,条形宽度,轮廓颜色=颜色主题(panel.background = element_rect(fill =ivory,color =ivory))+ #backgroundcolor coord_flip()+ 主题(legend.position =bottom)+ guides(fill = guide_legend(nrow = 1))+ geom_text(data = df,aes(x = X,y = pos, label = paste0(nr,%)), size = 4) p 给我这个结果: 解决方案 您只需在 guide_legend中使用 reverse 参数并将其设置为 TRUE ,例如: df nr = c ,14,7,4,3,2,1,2)) library(ggplot2) df $ prot p geom_bar(stat =身份,position = position_stack(),width = 0.2,color =gray,aes(fill = prot))+#stacked,bar-width,outline-color = color theme(panel.background = element_rect (fill =ivory,color =ivory))+ coord_flip()+ theme(legend.position =bottom)+ #改变图例排序,设置reverse = TRUE guides(fill = guide_legend(nrow = 1,reverse = TRUE))+ geom_text(aes(label = nr),color =blac k,vjust = -3,position = position_stack())#添加标签(%)不重叠 p 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 nr1 unmodified same 682 1 Ac same 143 2 Ac same 74 3 Ac same 45 4 Ac same 36 5 Ac same 27 6 Ac same 18 7 Ac same 2I 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 groupsp<-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 = colourtheme(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-overlappingpgiving 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 groupsdf <- 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 barp<-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 = colourtheme(panel.background = element_rect(fill = "ivory" , colour="ivory"))+ #backgroundcolorcoord_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)ppgiving me this result: 解决方案 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 groupsp<-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-overlappingp 这篇关于更改堆叠条形图中的组的顺序和R中的图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!