如何使用ggmagnify和ggplot2制作地图图表的放大插图。

huangapple go评论205阅读模式
英文:

How to make a magnified inset of a map plot with ggmagnify and ggplot2

问题

以下是已翻译的内容:

  1. 对于过去一周的```tidytuesday```挑战之一,即分析```Tornado```数据集的挑战,我尝试使用```ggplot2```中的```ggmagnify```包创建了一个缩放的德克萨斯地图与美国地图的插图地图。所以,让我写下代码来演示我迄今为止所实现的内容,接下来将是我提出的问题。
  2. ```R
  3. library(tidyverse)
  4. library(sf)
  5. library(rnaturalearth)
  6. library(rnaturalearthdata)
  7. library(bertin)
  8. library(usmap)
  9. library(maps)
  10. library(showtext)
  11. library(showtextdb)
  12. library(ggtext)
  13. library(cowplot)
  14. library(ggmagnify)
  15. tornados <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-05-16/tornados.csv')
  16. state_names_df <- tibble(state = state.name) %>%
  17. bind_cols(tibble(abb = state.abb)) %>%
  18. bind_rows(tibble(state = "District Of Columbia", abb = "DC")) %>%
  19. as.data.frame()
  20. maps_states_df <- st_as_sf(map("state", plot = FALSE, fill = TRUE)) %>%
  21. mutate(ID = str_to_title(ID))
  22. us_geo_data <- state_names_df %>%
  23. left_join(maps_states_df, by = c("state" = "ID"))
  24. tornados_across_us <- ggplot(us_geo_data) +
  25. geom_sf(aes(geometry = geom), linewidth = 0.1) +
  26. 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) +
  27. ggthemes::theme_map()

这段代码生成了以下地图:

如何使用ggmagnify和ggplot2制作地图图表的放大插图。

现在,正如我上面提到的,我想要实现的是使用ggmagnify包绘制德克萨斯的插图地图。以下是我尝试这样做的代码。

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

然而,这会引发一个错误,说道:

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

我无法找到解决此问题的方法,因此,我想提前感谢您提供的任何帮助来解决此问题。谢谢!

  1. <details>
  2. <summary>英文:</summary>
  3. For one of the past week&#39;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()

  1. This code produced the following map:
  2. [![enter image description here][1]][1]
  3. 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))

  1. 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()).

  1. 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.
  2. [1]: https://i.stack.imgur.com/fAVNb.png
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. 我认为问题在于你的 `us_geo_data` 只是一个数据框,而不是一个 `sf` 对象。我们可以通过使用 `right_join()` 而不是 `left_join()` 来解决这个问题:
  7. ```r
  8. tornados <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-05-16/tornados.csv')
  9. state_names_df <- tibble(state = state.name) %>%
  10. bind_cols(tibble(abb = state.abb)) %>%
  11. bind_rows(tibble(state = "District Of Columbia", abb = "DC")) %>%
  12. as.data.frame()
  13. maps_states_df <- st_as_sf(map("state", plot = FALSE, fill = TRUE)) %>%
  14. mutate(ID = str_to_title(ID))
  15. us_geo_data <- right_join(maps_states_df, state_names_df, by = c("ID" = "state"))

然后绘图:

  1. tornados_across_us <- ggplot(us_geo_data) +
  2. geom_sf(linewidth = 0.1) +
  3. geom_point(data = filter(tornados, !st %in% c("VI", "DC", "AK", "PR", "HI")) ,
  4. aes(x = slon, y = slat), size = 0.2, color = "darkgreen", alpha = 0.2) +
  5. ggthemes::theme_map()
  6. tornados_across_us
  7. tornados_across_us + geom_magnify(aes(from = abb == "TX"),
  8. to = c(-125, -98, 10, 30),
  9. shadow = TRUE, linewidth = 1, colour = "orange2",
  10. shape = "outline",
  11. aspect = "fixed",
  12. expand = 0)

效果如下:

如何使用ggmagnify和ggplot2制作地图图表的放大插图。

请注意,这只能在从 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():

  1. tornados &lt;- readr::read_csv(&#39;https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-05-16/tornados.csv&#39;)
  2. state_names_df &lt;- tibble(state = state.name) %&gt;%
  3. bind_cols(tibble(abb = state.abb)) %&gt;%
  4. bind_rows(tibble(state = &quot;District Of Columbia&quot;, abb = &quot;DC&quot;)) %&gt;%
  5. as.data.frame()
  6. maps_states_df &lt;- st_as_sf(map(&quot;state&quot;, plot = FALSE, fill = TRUE)) %&gt;%
  7. mutate(ID = str_to_title(ID))
  8. us_geo_data &lt;- right_join(maps_states_df, state_names_df, by = c(&quot;ID&quot; = &quot;state&quot;))

Then plotting:

  1. tornados_across_us &lt;- ggplot(us_geo_data) +
  2. geom_sf(linewidth = 0.1) +
  3. geom_point(data = filter(tornados, !st %in% c(&quot;VI&quot;, &quot;DC&quot;, &quot;AK&quot;, &quot;PR&quot;, &quot;HI&quot;)) ,
  4. aes(x = slon, y = slat), size = 0.2, color = &quot;darkgreen&quot;, alpha = 0.2) +
  5. ggthemes::theme_map()
  6. tornados_across_us
  7. tornados_across_us + geom_magnify(aes(from = abb == &quot;TX&quot;),
  8. to = c(-125, -98, 10, 30),
  9. shadow = TRUE, linewidth = 1, colour = &quot;orange2&quot;,
  10. shape = &quot;outline&quot;,
  11. aspect = &quot;fixed&quot;,
  12. expand = 0)

which gives:

如何使用ggmagnify和ggplot2制作地图图表的放大插图。

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.

huangapple
  • 本文由 发表于 2023年5月29日 21:41:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定