ggplot2中多个条形的常用X轴标签

时间:2014-03-13 12:48:19

标签: r plot ggplot2 axis-labels


我正在生成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

plot
但我喜欢为多个条形图设置相同的x轴标签,就像前三个条形图的常用标签一样。 这样的事情:

plot2
在这里,一个共同的标签&#39;椰子&#39;对于前三个酒吧和第二个&#39; Rice&#39;接下来的三个酒吧。如果可能的话,可以对第一栏和下三栏进行区分。

我对使用facet_grid执行此操作不感兴趣 提前谢谢你 拉梅什

2 个答案:

答案 0 :(得分:0)

只需简单地重做id的命名。(见第一张图片)。或者您可以使用theme()更改轴大小,请参阅r-changing font size by Drew Steen

答案 1 :(得分:0)

此?

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