I am quite new to ggplot2, so forgive me if this post is too stupid.
I used the following code to plot the data, but I am not able to get the style that I need for publication.
In the output, I need:
-
a legend. In my data case, there is nothing after
opts(legend.position="top")I have no idea why. And I also would like to split the legend into 3 columns likecolumns=3inauto.keyof lattice -
Colorize the bars using grey system (eg,
fill=c("white","grey20","grey70"))according to factorpl, but it seems that I cannot change the style withscale_colour_manual -
turn around the labels on the x-axis into horizontal.
-
maybe a y-axis? But,do you think it is necessary?
BTW, I have no idea how to prepare a figure for publication, so, any suggestion is very welcome!
library(ggplot2)
wt<-gl(3,4,108,labels=c("W30","W60","W90"))
pl<-gl(3,12,108,labels=c("P0","P1","P2"))
gp<-gl(3,36,108,labels=c("A","B","C"))
dat<-cbind(A=runif(108),B=runif(108,min=1,max=10),C=runif(108,min=100,max=200),D=runif(108,min=1000,max=1500))
dat.df<-data.frame(wt,pl,gp,dat)
dat.m<-melt(dat.df)
ggplot(dat.m,aes(x=wt,y=value,group=pl,facet=gp,fill=pl))+
stat_summary(fun.y=mean,geom="bar",size=2,position="dodge")+
stat_summary(fun.ymin=function(x)(mean(x)-sd(x)/sqrt(length(x))),geom="errorbar",
fun.ymax=function(x)(mean(x)+sd(x)/sqrt(length(x))),position="dodge")+
facet_grid(variable~facet,scale="free_y")+ opts(legend.position="top")+
scale_colour_manual(values = c("red", "blue", "green"))

Here are some pointers:
To get a horizontal legend, use
opts(legend.direction="horizontal")To change the fill of the bars, you have to specify
scale_fill_manual(values=c("white", "grey20", "grey70")). In your example, you have correctly mapped fill to pl. The only missing step is to map the manual scale to fill, rather than colour. Colour generally refers to the outline of the bar, and fill refers to the inside of the bar.To rotate the angle of axis text, use
opts(axis.text.x = theme_text(angle=45)). The default orientation is horizontal, so I use 45 degrees for illustration.I don’t know what you mean by “maybe a y-axis”. Perhaps you don’t want to display the y-axis, in which case you can suppress it by
opts(axis.title.y = theme_blank())Note that your example was not reproducible, so I had to invent some data. You can make it easier for us to respond if you ensure your example is reproducible:
yeartrtin your data.framegrpbut then refer to it asgpMy code: