簡體   English   中英

ggplot2中多個條的公共X軸標簽

[英]common X axis labels for more than one bar in ggplot2


我正在生成ggplot2堆積的條形圖。 一切都很好,這是我的代碼

> ab<-read.table("ab.txt", header=TRUE, sep="\t")
> head(ab)
  id P1 P2 P3
1  A  1  6  5
2  B  6  5  3
3  C  8  5  3
4  D  7  1  2
5  E  8  2  1
> library("reshape2")
> ab1<-melt(ab)
Using id as id variables
> head(ab1)
  id variable value
1  A       P1     1
2  B       P1     6
3  C       P1     8
4  D       P1     7
5  E       P1     8
6  A       P2     6
> library("ggplot2")
> ab1$id <- factor(ab1$id, levels=ab1$id)
> p<-ggplot(data=ab1, aes(x=id, y=value, fill=variable))+geom_bar(stat="identity", width=1)+scale_y_continuous(expand = c(0,0)) <br/>
> p

情節
但是我喜歡在多個條形上使用相同的x軸標簽,例如前三個條形的通用標簽。 像這樣:

情節2
在這里,前三個小節的通用標簽為“椰子”,后三個小節的通用標簽為“米”。 如果可能的話,可以將前三個小節區分開。

我對使用facet_grid執行此操作不感興趣。
提前致謝
拉梅什

只需簡單地重做id的命名即可(請參見第一張圖片)。 或者,您可以使用theme()更改軸的大小,請參閱Drew Steen的r更改字體大小

這個?

p<-ggplot(data=ab1, aes(x=id, y=value, fill=variable))+
  geom_bar(stat="identity", width=1)+
  scale_x_discrete(labels=c("","Coconut","","Rice",""))+
  theme(axis.text.x=element_text(angle=45,size=14,face="bold",hjust=1, vjust=1))
p

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM