R // ggplot2:结合facet_wrap和for循环时的动态标题

锐钛矿

我有一个时空数据集,其中包含我希望随时间推移映射的一组位置的各种指标。

这是我的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)
}

问题是,而不是有变种的实际名称iggtitle,我得到的第一个值。我如何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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

R:在 ggplot2 中重新标记 facet_wrap 标题

R: ggplot2 使用 facet_wrap 设置中间的最后一个图

使用 R 中 ggplot2 的 facet_wrap 功能對多個數據集進行 Boxplot 比較?

R ggplot2 facet_wrap重新排序子图并为每个ID标签设置不同的颜色

如何在 R ggplot2 中的 geom_smooth (facet_wrap) 中传递多个公式?

ggplot 在 R 中使用 facet_wrap?

R ggplot facet_wrap 带条件

在R中的facet_wrap ggplot中的方括号上方添加多个标题/文本

在R ggplot2和ggforce中使用facet_wrap_paginate缺少x轴标签

使用ggplot2 facet wrap R的控制图

使用ggplot2结合facet_wrap和95%的密度图面积

从默认更改ggplot2 :: facet_wrap标题

通过使用facet_wrap将r平方成ggplot

将标题添加到ggplot2图中,因为标题显示在facet_wrap图中

R-Facet_Wrap 每 n 列可能吗?ggplot2

R ggplot2 facet wrap dot plot 重新排序每个

R Shiny ggplot2 折线图在使用“key”属性和 facet_grid 时不会显示线条

ggplot2中的facet_wrap()和facet_grid()有什么区别?

R:使用包含等号的字符串重新标记facet_wrap标题

r facet_wrap 奇数图,x 轴标题上的中心底部图

R:没有因素的facet_wrap()

如何使用ggplot在R中创建不均匀的facet_wrap网格?

R / ggplot:带有facet_wrap的垂直条文本

ggplot在R中使用多个data.frame的facet_wrap?

使用R中ggplot的facet_wrap的流动持续时间曲线

如何使用facet_wrap在R中ggplot多个位置点?

如何使用 R 中的 facet_wrap 使用上下限进行 ggplot 着色?

为 R 中的 facet_wrap 直方图的每个图分配自定义颜色 - ggplot

指定组时,ggplot2中带有facet_wrap的geom_text