在 r 中创建制图时出现问题
Issue while creating cartogram plot in r
我是 Spatial data
& cartogram
lib 的新手,在尝试从以下位置重新创建情节时遇到了一些问题:https://www.r-graph-gallery.com/a-smooth-transition-between-chloropleth-and-cartogram.html
资料库与数据
library(tidyverse)
library(maptools)
library(cartogram)
library(viridis)
library(sf)
data("wrld_simpl")
afr_cartogram = wrld_simpl[wrld_simpl$REGION==2,]
在此之后,我遇到了一些 error: like st_transform
..... 我使用 sf
lib 进行了一些谷歌搜索后修复了它。
afr_sf <- st_as_sf(afr_cartogram)
afr_sf_proj = st_transform(afr_sf,3857)
afr_plot <- cartogram::cartogram(afr_sf_proj, "POP2005", itermax =7)
问题: 现在,在这一步之后,我无法重新创建代码,因为它在 演示网站 中,因为我 我的数据中没有列 group
。
ggplot() +
geom_polygon(data = afr_plot, aes(fill = POP2005/1000000, x = LON, y = LAT, group = group) , size=0, alpha=0.9) +
theme_void()
从哪里可以获得 group
列 ???
网站使用的代码:
data(wrld_simpl)
afr=wrld_simpl[wrld_simpl$REGION==2,]
afr_cartogram <- cartogram(afr, "POP2005", itermax=7)
# Transform these 2 objects in dataframe, plotable with ggplot2
afr_cartogram_df <- tidy(afr_cartogram) %>% left_join(. , afr_cartogram@data, by=c("id"="ISO3"))
afr_df <- tidy(afr) %>% left_join(. , afr@data, by=c("id"="ISO3"))
# And using the advices of chart #331 we can custom it to get a better result:
ggplot() +
geom_polygon(data = afr_df, aes(fill = POP2005/1000000, x = long, y = lat, group = group) , size=0, alpha=0.9) +
theme_void() +
scale_fill_viridis(name="Population (M)", breaks=c(1,50,100, 140), guide = guide_legend( keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"), label.position = "bottom", title.position = 'top', nrow=1)) +
labs( title = "Africa", subtitle="Population per country in 2005" ) +
ylim(-35,35) +
theme(
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#f5f5f4", color = NA),
panel.background = element_rect(fill = "#f5f5f4", color = NA),
legend.background = element_rect(fill = "#f5f5f4", color = NA),
plot.title = element_text(size= 22, hjust=0.5, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
plot.subtitle = element_text(size= 13, hjust=0.5, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
legend.position = c(0.2, 0.26)
) +
coord_map()
group
列是在这些行中生成的
afr_cartogram_df <- tidy(afr_cartogram) %>%
left_join(afr_cartogram@data, by = ("id" = "ISO3"))
afr_df <- tidy(afr) %>%
left_join(afr@data, by = c("id" = "ISO3"))
通过程序包 broom
中的 tidy
函数,该函数未附加在您的代码中!
使用 library(broom)
附加 broom
或从其命名空间调用 tidy()
,如下所示:broom::tidy(...)
.
代码中的 'data section' 应如下所示:
data(wrld_simpl)
afr <- wrld_simpl[wrld_simpl$REGION==2, ]
afr_cartogram <- wrld_simpl[wrld_simpl$REGION == 2,]
afr_sf <- st_as_sf(afr_cartogram)
afr_sf_proj <- st_transform(afr_sf, 3857)
afr_plot <- cartogram_cont(afr_sf_proj, "POP2005", itermax =7)
afr_cartogram_df <- broom::tidy(afr_cartogram) %>%
left_join(afr_cartogram@data, by=c("id" = "ISO3"))
afr_df <- broom::tidy(afr) %>%
left_join(afr@data, by=c("id" = "ISO3"))
随后的 ggplot
代码可以正常工作:
我是 Spatial data
& cartogram
lib 的新手,在尝试从以下位置重新创建情节时遇到了一些问题:https://www.r-graph-gallery.com/a-smooth-transition-between-chloropleth-and-cartogram.html
资料库与数据
library(tidyverse)
library(maptools)
library(cartogram)
library(viridis)
library(sf)
data("wrld_simpl")
afr_cartogram = wrld_simpl[wrld_simpl$REGION==2,]
在此之后,我遇到了一些 error: like st_transform
..... 我使用 sf
lib 进行了一些谷歌搜索后修复了它。
afr_sf <- st_as_sf(afr_cartogram)
afr_sf_proj = st_transform(afr_sf,3857)
afr_plot <- cartogram::cartogram(afr_sf_proj, "POP2005", itermax =7)
问题: 现在,在这一步之后,我无法重新创建代码,因为它在 演示网站 中,因为我 我的数据中没有列 group
。
ggplot() +
geom_polygon(data = afr_plot, aes(fill = POP2005/1000000, x = LON, y = LAT, group = group) , size=0, alpha=0.9) +
theme_void()
从哪里可以获得 group
列 ???
网站使用的代码:
data(wrld_simpl)
afr=wrld_simpl[wrld_simpl$REGION==2,]
afr_cartogram <- cartogram(afr, "POP2005", itermax=7)
# Transform these 2 objects in dataframe, plotable with ggplot2
afr_cartogram_df <- tidy(afr_cartogram) %>% left_join(. , afr_cartogram@data, by=c("id"="ISO3"))
afr_df <- tidy(afr) %>% left_join(. , afr@data, by=c("id"="ISO3"))
# And using the advices of chart #331 we can custom it to get a better result:
ggplot() +
geom_polygon(data = afr_df, aes(fill = POP2005/1000000, x = long, y = lat, group = group) , size=0, alpha=0.9) +
theme_void() +
scale_fill_viridis(name="Population (M)", breaks=c(1,50,100, 140), guide = guide_legend( keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"), label.position = "bottom", title.position = 'top', nrow=1)) +
labs( title = "Africa", subtitle="Population per country in 2005" ) +
ylim(-35,35) +
theme(
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#f5f5f4", color = NA),
panel.background = element_rect(fill = "#f5f5f4", color = NA),
legend.background = element_rect(fill = "#f5f5f4", color = NA),
plot.title = element_text(size= 22, hjust=0.5, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
plot.subtitle = element_text(size= 13, hjust=0.5, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
legend.position = c(0.2, 0.26)
) +
coord_map()
group
列是在这些行中生成的
afr_cartogram_df <- tidy(afr_cartogram) %>%
left_join(afr_cartogram@data, by = ("id" = "ISO3"))
afr_df <- tidy(afr) %>%
left_join(afr@data, by = c("id" = "ISO3"))
通过程序包 broom
中的 tidy
函数,该函数未附加在您的代码中!
使用 library(broom)
附加 broom
或从其命名空间调用 tidy()
,如下所示:broom::tidy(...)
.
代码中的 'data section' 应如下所示:
data(wrld_simpl)
afr <- wrld_simpl[wrld_simpl$REGION==2, ]
afr_cartogram <- wrld_simpl[wrld_simpl$REGION == 2,]
afr_sf <- st_as_sf(afr_cartogram)
afr_sf_proj <- st_transform(afr_sf, 3857)
afr_plot <- cartogram_cont(afr_sf_proj, "POP2005", itermax =7)
afr_cartogram_df <- broom::tidy(afr_cartogram) %>%
left_join(afr_cartogram@data, by=c("id" = "ISO3"))
afr_df <- broom::tidy(afr) %>%
left_join(afr@data, by=c("id" = "ISO3"))
随后的 ggplot
代码可以正常工作: