r - 循环遍历多列并在 R 中绘制一个图(每个图都有误差线)
问题描述
我在这篇文章的答案中发现了很大的实用性(循环遍历多个列并在 R 中为每个列绘制一个图?),但想知道如何在我的图中添加标准误差线。这是我的尝试,但我不确定如何使其正常工作。
data <- structure(list(year = c(2019L, 2019L, 2019L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L, 2019L, 2020L, 2020L, 2020L, 2020L, 2020L,
2020L, 2020L, 2020L, 2020L, 2020L), season = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L), .Label = c("dry", "wet"), class = "factor"), site = c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L), temp = c(26.9, 27.8, 27.3, 26.4, 26.8, 29.4, 29.9,
29.9, 29.9, 30, 24, 23.5, 23.9, 23.7, 24.1, 30.2, 30.8, 30.8,
30.3, 30.1), do = c(7.5, 7.2, 7.8, 8.1, 7.4, 3.7, 3.2, 6.4, 4.9,
5, 6.5, 5.2, 5.9, 6.7, 6.3, 3.63, 1.81, 1.85, 4.25, 0.69), salinity = c(32.29,
30.1, 31.35, 31.93, 30.77, 27.35, 27.34, 28.42, 28.37, 28.24,
32.69, 29.72, 29.15, 28.9, 25.29, 24.37, 23.47, 25.1, 24.79,
23.62), pH = c(8.24, 8.28, 8.37, 8.32, 8.39, 7.85, 7.84, 8.13,
8.04, 8.06, 8.26, 8.17, 8.18, 8.24, 8.13, 7.8, 7.61, 7.61, 7.95,
7.41), water_depth = c(95L, 95L, 62L, 63L, 55L, 100L, 107L, 110L,
140L, 95L, 85L, 80L, 60L, 53L, 55L, 125L, 135L, 145L, 125L, 100L
), sed_depth = c(56L, 40L, 22L, 1L, 20L, 60L, 47L, 68L, 40L,
20L, 55L, 35L, 20L, 1L, 25L, 45L, 30L, 35L, 1L, 20L), SAV = c(25.5,
41.5, 50, 47.5, 60.1, 53.5, 46.5, 80.5, 20, 32.5, 26.1, 39.5,
29.1, 48.5, 39.6, 63, 50.5, 70, 70, 56)), row.names = c(1L, 155L,
309L, 463L, 617L, 771L, 925L, 1079L, 1233L, 1387L, 1541L, 1695L,
1849L, 2003L, 2157L, 2311L, 2465L, 2619L, 2773L, 2927L), class = "data.frame")
这是我的代码。请注意,我仍然希望 ggtitle 和文件命名系统能够正常工作:
col_names <- colnames(data[,-c(1:3)])
for (i in col_names) { # for-loop over columns
cdata2 <- plyr::ddply(i, c("year", "season"), summarise,
N = length(i),
n_mean = mean(i),
n_median = median(i),
sd = sd(num),
se = sd / sqrt(N))
ggplot(cdata2, aes(x = year, y = n_mean, color = season)) +
geom_errorbar(aes(ymin=n_mean-se, ymax=n_mean+se),
width=.2,
color = "black") +
geom_point(color = "black", # Make both seasons have black borders
shape = 21,
size = 3,
aes(fill = season)) +
scale_fill_manual(values=c("white", "#C0C0C0")) +
scale_x_continuous(breaks=c(2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2018,2019,2020)) +
labs(x= NULL, y = "Mean count") +
ggtitle(i)
setwd('D:/.../Trend plots')
ggsave(paste0(i, "- ENV_trend_plot.png"),
height = 5, width=7, units = "in")
}
解决方案
我的 plyr 词汇量非常有限,所以我用 dplyr 语法代替了它。更换 plyr 块后,从那里开始似乎一帆风顺。我已经包含了几行来显示情节并注释掉了将情节写入磁盘;你当然可以扭转这些变化。
library(ggplot2)
library(dplyr)
col_names <- colnames(data[,-c(1:3)])
# A list in which to store plots for reproducibility
plist <- list()
for (i in col_names) { # for-loop over columns
cdata2 <- data %>%
group_by(year, season) %>%
summarise(N = length(.data[[i]]),
n_mean = mean(.data[[i]]),
n_median = median(.data[[i]]),
sd = sd(.data[[i]]),
se = sd / sqrt(N))
ggplot(cdata2, aes(x = year, y = n_mean, color = season)) +
geom_errorbar(aes(ymin=n_mean-se, ymax=n_mean+se),
width=.2,
color = "black") +
geom_point(color = "black", # Make both seasons have black borders
shape = 21,
size = 3,
aes(fill = season)) +
scale_fill_manual(values=c("white", "#C0C0C0")) +
scale_x_continuous(breaks=c(2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2018,2019,2020)) +
labs(x= NULL, y = "Mean count") +
ggtitle(i)
# Comment out the line below
plist[[i]] <- last_plot()
# Uncomment these lines
# setwd('D:/.../Trend plots')
# ggsave(paste0(i, "- ENV_trend_plot.png"),
# height = 5, width=7, units = "in")
}
# Showing plots
patchwork::wrap_plots(plist)
由reprex 包于 2021-09-09 创建(v2.0.1)
推荐阅读
- apache-camel - 在 Camel SFTP 中设置文件名和目录的动态配置
- protocol-buffers - 将 proto 编译成单个文件,而不是单独的头和源
- javascript - 从元素 JavaScript 中删除嵌套类
- python - 批量 IP 地址验证
- flutter - Flutter - 在小部件 2 上的 Navigator.pop() 之后检测小部件 1 是否可见?
- spring-boot - intellij idea Rest Client 生成的 generated-requests.http 文件的保存文件位置是什么?
- shopify - Shopify 问题:服务器已在 http://localhost:8081 处准备就绪,但从未编译且未找到 Ngrok 隧道
- html - 超链接:文本与直接 - 为什么结果不同?
- oracle - 在我的机器上登录 ADW 数据库操作会使我退出其他会话
- visual-studio - 调试在对话框中加载的文件中不起作用