自定义 ggplot 输出(颜色、外观等)
我对 ggplot2 还很陌生,所以如果这篇文章太愚蠢了,请原谅我。 我使用以下代码来绘制数据,但无法获得发布所需的样式。
在输出中,我需要:
一个图例。在我的数据案例中,
opts(legend.position="top")
之后没有任何内容,我不知道为什么。我还想将图例分成 3 列,例如网格的auto.key
中的columns=3
使用灰色系统对条形进行着色(例如
fill=c("white","grey20","grey70")
)根据因素pl
,但似乎我无法更改样式scale_colour_manual
将 x 轴上的标签转为水平方向来更改样式。
也许是 y 轴?但是,你觉得有必要吗?
顺便说一句,我不知道如何准备要发表的图,所以,非常欢迎任何建议!
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"))
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=3
inauto.key
of latticeColorize 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"))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是一些提示:
要获取水平图例,请使用
opts(legend.direction="horizontal")
要更改条形的填充,您必须指定
scale_fill_manual(values=c("white", "grey20", "grey70"))
。在您的示例中,您已正确地将 fill 映射到 pl。唯一缺少的步骤是将手动比例映射到填充,而不是颜色。颜色通常是指条形的轮廓,填充是指条形的内部。要旋转轴文本的角度,请使用
opts(axis.text.x = theme_text(angle=45))
。默认方向是水平的,因此我使用 45 度进行说明。我不知道你所说的“也许是y轴”是什么意思。也许您不想显示 y 轴,在这种情况下您可以通过
opts(axis.title.y = theme_blank())
抑制它
请注意,您的示例不可重现,所以我必须发明一些数据。如果您确保您的示例是可重现的,您可以让我们更轻松地做出回应:
year
的数据trt
的引用grp
获取数据,然后将其引用为gp
我的代码:
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:
year
trt
in your data.framegrp
but then refer to it asgp
My code: