ggplot2中绘制的一组因子的更改顺序 [英] Change order of one set of factors plotted in ggplot2

查看:151
本文介绍了ggplot2中绘制的一组因子的更改顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a plot using ggplot2 which is almost perfect, it looks like this:

但是,我想更改显示浇水处理"框的顺序-我希望顺序为红色,黄色,蓝色,紫色,绿色.

However, I want to change the order that the 'watering treatment' boxes are shown - I want the order to be red, yellow, blue, purple, green.

要为我的绘图创建数据框,我从一个看起来像这样的数据框开始(注意:"Water"字母是上述颜色的缩写):

To create the dataframe for my plot, I started with a dataframe that looked like this (note: 'Water' letters are abbreviations for the colours mentioned above):

  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)将浇水处理的名称更改为A B C D或1 2 3 4,以便它们以正确的顺序自动绘制 b)添加一些代码,要求ggplot按我想要的顺序绘制它们.

I thought I could do one of the following: a) change the name of the watering treatment to A B C D or 1 2 3 4 so they automatically plot in the correct order b) add a bit of code that asks ggplot to plot them in the order I want.

但是我也不知道该怎么做!我搞砸了"ifelse",但这似乎要求您做出与数字列相关的声明,而不是其他因素.我还尝试过"xlim",但不确定如何针对绘制为颜色的因素而不是x轴上的主要值来进行这项工作.

But I can't figure out how to do either! I've messed around with 'ifelse', but this seems to require you to make a statement that is relevant to a numeric column, not another factor. I also tried 'xlim' but am unsure how to make this work for a factor that is being plotted as the colour, rather than the primary value on the x axis.

我用来创建情节的代码是:

The code I've used to create the plot is:

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})))

任何想法都将不胜感激,在此先感谢:-)

Any thoughts would be greatly appreciated, thanks in advance :-)

推荐答案

在我的评论之后,这是如何使用构面使Water值更容易使用而没有图例的方法.我还包括factor代码以设置Water级别的顺序.此外,在绘制之前仅重新编码Water的电平可能会更容易,因此我也提供了执行此操作的代码.

Following up on my comment, here's how you could use facetting to make the Water values easier to follow without a legend. I also include the factor code to set the order of the Water levels. In addition, it might be easier to just recode the levels of Water before plotting, so I include code to do that as well.

# 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() 

这篇关于ggplot2中绘制的一组因子的更改顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆