如何在R的线上放置箱形图来绘制时间序列图?

时间:2019-04-29 17:10:12

标签: r time-series boxplot

我有一个称为df的数据框。我每个sample.id都有10个重复项,使用这些重复项我想获得好坏Location的统计信息(显示箱形图)。 X轴应具有depth值,Y轴应具有observed_otus值。我想得到一个类似https://i.stack.imgur.com/MOYvX.png

的图
df <- structure(list(sample.id = c("s1", "s10", "s11", "s13", "s14", 
"s16", "s1", "s10", "s11", "s13", "s14", "s16", "s1", "s10", 
"s11", "s13", "s14", "s16", "s1", "s10", "s11", "s13", "s14", 
"s16", "s1", "s10", "s11", "s13", "s14", "s16", "s1", "s10", 
"s11", "s13", "s14", "s16", "s1", "s10", "s11", "s13", "s14", 
"s16", "s1", "s10", "s11", "s13", "s14", "s16", "s1", "s10", 
"s11", "s13", "s14", "s16", "s1", "s10", "s11", "s13", "s14", 
"s16", "s1", "s10", "s11", "s13", "s14", "s16", "s1", "s10", 
"s11", "s13", "s14", "s16", "s1", "s10", "s11", "s13", "s14", 
"s16", "s1", "s10", "s11", "s13", "s14", "s16", "s1", "s10", 
"s11", "s13", "s14", "s16", "s1", "s10", "s11", "s13", "s14", 
"s16", "s1", "s10", "s11", "s13", "s14", "s16", "s1", "s10", 
"s11", "s13", "s14", "s16", "s1", "s10", "s11", "s13", "s14", 
"s16", "s1", "s10", "s11", "s13", "s14", "s16", "s1", "s10", 
"s11", "s13", "s14", "s16", "s1", "s10", "s11", "s13", "s14", 
"s16", "s1", "s10", "s11", "s13", "s14", "s16", "s1", "s10", 
"s11", "s13", "s14", "s16", "s1", "s10", "s11", "s13", "s14", 
"s16", "s1", "s10", "s11", "s13", "s14", "s16", "s1", "s10", 
"s11", "s13", "s14", "s16", "s1", "s10", "s11", "s13", "s14", 
"s16", "s1", "s10", "s11", "s13", "s14", "s16", "s1", "s10", 
"s11", "s13", "s14", "s16"), Location = c("GOOD", "GOOD", "SALINE", 
"SALINE", "SALINE", "SALINE", "GOOD", "GOOD", "SALINE", "SALINE", 
"SALINE", "SALINE", "GOOD", "GOOD", "SALINE", "SALINE", "SALINE", 
"SALINE", "GOOD", "GOOD", "SALINE", "SALINE", "SALINE", "SALINE", 
"GOOD", "GOOD", "SALINE", "SALINE", "SALINE", "SALINE", "GOOD", 
"GOOD", "SALINE", "SALINE", "SALINE", "SALINE", "GOOD", "GOOD", 
"SALINE", "SALINE", "SALINE", "SALINE", "GOOD", "GOOD", "SALINE", 
"SALINE", "SALINE", "SALINE", "GOOD", "GOOD", "SALINE", "SALINE", 
"SALINE", "SALINE", "GOOD", "GOOD", "SALINE", "SALINE", "SALINE", 
"SALINE", "GOOD", "GOOD", "SALINE", "SALINE", "SALINE", "SALINE", 
"GOOD", "GOOD", "SALINE", "SALINE", "SALINE", "SALINE", "GOOD", 
"GOOD", "SALINE", "SALINE", "SALINE", "SALINE", "GOOD", "GOOD", 
"SALINE", "SALINE", "SALINE", "SALINE", "GOOD", "GOOD", "SALINE", 
"SALINE", "SALINE", "SALINE", "GOOD", "GOOD", "SALINE", "SALINE", 
"SALINE", "SALINE", "GOOD", "GOOD", "SALINE", "SALINE", "SALINE", 
"SALINE", "GOOD", "GOOD", "SALINE", "SALINE", "SALINE", "SALINE", 
"GOOD", "GOOD", "SALINE", "SALINE", "SALINE", "SALINE", "GOOD", 
"GOOD", "SALINE", "SALINE", "SALINE", "SALINE", "GOOD", "GOOD", 
"SALINE", "SALINE", "SALINE", "SALINE", "GOOD", "GOOD", "SALINE", 
"SALINE", "SALINE", "SALINE", "GOOD", "GOOD", "SALINE", "SALINE", 
"SALINE", "SALINE", "GOOD", "GOOD", "SALINE", "SALINE", "SALINE", 
"SALINE", "GOOD", "GOOD", "SALINE", "SALINE", "SALINE", "SALINE", 
"GOOD", "GOOD", "SALINE", "SALINE", "SALINE", "SALINE", "GOOD", 
"GOOD", "SALINE", "SALINE", "SALINE", "SALINE", "GOOD", "GOOD", 
"SALINE", "SALINE", "SALINE", "SALINE", "GOOD", "GOOD", "SALINE", 
"SALINE", "SALINE", "SALINE", "GOOD", "GOOD", "SALINE", "SALINE", 
"SALINE", "SALINE"), depth = c("1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "98", "98", "98", "98", "98", "98", "98", "98", "98", 
"98", "98", "98", "98", "98", "98", "98", "98", "98", "98", "98", 
"98", "98", "98", "98", "98", "98", "98", "98", "98", "98", "98", 
"98", "98", "98", "98", "98", "98", "98", "98", "98", "98", "98", 
"98", "98", "98", "98", "98", "98", "98", "98", "98", "98", "98", 
"98", "98", "98", "98", "98", "98", "98", "196", "196", "196", 
"196", "196", "196", "196", "196", "196", "196", "196", "196", 
"196", "196", "196", "196", "196", "196", "196", "196", "196", 
"196", "196", "196", "196", "196", "196", "196", "196", "196", 
"196", "196", "196", "196", "196", "196", "196", "196", "196", 
"196", "196", "196", "196", "196", "196", "196", "196", "196", 
"196", "196", "196", "196", "196", "196", "196", "196", "196", 
"196", "196", "196"), rep = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 
7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 
10L, 10L, 10L, 10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 
7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 
10L, 10L, 10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 
7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 
10L, 10L, 10L, 10L), .Label = c("iter.1", "iter.2", "iter.3", 
"iter.4", "iter.5", "iter.6", "iter.7", "iter.8", "iter.9", "iter.10"
), class = "factor"), observed_otus = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 68L, 58L, 54L, 73L, 61L, 70L, 66L, 52L, 
61L, 71L, 62L, 76L, 74L, 59L, 53L, 73L, 73L, 76L, 70L, 57L, 51L, 
71L, 59L, 70L, 73L, 57L, 57L, 75L, 70L, 63L, 74L, 61L, 56L, 74L, 
66L, 66L, 74L, 56L, 55L, 72L, 60L, 73L, 76L, 55L, 63L, 68L, 64L, 
71L, 79L, 59L, 56L, 72L, 58L, 61L, 73L, 56L, 56L, 70L, 65L, 69L, 
122L, 79L, 82L, 118L, 95L, 117L, 117L, 82L, 82L, 118L, 97L, 100L, 
115L, 86L, 77L, 109L, 96L, 115L, 120L, 76L, 84L, 117L, 102L, 
116L, 110L, 87L, 81L, 117L, 91L, 115L, 121L, 79L, 79L, 127L, 
96L, 114L, 117L, 78L, 86L, 109L, 96L, 114L, 113L, 85L, 70L, 111L, 
100L, 107L, 117L, 86L, 79L, 118L, 104L, 117L, 111L, 75L, 83L, 
110L, 95L, 110L)), row.names = c(NA, -180L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

一个ggplot解决方案,用于解决您的问题:

library(tidyverse)

df <- df %>% mutate_at("depth",factor,unique(sort(as.numeric(.$depth)))) #converting the depth values to factor and order

ggplot(df, aes(x=depth, y=observed_otus,color=Location)) + #initialize ggplot
geom_boxplot(position="identity") +  #initialize boxplot
  stat_summary(fun.y=mean, geom="line", aes(group=Location))  + #add mean line
  stat_summary(fun.y=mean, geom="point") # add mean points

the new style

答案 1 :(得分:0)

这里是一个解决方案,其中将方框绘制到深度的比例。我还添加了一些偏移,以防止盒子完全重叠。

#convert depth to numeric 
 df$depth<- as.numeric(df$depth)

library(ggplot2)
g<-ggplot(df, aes(x=depth, y=observed_otus, color=Location)) +
  geom_boxplot(data=df[df$Location =="GOOD",], aes(x=depth-1, y=observed_otus, group=depth), width=1.5) +
  geom_boxplot(data=df[df$Location =="SALINE",], aes(x=depth+1, y=observed_otus, group=depth),  color="blue", width=1.5) + 
  stat_summary(fun.y=mean, geom="line", aes(group=Location, color=Location))  + 
  stat_summary(fun.y=mean, geom="point", aes(group=Location)) +
  scale_color_manual(values = c("black", "blue"))
print(g)

enter image description here