FUN(X[[i]], ...) 中的错误:对象 'XXX'在ggplot中找不到

发布于 2025-01-10 21:23:34 字数 2436 浏览 0 评论 0原文

我有一个错误

FUN(X[[i]], ...) 中出现错误:对象“X2019..YR2019”。未找到。

我知道有一些帖子讨论了这个错误,但没有一个真正帮助我解决我的问题或与我的情况类似。

这是我的代码。我尝试在 2019 年的世界地图上显示生育率。当我运行最后一个代码来显示图表时,出现错误。

# combine data frame with map
fertility_rate_map <- joinCountryData2Map(fertility_rate, 
                                  joinCode = "ISO3",
                                  nameJoinColumn = "Country.Code")


-----------------
# create cut off points in Population
fertility_rate_map$X2019..YR2019._C <- base::cut(fertility_rate_map$X2019..YR2019.,
                              breaks = c(0, 1, 2, 3, 
                                         4, 5, 6,
                                         7, 8), 
                              labels = 1:8, right = F, ordered_result = T)

-----1-----
# define colors
palette = colorRampPalette(brewer.pal(n=8, name='Oranges'))(8)
palette = c("white", palette)
# create map
world_fertility <- ggplot() +
  geom_sf(data = fertility_rate_map, aes(fill = X2019..YR2019._C)) + 
  scale_fill_manual(values = palette) +
# customize legend title
labs(fill = "Fertility Rate") +
theme(panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(),
      # surpress legend
      legend.position = "none")

-----2-----
# create map
world_fertility <- ggplot(data = fertility_rate_map) +  
  ggplot2::geom_sf(data = fertility_rate_map) +  
  aes(fill = X2019..YR2019._C) +  
  scale_fill_viridis_c(option = "magma") +  
  ggplot2::coord_sf(xlim = c(-175, 47.5),  
                    ylim = c(23, 85),  
                    expand = FALSE) +  
  labs(title = "World Fertilty Rate",  
       subtitle = "around the world",  
       fill = "Share of Fertility (%)") +  
  theme_bw() +  
  theme(plot.title =  
          element_text(hjust = 0.5)) +  
  theme(plot.subtitle =
          element_text(hjust = 0.5))
----------
# display map
world_fertility

在创建地图之前,我使用 joinCountryData2Map 按国家/地区代码加入 Fertility_rate 和内置数据世界。然后,我将人口中的截止点创建为 8 个断点,称为“X2019..YR2019._C”。我尝试了两种不同的代码来创建地图,但它们都使用相同的包。

我觉得这个错误很奇怪,因为我已经检查了该变量是否在 Fertility_rate_map 中,那么它就在那里!非常不确定为什么错误消息说找不到它。我猜错误可能来自 geom_sf(data = Fertility_rate_map, aes(fill = Factor(X2019..YR2019._C)))。 顺便说一句,我的连接数据是否有可能是数据框而不是数据表,所以会出现此错误?我认为这并不重要,但对此不确定。

我对 R 还是新手,希望可以从这里得到任何帮助,以便我可以完成我的项目:(

在此处输入图像描述

I have got an error for

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

I know there are some posts discussing about this error, while none of them really help me to solve my issue or are in the similar case as mine.

Here is my code. I tried to display the fertility rate on a world map in 2019. The error shows as I run the last code to display the graph.

# combine data frame with map
fertility_rate_map <- joinCountryData2Map(fertility_rate, 
                                  joinCode = "ISO3",
                                  nameJoinColumn = "Country.Code")


-----------------
# create cut off points in Population
fertility_rate_map$X2019..YR2019._C <- base::cut(fertility_rate_map$X2019..YR2019.,
                              breaks = c(0, 1, 2, 3, 
                                         4, 5, 6,
                                         7, 8), 
                              labels = 1:8, right = F, ordered_result = T)

-----1-----
# define colors
palette = colorRampPalette(brewer.pal(n=8, name='Oranges'))(8)
palette = c("white", palette)
# create map
world_fertility <- ggplot() +
  geom_sf(data = fertility_rate_map, aes(fill = X2019..YR2019._C)) + 
  scale_fill_manual(values = palette) +
# customize legend title
labs(fill = "Fertility Rate") +
theme(panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(),
      # surpress legend
      legend.position = "none")

-----2-----
# create map
world_fertility <- ggplot(data = fertility_rate_map) +  
  ggplot2::geom_sf(data = fertility_rate_map) +  
  aes(fill = X2019..YR2019._C) +  
  scale_fill_viridis_c(option = "magma") +  
  ggplot2::coord_sf(xlim = c(-175, 47.5),  
                    ylim = c(23, 85),  
                    expand = FALSE) +  
  labs(title = "World Fertilty Rate",  
       subtitle = "around the world",  
       fill = "Share of Fertility (%)") +  
  theme_bw() +  
  theme(plot.title =  
          element_text(hjust = 0.5)) +  
  theme(plot.subtitle =
          element_text(hjust = 0.5))
----------
# display map
world_fertility

Before I create the map, I use the joinCountryData2Map to join the fertility_rate and built-in data world by the Country code. Then, I create cut off points in population into 8 breaks, called "X2019..YR2019._C". I tried two different codes to create the map but they all use same packages.

I feel this error is pretty weird, since I have checked whether the variable is in the fertility_rate_map, then it is there! Pretty not sure why the error message said it cannot found it. I guess the error might come from geom_sf(data = fertility_rate_map, aes(fill = factor(X2019..YR2019._C))).
Btw, is it possible that my joined data is a dataframe but not a datatable so this error comes? I think this doesn't really matter but not sure about this.

I am still new to R, hope can receive any helps from here so that I could finish my project :(

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文