问题创建将 ggplot 与数据帧列表相乘,存储在绘图列表中
Problem creation multiples ggplot with a list of dataframes, stored in a list of plot
我有这个数据框列表
我想为每个数据帧生成相同的图。 (感兴趣的列共享相同的名称,即:“ COType ; Zscore ; SpatialLag ”。
这是我绘制单个数据框的代码(社区):
ggplot(List_df_EU[["COMMUNITY"]], aes(x = Zscore,
y = SpatialLag,
fill = COType)) +
geom_point(color = "white", shape = 21, size = 2) +
theme_minimal() +
geom_hline(yintercept = 0, linetype = "dashed", size = 0.7) +
geom_vline(xintercept = 0, linetype = "dashed", size = 0.7) +
scale_fill_manual(values = color_values) +
labs(title = names(List_df_EU[1]),
subtitle = "Graph de Moran sur les scores ESG des entreprises EU",
x = "z-score",
y = "Spatial lag",
fill = "Type de voisinage")+
theme_fivethirtyeight()+
theme (axis.title = element_text(), text = element_text(family = myFont1))
这是应该为每个数据框创建单个图的代码。它没有填满列表,也没有发回错误消息
plotlist <- vector()
plotlist <- for(i in 1:length(List_df_EU)) {
ggplot(List_df_EU[[i]], aes(x = Zscore,
y = SpatialLag,
fill = COType)) +
geom_point(color = "white", shape = 21, size = 2) +
theme_minimal() +
geom_hline(yintercept = 0, linetype = "dashed", size = 0.7) +
geom_vline(xintercept = 0, linetype = "dashed", size = 0.7) +
scale_fill_manual(values = color_values) +
labs(title = names(List_df_EU[[i]]),
subtitle = "Graph de Moran sur les scores ESG des entreprises EU",
x = "z-score",
y = "Spatial lag",
fill = "Type de voisinage")+
theme_fivethirtyeight()+
theme (axis.title = element_text(), text = element_text(family = myFont1))
}
如果您有任何提示,那就太棒了!(手动生成地块将是绝望的举动)
问题是您在循环的每次迭代中都将新图分配给 plotlist,而之前的迭代被覆盖。
您需要将新迭代分配给列表的新索引。
例如
for (i in listOfPlotsToMake)
{
plotlist[[i]] <- listOfPlotsToMake[i]...
}
对于 运行 相同的 ggplot
代码在数据帧列表上,我认为 lapply
比 for 循环更好。试试这个:
plotlist <- lapply(names(List_df_EU), function(x) {
ggplot(List_df_EU[[x]], aes(x = Zscore,
y = SpatialLag,
fill = COType)) +
geom_point(color = "white", shape = 21, size = 2) +
theme_minimal() +
geom_hline(yintercept = 0, linetype = "dashed", size = 0.7) +
geom_vline(xintercept = 0, linetype = "dashed", size = 0.7) +
scale_fill_manual(values = color_values) +
labs(title = x,
subtitle = "Graph de Moran sur les scores ESG des entreprises EU",
x = "z-score",
y = "Spatial lag",
fill = "Type de voisinage")+
theme_fivethirtyeight()+
theme (axis.title = element_text(), text = element_text(family = myFont1))
}
我有这个数据框列表
我想为每个数据帧生成相同的图。 (感兴趣的列共享相同的名称,即:“ COType ; Zscore ; SpatialLag ”。
这是我绘制单个数据框的代码(社区):
ggplot(List_df_EU[["COMMUNITY"]], aes(x = Zscore,
y = SpatialLag,
fill = COType)) +
geom_point(color = "white", shape = 21, size = 2) +
theme_minimal() +
geom_hline(yintercept = 0, linetype = "dashed", size = 0.7) +
geom_vline(xintercept = 0, linetype = "dashed", size = 0.7) +
scale_fill_manual(values = color_values) +
labs(title = names(List_df_EU[1]),
subtitle = "Graph de Moran sur les scores ESG des entreprises EU",
x = "z-score",
y = "Spatial lag",
fill = "Type de voisinage")+
theme_fivethirtyeight()+
theme (axis.title = element_text(), text = element_text(family = myFont1))
这是应该为每个数据框创建单个图的代码。它没有填满列表,也没有发回错误消息
plotlist <- vector()
plotlist <- for(i in 1:length(List_df_EU)) {
ggplot(List_df_EU[[i]], aes(x = Zscore,
y = SpatialLag,
fill = COType)) +
geom_point(color = "white", shape = 21, size = 2) +
theme_minimal() +
geom_hline(yintercept = 0, linetype = "dashed", size = 0.7) +
geom_vline(xintercept = 0, linetype = "dashed", size = 0.7) +
scale_fill_manual(values = color_values) +
labs(title = names(List_df_EU[[i]]),
subtitle = "Graph de Moran sur les scores ESG des entreprises EU",
x = "z-score",
y = "Spatial lag",
fill = "Type de voisinage")+
theme_fivethirtyeight()+
theme (axis.title = element_text(), text = element_text(family = myFont1))
}
如果您有任何提示,那就太棒了!(手动生成地块将是绝望的举动)
问题是您在循环的每次迭代中都将新图分配给 plotlist,而之前的迭代被覆盖。
您需要将新迭代分配给列表的新索引。 例如
for (i in listOfPlotsToMake)
{
plotlist[[i]] <- listOfPlotsToMake[i]...
}
对于 运行 相同的 ggplot
代码在数据帧列表上,我认为 lapply
比 for 循环更好。试试这个:
plotlist <- lapply(names(List_df_EU), function(x) {
ggplot(List_df_EU[[x]], aes(x = Zscore,
y = SpatialLag,
fill = COType)) +
geom_point(color = "white", shape = 21, size = 2) +
theme_minimal() +
geom_hline(yintercept = 0, linetype = "dashed", size = 0.7) +
geom_vline(xintercept = 0, linetype = "dashed", size = 0.7) +
scale_fill_manual(values = color_values) +
labs(title = x,
subtitle = "Graph de Moran sur les scores ESG des entreprises EU",
x = "z-score",
y = "Spatial lag",
fill = "Type de voisinage")+
theme_fivethirtyeight()+
theme (axis.title = element_text(), text = element_text(family = myFont1))
}