r - 使用 ggplot2 在 for 循环中添加图例标题
问题描述
我有以下代码来绘制列表图中的数据框。绘图有 18 个数据帧。数据帧的命名约定如下:84-9、84-12、84-15、92-9、92-12、92-15、100-9、100-12、100-15、108-9、 108-12、108-15、116-9、116-12、116-15、124-9、124-12、124-15。
下面提供了列表中的示例数据框。所有数据帧都具有相同的格式和大小:
Plot[["84-9"]]
Names Type Shear Moment
<chr> <chr> <dbl> <dbl>
1 Baseline ext_multi 0.824 0.614
2 Baseline ext_single 0.734 0.464
3 Baseline int_multi 0.727 0.527
4 Baseline int_single 0.599 0.338
5 Sample ext_multi 0.829 0.626
6 Sample ext_single 0.737 0.475
7 Sample int_multi 0.712 0.512
8 Sample int_single 0.595 0.327
9 Sample ext_multi 0.823 0.611
10 Sample ext_single 0.737 0.464
# ... with 34 more rows
我使用以下代码
library(ggplot2)
for (i in 1:length(Plot)) {
plot.Shear <- ggplot(data = subset(Plot[[i]], Names = "Sample"), aes(x = Type, y = Shear)) +
geom_boxplot(outlier.shape = NA) +
stat_summary(fun = mean, geom="point", shape=23, size=3) +
stat_boxplot(geom='errorbar', linetype=1, width=0.5) +
geom_point(data = subset(Plot[[i]], Names != "Sample"), aes(colour = Names)) +
scale_color_manual(values=c("red","green4","black")) +
theme(legend.title=element_blank()) +
theme(axis.title.x=element_blank()) +
theme(axis.title.y=element_blank()) +
labs(title = "Shear Live Load Distribution Factors") +
theme(plot.title = element_text(hjust = 0.5))
print(plot.Shear)
}
目前,我正在删除图例标题,否则它将读取列标题“名称”。我想在循环中添加图例标题,这样每个图例标题都会显示“UG”,然后是为其生成图的数据框的名称。示例 UG84-9。
解决方案
您的数据可能无助于完全重现您的问题。我通常会尽量避免在不必要的时候创建一个循环!可视化单独的组可能有更好的选择。 考虑分面
带有循环的选项(我不想那样做)
Ps 我已将您的情节代码精简到更重要的部分。
library(tidyverse)
Plot <- list('84-9' = mydat, '84-12' = mydat) # make fake list - this is repeating your data frame twice for convenience.
plot_list <- list() #empty list of plots
for (i in 1:length(Plot)) {
plot_list[[i]] <- ggplot(data = Plot[[i]], aes(x = Type, y = Shear, color = Type)) +
geom_point() +
scale_color_discrete() +
labs(title = "Shear Live Load Distribution Factors",
color = paste('UG', names(Plot)[i])) #that is the essential bit how to change the legend title.
}
#now plot the list of plots with e.g. patchwork
patchwork::wrap_plots(plot_list, nrow = 2)
或者,可以说更好更直接,只使用刻面
data_plot <- Plot %>% bind_rows(.id = 'ID')# bind all data frames
ggplot(data = data_plot, aes(x = Type, y = Shear, color = Type)) +
geom_point() +
scale_color_discrete() +
labs(title = "Shear Live Load Distribution Factors") +
facet_wrap(~ID, nrow = 2)
由reprex 包于 2020-03-30 创建(v0.3.0)
数据
#devtools::install_github("alistaire47/read.so")
mydat <- read.so::read_so("Names Type Shear Moment
<chr> <chr> <dbl> <dbl>
1 Baseline ext_multi 0.824 0.614
2 Baseline ext_single 0.734 0.464
3 Baseline int_multi 0.727 0.527
4 Baseline int_single 0.599 0.338
5 Sample ext_multi 0.829 0.626
6 Sample ext_single 0.737 0.475
7 Sample int_multi 0.712 0.512
8 Sample int_single 0.595 0.327
9 Sample ext_multi 0.823 0.611
10 Sample ext_single 0.737 0.464")
推荐阅读
- python - 有机会在 PyCharm 中获得 Tensorflow 2.0 的完全自动完成功能吗?
- c++ - SFML 和 OpenGL 在调用绘图函数时会导致 GL_INVALID_OPERATIONS
- php - 使用 wamp apache 服务器设置本地 Windows 注册表
- mongodb - 无法连接 MongoDB docker-compose
- c++ - Visual Studio 编译器错误
- botframework - 如何在 Microsoft Teams 应用中代表用户发布消息?
- c++ - 使用 gcc 编译具有 inline-assembly 和 Intel 语法的 c++ 代码并实现它
- azure - azure 应用服务身份验证模块是否支持 azure aad b2c?
- elasticsearch - elasticsearch Basic 许可证中每个集群的最大节点数是多少?
- c# - 我想创建一个共享数据库 c# windows 窗体应用程序?