使用来自一个 .csv 文件的 ggplot for loop 为不同的组创建并保存绘图
Create and save a plot for different groups using ggplot for loop from one .csv file
如何创建一个循环,根据 .csv 文件中的“名称”生成并保存 45 个不同的线图?
我有一个大型 .csv 文件,其中包含按日期分类的 45 座不同建筑物的数据。我需要为每个建筑物创建一个线图,在 y-axis 上显示数字,在 x-axis 上显示日期。
我尝试使用 list() 来让建筑物在循环中使用,但是真实的建筑物名称对于 R 来说太复杂了。
下面的代码生成了 45 个具有不同标题的相同图。每个地块都包含所有数据,而不仅仅是一个建筑物的数据。
# Read in .csv file
d <- read_csv("res_data_ABC.csv")
# Reshape the data to make it longer and omit NA
d_long <- na.omit(pivot_longer(d, cols = -Name, names_to = "date", values_to = "n"))
print(d_long)
# A tibble: 114 x 3
Name date n
<chr> <chr> <dbl>
1 Building 1 09/14/20 2030
2 Building 1 09/21/20 17900
3 Building 1 09/30/20 1380
4 Building 2 09/14/20 57
5 Building 2 09/21/20 0
6 Building 2 09/28/20 301
7 Building 3 09/14/20 79200
8 Building 3 09/21/20 23700
9 Building 3 09/30/20 21800
10 Building 4 09/16/20 496
# … with 104 more rows
# Begin for loop for each building's plot
for (i in d_long$Name) {
ggplot(d_long, aes(date, n)) + geom_line() + ggtitle(i) + theme(axis.text.x = element_text(angle = 90)) + ylab("viral density/L") + theme(plot.title = element_text(size = 14, face = "bold", hjust=0.5), axis.title.x = element_text(size = 12, face = "bold"), axis.title.y = element_text(size = 12, face = "bold"))
ggsave(paste0("~/Desktop/plots/", i, ".png"), width = 6, height = 5, units = "cm", dpi=300)
}
这些是制作的情节:https://i.stack.imgur.com/5HWI3.png
我想不通如何通过单个建筑物来限制每个地块中的数据。
在 for 循环中,您正在使用整个数据框。你应该在你的for循环中过滤它
for (i in d_long$Name) {
ggplot(d_long[d_long$Name == i,], aes(date, n)) + geom_line() + ggtitle(i) + theme(axis.text.x = element_text(angle = 90)) + ylab("viral density/L") + theme(plot.title = element_text(size = 14, face = "bold", hjust=0.5), axis.title.x = element_text(size = 12, face = "bold"), axis.title.y = element_text(size = 12, face = "bold"))
ggsave(paste0("~/Desktop/plots/", i, ".png"), width = 6, height = 5, units = "cm", dpi=300)
}
如何创建一个循环,根据 .csv 文件中的“名称”生成并保存 45 个不同的线图?
我有一个大型 .csv 文件,其中包含按日期分类的 45 座不同建筑物的数据。我需要为每个建筑物创建一个线图,在 y-axis 上显示数字,在 x-axis 上显示日期。
我尝试使用 list() 来让建筑物在循环中使用,但是真实的建筑物名称对于 R 来说太复杂了。
下面的代码生成了 45 个具有不同标题的相同图。每个地块都包含所有数据,而不仅仅是一个建筑物的数据。
# Read in .csv file
d <- read_csv("res_data_ABC.csv")
# Reshape the data to make it longer and omit NA
d_long <- na.omit(pivot_longer(d, cols = -Name, names_to = "date", values_to = "n"))
print(d_long)
# A tibble: 114 x 3
Name date n
<chr> <chr> <dbl>
1 Building 1 09/14/20 2030
2 Building 1 09/21/20 17900
3 Building 1 09/30/20 1380
4 Building 2 09/14/20 57
5 Building 2 09/21/20 0
6 Building 2 09/28/20 301
7 Building 3 09/14/20 79200
8 Building 3 09/21/20 23700
9 Building 3 09/30/20 21800
10 Building 4 09/16/20 496
# … with 104 more rows
# Begin for loop for each building's plot
for (i in d_long$Name) {
ggplot(d_long, aes(date, n)) + geom_line() + ggtitle(i) + theme(axis.text.x = element_text(angle = 90)) + ylab("viral density/L") + theme(plot.title = element_text(size = 14, face = "bold", hjust=0.5), axis.title.x = element_text(size = 12, face = "bold"), axis.title.y = element_text(size = 12, face = "bold"))
ggsave(paste0("~/Desktop/plots/", i, ".png"), width = 6, height = 5, units = "cm", dpi=300)
}
这些是制作的情节:https://i.stack.imgur.com/5HWI3.png
我想不通如何通过单个建筑物来限制每个地块中的数据。
在 for 循环中,您正在使用整个数据框。你应该在你的for循环中过滤它
for (i in d_long$Name) {
ggplot(d_long[d_long$Name == i,], aes(date, n)) + geom_line() + ggtitle(i) + theme(axis.text.x = element_text(angle = 90)) + ylab("viral density/L") + theme(plot.title = element_text(size = 14, face = "bold", hjust=0.5), axis.title.x = element_text(size = 12, face = "bold"), axis.title.y = element_text(size = 12, face = "bold"))
ggsave(paste0("~/Desktop/plots/", i, ".png"), width = 6, height = 5, units = "cm", dpi=300)
}