我有一个时空数据集,其中包含我希望随时间推移映射的一组位置的各种指标。
这是我的facet_wrap +循环
#basemap
basemap <- ggplot() +
geom_sf(data= country_adm2) +
coord_sf() +
theme_void()
print(basemap)
#joining dataframe to polygons
df_poly <- left_join(country_adm2, df, by= "County") ##joins the data by County.
#loop & facet_wrap plots
## This goes over the dataset (df_poly) to plot the the three variables, and facet_wrap over ~Date to see time variation (over 12 months)
for(i in df_poly[40:42]){ ## the vars I wish to plot to iterate the temporal map through
imap <- basemap +
geom_sf(aes(fill = i), data= df_poly ) +
scale_fill_viridis(option = "cividis", direction= 1, alpha= 1, )+
facet_wrap(~ Date) + ##the temporal facet_wrap
ggtitle(paste0("Indicators:", i)) +
labs(fill = "% of\ncovered population")
print(imap)
}
问题是,而不是有变种的实际名称i
中ggtitle
,我得到的第一个值。我如何names(df_poly[40:42])
正确遍历每个图?我研究了各种方法,但没有一个起作用。
我认为制作循环的方式似乎是问题所在。我假设我需要遍历一个列表而不是直接遍历数据框,但是我不确定如何执行此操作。
尝试这个。您必须遍历变量的名称。我添加了以##开头的注释,并在其中更改了代码。
#basemap
basemap <- ggplot() +
geom_sf(data= country_adm2) +
coord_sf() +
theme_void()
print(basemap)
#joining dataframe to polygons
df_poly <- left_join(country_adm2, df, by= "County") ##joins the data by County.
#loop & facet_wrap plots
## This goes over the dataset (df_poly) to plot the the three variables, and facet_wrap over ~Date to see time variation (over 12 months)
## loop over the names of the vars to plot
for(i in names(df_poly)[40:42]){ ## the vars I wish to plot to iterate the temporal map through
imap <- basemap +
## Convert names to symbols using!!sym()
geom_sf(aes(fill = !!sym(i)), data = df_poly ) +
scale_fill_viridis(option = "cividis", direction= 1, alpha= 1, )+
facet_wrap(~ Date) + ##the temporal facet_wrap
ggtitle(paste0("Indicators:", i)) +
labs(fill = "% of\ncovered population")
print(imap)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句