使用ggplot()创建盒子和晶须图疑难解答

时间:2019-06-13 10:17:02

标签: r ggplot2

已更新:

数据现已更新为完整的化学值,而不是平均值。

我正在尝试在非常小的数据集上在r中创建箱形和晶须图。我的数据无法正常运行,或者我缺少一些明显的错误。

  This is the code i have for making said plot
  library(ggplot2)
  Methanogenesis_Data=read.csv("CO2-CH4 Rates.csv")
  attach(Methanogenesis_Data)
  summary(Methanogenesis_Data)
  str(Methanogenesis_Data)

  boxplot(CH4rate~Patch+Temperature, data = Methanogenesis_Data, 
  xlab="Patch", ylab="CH4 Production")
  cols<-c("red", "blue")

从这个小的数据集中。

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

我作为输出得到的图是好的,但是我希望X轴上的变量仅显示“ Gravel”,“ Macrophytes”,“ Marginal”,而不是每个带有Warm和Cold的变量。感谢您的协助

这就是我要解决的问题-----> Exact Boxplot I want to create

1 个答案:

答案 0 :(得分:1)

在更新示例图之后:

我还包括了图例位置的格式。如果要编辑y轴标签以包含下标,建议您阅读this。我为重新标记添加了一个空白标题。

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

现在,我将为每个图形创建两个数据集,只是为了简化起见,您可以将它们组合在一起,但是为了便于格式化,这可能会更容易。

CH4rate <- test %>%
  gather("id", "value", 3:4) %>%
  filter(id == "CH4rate")

CO2rate <- test %>%
  gather("id", "value", 3:4) %>%
  filter(id == "CO2rate")

第一个情节:

ggplot(CH4rate) +
  geom_boxplot(mapping = aes(x = Patch, y = value, fill=factor(Temperature, levels = c("Warm", "Cold")))) +
  theme(legend.position = c(0.15, 0.9), panel.background = element_rect(fill = "white", colour = "grey50")) +
  labs(title = "Title of graph", x="Patch Type", y = "CH4rate") +
  scale_fill_manual(name = "", values = c("orange", "light blue")
                    , labels = c("Cold" = "Incubated at 10˙C", "Warm" = "Incubated at 26˙C"))

First Plot: CH4rate

第二个情节:

ggplot(CO2rate) +
  geom_boxplot(mapping = aes(x = Patch, y = value, fill=factor(Temperature, levels = c("Warm", "Cold")))) +
  theme(legend.position = c(0.15, 0.9), panel.background = element_rect(fill = "white", colour = "grey50")) +
  labs(title = "Title of graph", x="Patch Type", y = "CO2rate") +
  scale_fill_manual(name = "", values = c("orange", "light blue")
                    , labels = c("Cold" = "Incubated at 10˙C", "Warm" = "Incubated at 26˙C"))

Second Plot: CO2rate