英文:
How to make a magnified inset of a map plot with ggmagnify and ggplot2
问题
以下是已翻译的内容:
对于过去一周的```tidytuesday```挑战之一,即分析```Tornado```数据集的挑战,我尝试使用```ggplot2```中的```ggmagnify```包创建了一个缩放的德克萨斯地图与美国地图的插图地图。所以,让我写下代码来演示我迄今为止所实现的内容,接下来将是我提出的问题。
```R
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_across_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()
这段代码生成了以下地图:
现在,正如我上面提到的,我想要实现的是使用ggmagnify
包绘制德克萨斯的插图地图。以下是我尝试这样做的代码。
tornados_across_us + geom_magnify(from = us_geo_data %>% filter(abb == "TX"),
to = c(-125, -98, 10, 30))
然而,这会引发一个错误,说道:
警告信息:
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()`).
我无法找到解决此问题的方法,因此,我想提前感谢您提供的任何帮助来解决此问题。谢谢!
<details>
<summary>英文:</summary>
For one of the past week's ```tidytuesday``` challenge, the one where ```Tornado``` dataset was analyzed, I tried to practice ```ggmagnify``` package with ```ggplot2``` to make a magnified inset map of Texas with the US map. So, let me write down the code to demonstrate what I have achieved so far, which will be followed by the question I am inquiring on.
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()
This code produced the following map:
[![enter image description here][1]][1]
Now, what I want to achieve, as I mentioned above, to plot an inset map of Texas with ``` ggmagnify ``` package I would like to practice on. Here is the code I have tried to do that.
tornados_acrros_us + geom_magnify(from = us_geo_data %>% filter(abb == "TX"),
to = c(-125, -98, 10, 30))
However, this threw an error saying that
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()
).
I could not find a solution on making the map I am aiming for and therefore, I would like to appreciate any assistance from you to solve this solution. Thank you beforehand.
[1]: https://i.stack.imgur.com/fAVNb.png
</details>
# 答案1
**得分**: 1
我认为问题在于你的 `us_geo_data` 只是一个数据框,而不是一个 `sf` 对象。我们可以通过使用 `right_join()` 而不是 `left_join()` 来解决这个问题:
```r
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} 的最新 dev 版本中工作。你可以尝试调整 to
中的值或限制以更好地定位插图地图。
英文:
I think the issue here is that your us_geo_data
is only a data frame not an sf
object. We can fix this using a right_join()
instead of a 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"))
Then plotting:
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)
which gives:
Note that, this only worked using the most recent dev version of {ggmagnify} installed from GitHub. You can play around with the values in to
or the limits to position the inset map a bit better.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论