R语言 如何使用ggmagnify和ggplot2制作Map图的放大插图

wlzqhblo  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(670)

在过去一周的tidytuesday挑战中,分析了Tornado数据集,我尝试使用ggplot2练习ggmagnify包,以使用美国Map制作德克萨斯州的放大插图Map。因此,让我写下代码来演示我到目前为止所实现的内容,然后是我正在询问的问题。

library(tidyverse)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(bertin)
library(usmap)
library(maps)
library(showtext)
library(showtextdb)
library(ggtext)
library(cowplot)
library(ggmagnify)

tornados <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-05-16/tornados.csv')
state_names_df <- tibble(state = state.name) %>%
  bind_cols(tibble(abb = state.abb)) %>% 
  bind_rows(tibble(state = "District Of Columbia", abb = "DC")) %>%
  as.data.frame()

maps_states_df <- st_as_sf(map("state", plot = FALSE, fill = TRUE)) %>%
  mutate(ID = str_to_title(ID)) 

us_geo_data <- state_names_df %>%
  left_join(maps_states_df, by = c("state" = "ID"))

tornados_acrros_us <- ggplot(us_geo_data) +
  geom_sf(aes(geometry = geom), linewidth = 0.1) +
  geom_point(data = tornados %>% filter(!st %in% c("VI", "DC", "AK", "PR", "HI")), aes(x = slon, y = slat), size = 0.2, color = "darkgreen", alpha = 0.2) +
  ggthemes::theme_map()

这段代码生成了以下Map:

现在,我想实现的是,正如我上面提到的,用ggmagnify包绘制德克萨斯州的插图,我想在上面练习。下面是我尝试做的代码。

tornados_acrros_us + geom_magnify(from = us_geo_data %>% filter(abb == "TX"),
                                  to =  c(-125, -98, 10, 30))

但是,这抛出了一个错误,表示

Warning messages:
1: In min(from$x) : No missing arguments for min; Returning inf
2: In min(from$y) : No missing arguments for min; Returning inf
3: In max(from$x) : No missing arguments for max; Returning -inf
4: In max(from$y) : No missing arguments for max; Returning -inf
5: Removed 51 rows containing missing values (`geom_magnify()`).

我无法找到一个解决方案,使Map,我的目标,因此,我想感谢您的任何援助,以解决这个问题。先谢谢你。

czq61nw1

czq61nw11#

我认为这里的问题是您的us_geo_data只是一个 Dataframe 而不是sf对象。我们可以使用right_join()而不是left_join()来解决这个问题:

tornados <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-05-16/tornados.csv')
state_names_df <- tibble(state = state.name) %>%
  bind_cols(tibble(abb = state.abb)) %>% 
  bind_rows(tibble(state = "District Of Columbia", abb = "DC")) %>%
  as.data.frame()
maps_states_df <- st_as_sf(map("state", plot = FALSE, fill = TRUE)) %>%
  mutate(ID = str_to_title(ID)) 
us_geo_data <- right_join(maps_states_df, state_names_df, by = c("ID" = "state"))

然后绘图:

tornados_across_us <- ggplot(us_geo_data) +
  geom_sf(linewidth = 0.1) +
  geom_point(data = filter(tornados, !st %in% c("VI", "DC", "AK", "PR", "HI")) ,
            aes(x = slon, y = slat), size = 0.2, color = "darkgreen", alpha = 0.2) +
  ggthemes::theme_map()
tornados_across_us

tornados_across_us + geom_magnify(aes(from = abb == "TX"),
                    to = c(-125, -98, 10, 30), 
                    shadow = TRUE, linewidth = 1, colour = "orange2",
                    shape = "outline", 
                    aspect = "fixed", 
                    expand = 0)

其给出:

请注意,这只适用于从GitHub安装的最新开发版本的{ggmagnify}。您可以使用to中的值或限制来更好地定位插图。

相关问题