首页 > 解决方案 > 箱线图故障排除,添加另一个变量因素

问题描述

我在 r 中构建了一个漂亮的箱线图,用于查看不同孵化温度下甲烷产生的数据。该图着眼于收集样本的斑块产生的 CH4。

但是有一个温度变量。将样品分开,50% 在 10* 孵育,50% 在 26* 孵育

这是我目前的情节:

  Methanogenesis_Data=read.csv("CO2-CH4 Rates.csv")
  attach(Methanogenesis_Data)
  summary(Methanogenesis_Data)
  str(Methanogenesis_Data)

  boxplot(CH4rate~Patch, data = Methanogenesis_Data, xlab="Patch", 
  ylab="CH4 µmol g-1 hr-1 ",
  col=c("lightblue","firebrick1"), main = "CH4 Production After 
  Incubation", frame.plot=FALSE)

这是我之前的情节:

  boxplot(CH4rate~Patch+Temperature, data = Methanogenesis_Data, 
  xlab="Patch", ylab="CH4 µmol g-1 hr-1 ",
  col=c("lightblue","firebrick1"), main = "CH4 Production After 
  Incubation", frame.plot=FALSE)

这是数据:

  structure(list(Patch = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 
  2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Gravel", "Macrophytes", 
  "Marginal"), class = "factor"), Temperature = structure(c(2L, 
  2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
  1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Cold", 
  "Warm"), class = "factor"), CH4rate = c(0.001262595, 0.00138508, 
  0.001675944, 0.001592354, 0.002169233, 0.001772964, 0.002156633, 
  0.002864403, 0.002301383, 0.002561042, 0.005189598, 0.004557227, 
  0.008484851, 0.006867866, 0.007438633, 0.005405327, 0.006381582, 
  0.008860084, 0.007615417, 0.007705906, 0.009198508, 0.00705233, 
  0.007943024, 0.008319768, 0.010362114, 0.007822153, 0.010339339, 
  0.009252302, 0.008249555, 0.008197657), CO2rate = c(0.002274825, 
  0.002484866, 0.003020209, 0.00289133, 0.003927232, 0.003219346, 
  0.003922613, 0.005217026, 0.00418674, 0.00466427, 0.009427322, 
  0.008236453, 0.015339532, 0.012494729, 0.013531303, 0.009839847, 
  0.011624428, 0.016136746, 0.0138831, 0.014051034, 0.016753211, 
  0.012780956, 0.01445912, 0.01515584, 0.01883252, 0.014249452, 
  0.018849478, 0.016863299, 0.015045964, 0.014941168)), .Names = 
  c("Patch", 
  "Temperature", "CH4rate", "CO2rate"), class = "data.frame", row.names = 
  c(NA, 
  -30L))

我想要做的是拥有我当前的情节,但箱线图中的方框代表 3 个补丁区域内的温暖和寒冷温度。 Patch inc 生产 CH4 的箱线图。Temp <---这就是我想做的!

谢谢你的帮助!!

标签: rboxplot

解决方案


您可以使用 ggplot2 进行尝试:

library(tidyverse)

Methanogenesis_Data %>% 
  ggplot(aes(x = Patch, y = CH4rate,  fill = Temperature)) +
    geom_boxplot() + 
    scale_fill_manual(values = c("lightblue","firebrick1")) + 
    scale_x_discrete(drop = F) + 
    theme_minimal()+
    labs(y = 'CH4 µmol g-1 hr-1', title = "CH4 Production After Incubation")

在此处输入图像描述


或者,如果您愿意,可以使用 base-R 进行尝试:

boxplot(CH4rate~Temperature + Patch, data = Methanogenesis_Data, xlab="Patch", 
        ylab="CH4 µmol g-1 hr-1 ",
        col=c("lightblue","firebrick1"), main = "CH4 Production After 
  Incubation", frame.plot=FALSE,xaxt = 'n')
legend('topleft', legend = c('cold', 'warm'), fill = c("lightblue","firebrick1"))
axis(1,at = c(1.5,3.5,5.5), labels = levels(Methanogenesis_Data$Patch))

在此处输入图像描述


推荐阅读