首页 文章

更改ggplot2中绘制的一组因子的顺序

提问于
浏览
2

我正在创建一个使用ggplot2的情节,这几乎是完美的,它看起来像这样:

enter image description here

但是,我想改变'浇水处理'框显示的顺序 - 我希望订单是红色,黄色,蓝色,紫色,绿色 .

为了创建我的绘图的数据框,我开始使用一个看起来像这样的数据框(注意:'Water'字母是上面提到的颜色的缩写):

Ring CO2 Water plot_NH4.2x25
1     1 550     B      2.228750
2     1 550     P      4.945625
3     1 550     R     22.724375
4     1 550     W     -0.644375
5     1 550     Y     -0.770000
6     2 475     B      2.228750
7     2 475     P      4.945625
8     2 475     R      1.348750

等等

我想我可以做以下其中一项:a)将浇水处理的名称更改为ABCD或1 2 3 4,以便它们按正确的顺序自动绘图b)添加一些代码,要求ggplot按顺序绘制它们我想要 .

但我无法弄清楚怎么做!我用'ifelse'搞砸了,但这似乎要求你做一个与数字列相关的陈述,而不是另一个因素 . 我也试过'xlim',但我不确定如何使这个因素被绘制为颜色,而不是x轴上的主要值 .

我用来创建图的代码是:

g.Amm.2 <-   ggplot(data=NH4_24March_plot, aes(x=CO2, y=plot_NH4.2x25, fill=Water)) +
  stat_boxplot(geom ='errorbar', width = 0.5, position=position_dodge(0.75))+
  geom_boxplot()+
  theme_bw()+
  theme(panel.border = element_blank(),                            #remove box boarder
    axis.line.x = element_line(color="black", size = 0.5),      #add x axis line
    axis.line.y = element_line(color="black", size = 0.5),      #add y axis line
    panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
    legend.key.size = unit(1.5, 'lines'),
    legend.position=c(0.9,0.8),
    legend.key = element_blank(),    #remove grey box from around legend
    panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
 scale_y_continuous(expand = c(0, 0), limits = c(-5,140), breaks=seq(0,140,20))+     #change x axis to intercept y axis at 0
  scale_fill_manual(values=c("skyblue2", "orchid1", "firebrick1", "seagreen3", "yellow2"),
                name=" Watering treatment",
                labels=c("optimal summer \noptimal autumn", "excess summer \nlimited autumn", 
                         "excess summer \noptimal autumn","limited summer \nexcess autumn",
                         "optimal summer \nlimited autumn"))+
  ylab(expression(Membrane~available~NH[4]^{" +"}~-N~(~mu~g~resin^{-1}~14~day^{-1})))+
  xlab(expression(CO[2]~concentration~(mu~mol~mol^{-1})))

任何想法将不胜感激,提前谢谢:-)

2 回答

  • 1

    要按顺序获取颜色,请执行以下操作:1 . 将水柱设置为具有您希望它们显示顺序的级别的因子 . 2.在scale_fill_manual中设置值时,按照您希望它们显示的顺序设置颜色 .

    见下面的结果 . 请注意,我删除了您的标签,以便更清楚地了解哪些颜色与哪些因素级别相对应 . 当然,您可以将它们添加回来 .

    myDf = data.frame(
      Ring = c(1,1,1,1,1,2,2,2),
      CO2 = c(550,550,550,550,550,475,475,475),
      Water=c("B","P","R","W","Y","B","P","R"),
      "plot_NH4.2x25" = c(2.228750,4.945625,22.724375,-0.644375,-0.770000,2.228750,4.945625,1.348750)
    )
    
    myDf$Water = factor(myDf$Water, levels = c("R","Y","B","P","W"))
    
    
    ggplot(data=myDf, aes(x=CO2, y=plot_NH4.2x25, fill=Water)) +
      stat_boxplot(geom ='errorbar', width = 0.5, position=position_dodge(0.75))+
      geom_boxplot()+
      theme_bw()+
      theme(panel.border = element_blank(),                            #remove box boarder
            axis.line.x = element_line(color="black", size = 0.5),      #add x axis line
            axis.line.y = element_line(color="black", size = 0.5),      #add y axis line
            panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
            panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
            legend.key.size = unit(1.5, 'lines'),
            legend.position=c(0.9,0.8),
            legend.key = element_blank(),    #remove grey box from around legend
            panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
      scale_y_continuous(expand = c(0, 0), limits = c(-5,140), breaks=seq(0,140,20))+     #change x axis to intercept y axis at 0
      scale_fill_manual(values=c("firebrick1", "yellow2","skyblue2", "orchid1",  "seagreen3"),
                        name=" Watering treatment")+
      ylab(expression(Membrane~available~NH[4]^{" +"}~-N~(~mu~g~resin^{-1}~14~day^{-1})))+
      xlab(expression(CO[2]~concentration~(mu~mol~mol^{-1})))
    

    请注意,由于提供的行数有限,下面的图表与您的图表(大概)不匹配 .
    enter image description here

  • 1

    按照我的评论,这里是如何使用facetting使 Water 值更容易遵循没有图例 . 我还包含 factor 代码来设置 Water 级别的顺序 . 另外,在绘图之前重新编码 Water 的级别可能更容易,因此我也包含代码来执行此操作 .

    # Fake data
    set.seed(491)
    NH4_24March_plot = data.frame(CO2=rep(c(550,475), each=30), Water=rep(c("A","B","C"), 20), 
                                  values=rnorm(60, 50, 10))
    
    # Set order of Water column
    NH4_24March_plot$Water = factor(NH4_24March_plot$Water, levels=c("B","A","C"))
    
    # Recode Water values (and note that the recoded values maintain the corresponding order 
    # of the Water levels that we set in the previous line of code)
    library(dplyr)
    NH4_24March_plot$Water_recode = recode(NH4_24March_plot$Water,
                                           "A"="optimal summer\noptimal autumn",
                                           "B"="excess summer\nlimited autumn",
                                           "C"="limited summer\nexcess autumn")
    
    ggplot(NH4_24March_plot, aes(Water_recode, values, fill=Water_recode)) +
      geom_boxplot(show.legend=FALSE) +
      facet_grid(. ~ CO2, labeller=label_bquote(cols=CO[2]:~.(CO2)~mu*mol%.%mol^{-1})) +
      scale_y_continuous(limits=c(0, max(NH4_24March_plot$values))) +
      theme_bw()
    

    enter image description here

相关问题