我有這個資料框串列

我想為每個資料框生成相同的圖。(感興趣的列具有相同的名稱,即:“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))
}
如果您有任何提示,那就太棒了!(將不得不手動生成情節)
uj5u.com熱心網友回復:
要在資料幀串列上運行相同的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))
}
uj5u.com熱心網友回復:
問題是您在回圈的每次迭代中都將新繪圖分配給 plotlist 并且上一次迭代被覆寫。
您需要將新迭代分配給串列的新索引。例如
for (i in listOfPlotsToMake)
{
plotlist[[i]] <- listOfPlotsToMake[i]...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471615.html
下一篇:有多個色標時如何覆寫色標?
