R语言ggplot绘制柱形图barplot,调整柱子图例顺序

End(Not run)例子

dat <- data.frame(Group = c("S1", "S1", "S2", "S2", "S3", "S3"),
                  Sub   = c("A", "B", "A", "B","A", "B"),
                  Value = c(5,3,8,7,8,7))  
dat #可通过调整数字顺序来实现左高右迪
ggplot(dat, aes(Group, Value)) +
  geom_bar(aes(fill = Sub), stat="identity", position="dodge", width=.5) +
  geom_signif(stat="identity",
              data=data.frame(x=c(0.875,1.875,2.875), xend=c(1.125,2.125,3.125),
                              y=c(5.8,8.5,8.5), annotation=c("*** ", " ***", "***")),
              aes(x=x,xend=xend, y=y, yend=y, annotation=annotation)) 
image.png

现在我想让B排前面

End(Not run)例子

dat <- data.frame(Group = c("S1", "S1", "S2", "S2", "S3", "S3"),
Sub = c("A", "B", "A", "B","A", "B"),
Value = c(5,3,8,7,8,7))
dat #可通过调整数字顺序来实现左高右迪
sub <- factor(dat$Sub,levels = c('B','A'))
ggplot(dat, aes(Group, Value)) +
geom_bar(aes(fill = sub), stat="identity", position="dodge", width=.5) +
geom_signif(stat="identity",
data=data.frame(x=c(0.875,1.875,2.875), xend=c(1.125,2.125,3.125),
y=c(5.8,8.5,8.5), annotation=c("*** ", " ", "")),
aes(x=x,xend=xend, y=y, yend=y, annotation=annotation))

image.png

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容