我正在处理RspData包中的worldworldbank_df个数据集,需要对我的数据进行子集,并将结果绘制成图表.我 Select 了"大陆"和"城市流行"列,删除所有NA值,按大陆分组,总结所有大陆的平均城市人口.然而,当我使用geom_sf调用绘制结果时,我得到了一个错误:

Error in FUN(X[[i]], ...) : object 'mean_urban_pop' not found

我需要在世界 map 中绘制这些数据,但它对我不起作用,因为geom个坐标没有被转移到我的新数据集中.

How can I get these results graphed?

注:下面投影的坐标系是我必须使用的坐标系.

这是我的代码:

library(tidyverse)
library(sf)
library(spData)
library(ggplot2)

#Load in the data
world <- spData::world
wdb <- spData::worldbank_df

#Combine the data frames
wld_jn <- left_join(world, wdb, by = c('iso_a2', 'name_long' = 'name'))

#Reproject to required coordinate system. Obtained from: https://spatialreference.org/ref/sr-org/6/
wld_jn <- st_transform(wld_jn, '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs')

def_wld_jn <- st_set_crs(wld_jn, '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs')

#Graph mean urban population across continent:
wld_jn %>%
  select(continent, urban_pop) %>% 
  drop_na() %>% 
  group_by(continent) %>% 
  summarise_at(vars(urban_pop), list(mean_urban_pop = mean)) %>% 
  ggplot(.) +
  geom_sf(data = wld_jn, aes(fill = mean_urban_pop)) +
  scale_fill_gradient2(midpoint = 285)+
  guides(fill = guide_colorbar(title = "Population",
                             title.position = "bottom",
                             title.theme = element_text(size = 10,
                                                        face = "bold",
                                                        colour = "gray70",
                                                        angle = 0))) +
  ggtitle("World Urban Population") +
  theme(plot.title = element_text(hjust = 0.5))

推荐答案

问题似乎在于使用summarise()而不是mutate();当你使用summarise()时,你只保留感兴趣的变量,例如.

library(tidyverse)
head(mtcars)
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

mtcars %>%
  group_by(cyl) %>%
  summarise(mean_mpg = mean(mpg)) %>%
  head()
#> # A tibble: 3 × 2
#>     cyl mean_mpg
#>   <dbl>    <dbl>
#> 1     4     26.7
#> 2     6     19.7
#> 3     8     15.1

mtcars %>%
  group_by(cyl) %>%
  mutate(mean_mpg = mean(mpg)) %>%
  head()
#> # A tibble: 6 × 12
#> # Groups:   cyl [3]
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb mean_mpg
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>    <dbl>
#> 1  21       6   160   110  3.9   2.62  16.5     0     1     4     4     19.7
#> 2  21       6   160   110  3.9   2.88  17.0     0     1     4     4     19.7
#> 3  22.8     4   108    93  3.85  2.32  18.6     1     1     4     1     26.7
#> 4  21.4     6   258   110  3.08  3.22  19.4     1     0     3     1     19.7
#> 5  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2     15.1
#> 6  18.1     6   225   105  2.76  3.46  20.2     1     0     3     1     19.7

reprex package(v2.0.1)于2022年2月23日创建


如果将sumamrise()改为mutate()(并将geometry = geom加为geom_sf()),则不会出现错误.这能解决你的问题吗?

library(tidyverse)
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
#install.packages("spData")
library(spData)
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`

#Load in the data
world <- spData::world
wdb <- spData::worldbank_df

#Combine the data frames
wld_jn <- left_join(world, wdb, by = c('iso_a2', 'name_long' = 'name'))

#Reproject to required coordinate system. Obtained from: https://spatialreference.org/ref/sr-org/6/
wld_jn <- st_transform(wld_jn, '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs')

def_wld_jn <- st_set_crs(wld_jn, '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs')


#Graph mean urban population across continent:
wld_jn %>%
  select(continent, urban_pop) %>% 
  drop_na() %>%
  group_by(continent) %>% 
  mutate(mean_urban_pop = mean(urban_pop)) %>% 
  ggplot() +
  geom_sf(aes(fill = mean_urban_pop, geometry = geom)) +
  scale_fill_gradient2(midpoint = 285)+
  guides(fill = guide_colorbar(title = "Population",
                               title.position = "bottom",
                               title.theme = element_text(size = 10,
                                                          face = "bold",
                                                          colour = "gray70",
                                                          angle = 0))) +
  ggtitle("World Urban Population") +
  theme(plot.title = element_text(hjust = 0.5))

reprex package(v2.0.1)于2022年2月23日创建

R相关问答推荐

如何将具有重复名称的收件箱合并到R中的另一列中,而结果不同?

将模拟变量乘以多个观测结果中的模拟变量

使用rlang s arg_match判断函数输入列表

无法运行通过R中的Auto.arima获得的ARIMA模型

R创建一个数据透视表,计算多个组的百分比

在R中查找每个组不同时间段的总天数

更改绘图上的x轴断点,而不影响风险?

如何删除R中除某些特定名称外的所有字符串?

如何在modelsummary中重命名统计数据?

条形图和在Ploly中悬停的问题

合并DFS列表并将索引提取为新列

从多个线性回归模型中提取系数

为左表中的所有行使用值Fill滚动左连接

手动指定从相同数据创建的叠加图的 colored颜色

R基于变量组合创建新的指标列

自定义交互作用图的标签

如何调整一个facet_work()面板内的框图和移动标签之间的水平宽度?

如何在Quarto中使用美人鱼图表中的标记来加粗文本

如果满足条件,则替换列的前一个值和后续值

使用同一行中的前一个值填充R矩阵中的缺失值