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)) 
R语言ggplot绘制柱形图barplot,调整柱子图例顺序_第1张图片
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))

R语言ggplot绘制柱形图barplot,调整柱子图例顺序_第2张图片
image.png

你可能感兴趣的:(R语言ggplot绘制柱形图barplot,调整柱子图例顺序)