箱线图故障排除,添加了另一个可变因素

时间:2019-06-13 12:53:18

标签: r boxplot

我在r中构建了一个漂亮的箱形图,用于查看在不同孵育温度下甲烷产生的数据。该图考察了从中收集样品的贴剂产生的CH4。

但是有温度变量。将样品在50%的10 *和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个补丁区域中的冷热温度。 Boxplot of CH4 production by Patch inc. Temp <---这就是我想要做的!

谢谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您可以使用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")

enter image description here


或者,如果您愿意,请尝试使用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))

enter image description here