添加图例到ggplot地图

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

Adding a legend to a ggplot map

问题

我正在使用ggplot2来可视化与地图相关的数据。我已根据连续值对区域进行了着色,并希望添加一个带有颜色和区域名称的图例。我的数据有点复杂,不太方便共享,但我已经使用公共数据(ggplot2中的地图绘制)重新创建了这种情况。以下代码创建了包含的地图:

  1. library(ggplot2)
  2. library(sf)
  3. # 导入一个geojson或shapefile
  4. map <- read_sf("https://raw.githubusercontent.com/R-CoderDotCom/data/main/shapefile_spain/spain.geojson")
  5. ggplot(map) +
  6. geom_sf(color = "white", aes(fill = unemp_rate)) +
  7. geom_sf_text(aes(label = name), size = 2)

代替连续默认图例,我想要一个具有名称、数字和颜色的图例。基本上,一个图例显示数据的nameunemp_rate列,颜色与地图匹配(例如unemp_rate)。有点类似于第二张包含的图片的图例(但颜色不正确)。

name unemp_rate
"Andalucía" 18.68
"Aragón" 8.96
"Principado de Asturias" 11.36
"Islas Baleares" 9.29
"Islas Canarias" 17.76
"Cantabria" 8.17
"Castilla y León" 10.19
"Castilla-La Mancha" 14.11
"Cataluña" 9.29
"Comunidad Valenciana" 12.81
"Extremadura" 16.73
"Galicia" 11.20
"Comunidad de Madrid" 10.18
"Región de Murcia" 12.18
"Comunidad Foral de Navarra" 8.76
"País Vasco" 8.75
"La Rioja" 10.19
"Ceuta y Melilla" 23.71

实际代码如下:

  1. ggplot(map, aes(geometry = geometry, fill = Y1)) +
  2. theme_bw() + geom_sf(show.legend = FALSE) +
  3. scale_fill_gradient2(low = "brown", high = "green") +
  4. theme(
  5. axis.title = element_blank(),
  6. axis.text = element_blank(),
  7. axis.ticks = element_blank())
英文:

I am using ggplot2 to visualise map-related data. I have coloured regions according to a continuous value, and I would like to add a legend with colors and region names. My own data is a bit cumbersome to share, but I have recreated the scenario with public data (Mapping in ggplot2). The following code creates the included map:

  1. library(ggplot2)
  2. library(sf)
  3. # Import a geojson or shapefile
  4. map &lt;- read_sf(&quot;https://raw.githubusercontent.com/R-CoderDotCom/data/main/shapefile_spain/spain.geojson&quot;)
  5. ggplot(map) +
  6. geom_sf(color = &quot;white&quot;, aes(fill = unemp_rate)) +
  7. geom_sf_text(aes(label = name), size = 2)

添加图例到ggplot地图

Instead of the continuous default legend, I would like to have a legend with names, numbers and colors. Basically, a legend that shows the name and unemp_rate columns of the data with colors matching the map (eg. unemp_rate). Somewhat like the legend of the second included picture (but the colors are not right).

name unemp_rate
"Andalucía" 18.68
"Aragón" 8.96
"Principado de Asturias" 11.36
"Islas Baleares" 9.29
"Islas Canarias" 17.76
"Cantabria" 8.17
"Castilla y León" 10.19
"Castilla-La Mancha" 14.11
"Cataluña" 9.29
"Comunidad Valenciana" 12.81
"Extremadura" 16.73
"Galicia" 11.20
"Comunidad de Madrid" 10.18
"Región de Murcia" 12.18
"Comunidad Foral de Navarra" 8.76
"País Vasco" 8.75
"La Rioja" 10.19
"Ceuta y Melilla" 23.71

添加图例到ggplot地图

My actual code looks like so:

  1. ggplot(map, aes(geometry = geometry, fill = Y1)) +
  2. theme_bw() + geom_sf(show.legend = FALSE) +
  3. scale_fill_gradient2(low = &quot;brown&quot;, high = &quot;green&quot;) +
  4. theme(
  5. axis.title = element_blank(),
  6. axis.text = element_blank(),
  7. axis.ticks = element_blank())

答案1

得分: 2

也许可以考虑使用嵌套的条形图代替:

  1. library(ggplot2)
  2. library(sf)
  3. library(dplyr)
  4. library(patchwork)
  5. # 导入一个geojson或shapefile
  6. map_ <- read_sf("https://raw.githubusercontent.com/R-CoderDotCom/data/main/shapefile_spain/spain.geojson") %>%
  7. mutate(name = forcats::fct_reorder(name, desc(unemp_rate)))
  8. g1 <- map_ %>%
  9. ggplot() +
  10. geom_sf(color = "white", aes(fill = unemp_rate)) +
  11. geom_sf_text(aes(label = name), size = 2) +
  12. scale_fill_gradient2(low = "brown", high = "green", midpoint = 16) +
  13. theme_minimal() +
  14. theme(legend.position = "none", axis.text = element_blank(), axis.title = element_blank(), panel.grid = element_blank())
  15. g2 <- map_ %>%
  16. ggplot(aes(x = unemp_rate, y = name, fill = unemp_rate)) +
  17. geom_col() +
  18. scale_fill_gradient2(low = "brown", high = "green", midpoint = 16) +
  19. geom_text(aes(label = name, x = .5, hjust = 0)) +
  20. geom_text(aes(label = unemp_rate), nudge_x = - .5, hjust = 1) +
  21. theme_void() +
  22. theme(legend.position = "none")
  23. g1 + inset_element(g2, 0, .2, .9, 1)

添加图例到ggplot地图

  1. <details>
  2. <summary>英文:</summary>
  3. Perhaps an inset bar chart instead:

library(ggplot2)
library(sf)
library(dplyr)
library(patchwork)

Import a geojson or shapefile

map_ <- read_sf("https://raw.githubusercontent.com/R-CoderDotCom/data/main/shapefile_spain/spain.geojson") %>%
mutate(name = forcats::fct_reorder(name, desc(unemp_rate)))

g1 <- map_ %>%
ggplot() +
geom_sf(color = "white", aes(fill = unemp_rate)) +
geom_sf_text(aes(label = name), size = 2) +
scale_fill_gradient2(low = "brown", high = "green", midpoint = 16) +
theme_minimal() +
theme(legend.position = "none", axis.text = element_blank(), axis.title = element_blank(), panel.grid = element_blank())

g2 <- map_ %>%
ggplot(aes(x = unemp_rate, y = name, fill = unemp_rate)) +
geom_col() +
scale_fill_gradient2(low = "brown", high = "green", midpoint = 16) +
geom_text(aes(label = name, x = .5, hjust = 0)) +
geom_text(aes(label = unemp_rate), nudge_x = - .5, hjust = 1) +
theme_void() +
theme(legend.position = "none")

g1 + inset_element(g2, 0, .2, .9, 1)

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/ZeQN2.png
  3. </details>
  4. # 答案2
  5. **得分**: 1
  6. 这有点复杂,因为这是一种非常非标准的制作图例的方式,但使用`scales`包的帮助并不太难。您可以首先创建自己的颜色比例尺,例如:
  7. ```R
  8. library(scales)
  9. my_pal <- seq_gradient_pal(low = 'brown', high = 'green')
  10. my_scale <- col_numeric(palette = my_pal, domain = range(map$unemp_rate))
  11. my_cols <- setNames(my_scale(map$unemp_rate), map$name)

然后使用以下代码创建图表:

  1. ggplot(map) +
  2. geom_sf(color = "white", aes(fill = name)) +
  3. geom_sf_text(aes(label = name), size = 2) +
  4. scale_fill_manual(
  5. values = my_cols,
  6. name = "失业率",
  7. labels = setNames(paste(map$name, map$unemp_rate, sep = ': '), map$name)
  8. )

添加图例到ggplot地图

英文:

This is a bit involved, since this is a pretty non-standard way of making a legend, but not too bad with help from the scales package. You can make your own color scale first, e.g. like so:

  1. library(scales)
  2. my_pal &lt;- seq_gradient_pal(low = &#39;brown&#39;, high = &#39;green&#39;)
  3. my_scale &lt;- col_numeric(palette = my_pal, domain = range(map$unemp_rate))
  4. my_cols &lt;- setNames(my_scale(map$unemp_rate), map$name)
  5. ggplot(map) +
  6. geom_sf(color = &quot;white&quot;, aes(fill = name)) +
  7. geom_sf_text(aes(label = name), size = 2) +
  8. scale_fill_manual(
  9. values = my_cols,
  10. name = &quot;Unemployment rate&quot;,
  11. labels = setNames(paste(map$name, map$unemp_rate, sep = &#39;: &#39;), map$name)
  12. )

添加图例到ggplot地图

答案3

得分: 0

你需要创建一个字段,将名称和失业率合并,并将其用作填充变量。另外,我不确定关于“颜色不对”的部分,但也许你想要一个连续的颜色比例尺?所以我正在使用viridis颜色调色板来获取类似的连续比例尺,但适用于分类数据。

  1. map |&gt;
  2. mutate(fill_var = paste0(name, ' - ', unemp_rate)) |&gt;
  3. ggplot() +
  4. geom_sf(color = "white", aes(fill = fill_var)) +
  5. geom_sf_text(aes(label = name), size = 2) +
  6. ggthemes::theme_map() + # optional
  7. scale_fill_viridis_d() +
  8. labs(fill = "")

添加图例到ggplot地图

  1. <details>
  2. <summary>英文:</summary>
  3. You need to create a field which combines the name and unemployment rate and use that for the fill variable. Also I am not sure about the &quot;colours are not right&quot; part, but maybe you want a continuous colour scale? So I am using viridis colour palette to get similar continuous scale but for categorical data.
  4. map |&gt;
  5. mutate(fill_var = paste0(name, &#39; - &#39;, unemp_rate)) |&gt;
  6. ggplot() +
  7. geom_sf(color = &quot;white&quot;, aes(fill = fill_var)) +
  8. geom_sf_text(aes(label = name), size = 2) +
  9. ggthemes::theme_map() + # optional
  10. scale_fill_viridis_d() +
  11. labs(fill = &quot;&quot;)
  12. [![sample_plot_output_with_legends][1]][1]
  13. [1]: https://i.stack.imgur.com/j6VOf.png
  14. </details>

huangapple
  • 本文由 发表于 2023年6月2日 01:12:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384287.html
匿名

发表评论

匿名网友

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

确定