自定义 ggplot 输出(颜色、外观等)

发布于 2024-11-03 08:21:49 字数 1413 浏览 2 评论 0原文

我对 ggplot2 还很陌生,所以如果这篇文章太愚蠢了,请原谅我。 我使用以下代码来绘制数据,但无法获得发布所需的样式。

在输出中,我需要:

  1. 一个图例。在我的数据案例中, opts(legend.position="top") 之后没有任何内容,我不知道为什么。我还想将图例分成 3 列,例如网格的 auto.key 中的 columns=3

  2. 使用灰色系统对条形进行着色(例如 fill=c("white","grey20","grey70"))根据因素pl,但似乎我无法更改样式scale_colour_manual

  3. 将 x 轴上的标签转为水平方向来更改样式。

  4. 也许是 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:

  1. 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 like columns=3 in auto.key of lattice

  2. Colorize the bars using grey system (eg, fill=c("white","grey20","grey70"))according to factor pl, but it seems that I cannot change the style with scale_colour_manual

  3. turn around the labels on the x-axis into horizontal.

  4. 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")) 

My output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不打扰别人 2024-11-10 08:21:49

以下是一些提示:

  1. 要获取水平图例,请使用 opts(legend.direction="horizo​​ntal")

  2. 要更改条形的填充,您必须指定 scale_fill_manual(values=c("white", "grey20", "grey70"))。在您的示例中,您已正确地将 fill 映射到 pl。唯一缺少的步骤是将手动比例映射到填充,而不是颜色。颜色通常是指条形的轮廓,填充是指条形的内部。

  3. 要旋转轴文本的角度,请使用 opts(axis.text.x = theme_text(angle=45))。默认方向是水平的,因此我使用 45 度进行说明。

  4. 我不知道你所说的“也许是y轴”是什么意思。也许您不想显示 y 轴,在这种情况下您可以通过 opts(axis.title.y = theme_blank())

    抑制它

请注意,您的示例不可重现,所以我必须发明一些数据。如果您确保您的示例是可重现的,您可以让我们更轻松地做出回应:

  • 没有 year 的数据
  • 在您的 data.frame 中存在对 trt 的引用
  • 您设置为 grp 获取数据,然后将其引用为 gp

我的代码:

dat.df <- data.frame(
    gp = gl(3, 36, 108, labels=c("A", "B", "C")),
    yr = sample(2000:2010, 108, replace=TRUE),
    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.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")+#, position="dodge"
    facet_grid(variable~facet, scale="free_y")+
    scale_fill_manual(values=c("white", "grey20", "grey70")) +
    opts(
        legend.position="top", 
        legend.direction="horizontal",
        axis.text.x = theme_text(angle=45),
        axis.title.y = theme_blank()
    ) 

在此输入图像描述

Here are some pointers:

  1. To get a horizontal legend, use opts(legend.direction="horizontal")

  2. 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.

  3. 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.

  4. 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:

  • There is no data for year
  • There is a reference to trt in your data.frame
  • You set up data for grp but then refer to it as gp

My code:

dat.df <- data.frame(
    gp = gl(3, 36, 108, labels=c("A", "B", "C")),
    yr = sample(2000:2010, 108, replace=TRUE),
    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.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")+#, position="dodge"
    facet_grid(variable~facet, scale="free_y")+
    scale_fill_manual(values=c("white", "grey20", "grey70")) +
    opts(
        legend.position="top", 
        legend.direction="horizontal",
        axis.text.x = theme_text(angle=45),
        axis.title.y = theme_blank()
    ) 

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文