0


I am generating ggplot2 stacked bar plots. Everything is fine, here is my code

> 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
But I like to have same x-axis labels for more than one bar, like common label for first three bars. Something like this:

plot2
Here, a common label 'Coconut' for first three bars and second 'Rice' for next three bars. If possible for first and next three bars to distinguish.

I am not interested in doing this using facet_grid.
Thanks in advance
Ramesh

ramesh
  • 1,187
  • 7
  • 19
  • 42
  • This post http://stackoverflow.com/questions/18165863/ggplot2-labels-of-grouping-values-below-the-plot should help – user20650 Mar 13 '14 at 13:53

2 Answers2

0

Just simply redo the naming of the ids.(see your first image). Or you can change the axes size with theme() see r-changing font size by Drew Steen

Community
  • 1
  • 1
Sander Van der Zeeuw
  • 1,092
  • 1
  • 13
  • 35
0

This?

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
jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Thank you, as some user suggested here, this is the one i wanted: http://stackoverflow.com/questions/18165863/ggplot2-labels-of-grouping-values-below-the-plot – ramesh Mar 13 '14 at 16:00