R語言ggplot繪制柱形圖barplot,調(diào)整柱子圖例順序

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 #可通過調(diào)整數(shù)字順序來實現(xiàn)左高右迪
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

現(xiàn)在我想讓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 #可通過調(diào)整數(shù)字順序來實現(xiàn)左高右迪
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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容