英文:
Specifying different ggplot titles when using for loop to make multiple plots
问题
You can update the ggtitle
line in your for loop to correctly display the Season and Year for each plot. Here's how you can modify that line:
ggtitle(paste0(EXAMPLE$Season[EXAMPLE$Cruise == Cru_], " ", EXAMPLE$Year[EXAMPLE$Cruise == Cru_]))
Replace the existing ggtitle
line in your loop with this updated line, and it should display the correct title with the Season and Year for each plot based on the cruise number.
英文:
I am using a loop to easily plot my data for different groups. I need a different plot for each cruise # (each year + season). I am having issues specifying a title that includes the Season and Year for each plot.
Example df (original data has more sites, species, cruises, dates, etc..)
structure(list(Cruise = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 44L, 44L, 44L, 44L,
44L, 44L, 44L, 44L, 44L), Season = c("Winter", "Winter", "Winter",
"Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Summer",
"Summer", "Summer", "Summer", "Summer", "Summer", "Summer", "Summer",
"Summer", "Summer", "Summer", "Summer", "Summer", "Summer", "Summer",
"Summer", "Summer", "Summer"), Year = c(2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L, 2019L, 2019L, 2019L), Taxon = c("Calanus finmarchicus",
"Calanus finmarchicus", "Calanus finmarchicus", "Centropages typicus",
"Centropages typicus", "Centropages typicus", "Temora longicornis",
"Temora longicornis", "Temora longicornis", "Calanus finmarchicus",
"Calanus finmarchicus", "Calanus finmarchicus", "Centropages typicus",
"Centropages typicus", "Centropages typicus", "Temora longicornis",
"Temora longicornis", "Temora longicornis", "Calanus finmarchicus",
"Calanus finmarchicus", "Calanus finmarchicus", "Centropages typicus",
"Centropages typicus", "Centropages typicus", "Temora longicornis",
"Temora longicornis", "Temora longicornis"), Site = c(2L, 3L,
5L, 2L, 3L, 5L, 2L, 3L, 5L, 2L, 3L, 5L, 2L, 3L, 5L, 2L, 3L, 5L,
2L, 3L, 5L, 2L, 3L, 5L, 2L, 3L, 5L), Density = c(0, 295.4030121,
8032.665912, 14219.19758, 36113.01823, 20261.50059, 823.2167022,
590.8060242, 59.945268, 27877.49705, 46128.12363, 25033.45344,
341310.9774, 134047.5388, 11149.35321, 36918.84745, 37848.71683,
140.2434366, 7684.064241, 10316.29105, 10720.82713, 89738.8931,
17193.81842, 25152.7098, 30187.39523, 0, 0)), class = "data.frame", row.names = c(NA,
-27L))
For loop for plots:
Cru <- unique(EXAMPLE$Cruise)
Cru_Dens <- list()
for(Cru_ in Cru) {
Cru_Dens[[Cru_]] = ggplot(EXAMPLE %>% filter(Cruise == Cru_),
aes(x = Site,
y=Density,
group = Taxon,
fill = Taxon)) +
geom_bar(stat = "identity") +
ggtitle(paste0(" ", EXAMPLE$Season , EXAMPLE$Year)) + #THIS IS WHAT I NEED HELP WITH
labs(x="Site", caption = paste0("Cruise: ", Cru_)) +
ylab(expression("Density (Ind \u00D7" ~ m^{2} ~ ")")) +
theme_bw() +
scale_fill_manual(values = mycolors) +
theme(legend.position = "right",
plot.title = element_text(hjust=0.5, face = "bold",
size = 15),
legend.title = element_blank(),
legend.text = element_text(size = 10),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(size = 14,
color = "black"),
axis.title.y = element_text(size = 14,
color = "black"),
axis.text.x = element_text(color = "black",
size = 12, angle = 45, hjust = 1),
axis.text.y = element_text(color = "black",
size = 12),
axis.ticks.length = unit(0.2,"cm"),
plot.caption.position = "plot",
plot.caption = element_text(size = 12)) +
scale_y_continuous(expand = expansion(mult = c(0,0.1)))
print(Cru_Dens[[Cru_]])
ggsave(Cru_Dens[[Cru_]], file=paste0("CruiseSeason_Dens_bar_", Cru_, ".png"))
}
I am not sure how to set the title so it shows the season + year for each plot.
The previous code keeps the same season + year for all plots
I also tried:
Season <- unique(EXAMPLE$Season)
Seas_l <- list()
Year <- unique(EXAMPLE$Year)
Year_l <- list()
with either:
ggtitle(paste0(" ", Seas_l , Year_l))
or
ggtitle(paste0(" ", Season , Year))
How can I have it display the correct title?
For this example, cruise 17 should have "Summer 2018" as title; cruise 44 "Summer 2019"; etc..
I haven't been able to find the answer to this after searching for a while and I struggle quite a bit with loops.
Happy to add additional information if this is not clear.
Thank you.
答案1
得分: 1
可能有更好的编码方式,但这将起作用:
ggtitle(paste0(" ", filter(EXAMPLE, Cruise==Cru_)$Season[1], " ", filter(EXAMPLE, Cruise==Cru_)$Year[1])) + #我需要帮助的部分
英文:
There might be a better way to code this, but this would work:
ggtitle(paste0(" ", filter(EXAMPLE, Cruise==Cru_)$Season[1] , " ", filter(EXAMPLE, Cruise==Cru_)$Year[1])) + #THIS IS WHAT I NEED HELP WITH
答案2
得分: 1
以下是您要翻译的内容:
考虑使用 by
(apply
家族中不常用的成员,是面向对象的 tapply
包装器)来代替通过 for
循环构建列表的簿记工作。具体来说,by
会根据因子拆分您的数据框,并将子集数据框传递到定义的函数中。不需要在循环内进行筛选。
定义单一绘图方法
proc_plot(sub) {
cat(sub$Cruise[1], "\n")
cru_plot <- (
ggplot(sub, aes(x = Site, y = Density, group = Taxon, fill = Taxon)) +
geom_bar(stat = "identity") +
ggtitle(paste0(" ", sub$Season[1], sub$Year[1])) +
labs(x = "Site", caption = paste0("Cruise: ", sub$Cruise[1])) +
ylab(expression("Density (Ind ×" ~ m^{2} ~ ")")) +
theme_bw() +
scale_fill_manual(values = mycolors) +
theme(
legend.position = "right",
plot.title = element_text(hjust = 0.5, face = "bold", size = 15),
legend.title = element_blank(),
legend.text = element_text(size = 10),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(size = 14, color = "black"),
axis.title.y = element_text(size = 14, color = "black"),
axis.text.x = element_text(
color = "black", size = 12, angle = 45, hjust = 1
),
axis.text.y = element_text(color = "black", size = 12),
axis.ticks.length = unit(0.2, "cm"),
plot.caption.position = "plot",
plot.caption = element_text(size = 12)
) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1)))
)
ggsave(
cru_plot, file = paste0("CruiseSeason_Dens_bar_", sub$Cruise[1], ".png")
)
return(cru_plot)
}
构建绘图列表
cru_plots <- by(EXAMPLE, EXAMPLE$Cruise, proc_plot)
英文:
Consider by
(underused member of apply family being the object-oriented wrapper to tapply
) instead of the bookkeeping of building a list via a for
loop. Specifically, by
splits your data frame by factors and passes subset data frames into a defined function. No filtering within loop is necessary.
Single Plot Defined Method
proc_plot(sub) {
cat(sub$Cruise[1], "\n")
cru_plot <- (
ggplot(sub, aes(x = Site, y=Density, group = Taxon, fill = Taxon)) +
geom_bar(stat = "identity") +
ggtitle(paste0(" ", sub$Season[1], sub$Year[1])) +
labs(x="Site", caption = paste0("Cruise: ", sub$Cruise[1])) +
ylab(expression("Density (Ind \u00D7" ~ m^{2} ~ ")")) +
theme_bw() +
scale_fill_manual(values = mycolors) +
theme(
legend.position = "right",
plot.title = element_text(hjust=0.5, face = "bold", size = 15),
legend.title = element_blank(),
legend.text = element_text(size = 10),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(size = 14, color = "black"),
axis.title.y = element_text(size = 14, color = "black"),
axis.text.x = element_text(
color = "black", size = 12, angle = 45, hjust = 1
),
axis.text.y = element_text(color = "black", size = 12),
axis.ticks.length = unit(0.2,"cm"),
plot.caption.position = "plot",
plot.caption = element_text(size = 12)
) +
scale_y_continuous(expand = expansion(mult = c(0,0.1)))
)
ggsave(
cru_plot, file=paste0("CruiseSeason_Dens_bar_", sub$Cruise[1], ".png")
)
return(cru_plot)
}
Build List of Plots
cru_plots <- by(EXAMPLE, EXAMPLE$Cruise, proc_plot)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论