在ggplot中指导图例标题的位置以及颜色和填充的映射。

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

Guide legend title position on mapping color and fill in ggplot

问题

我正在尝试调整颜色和填充映射的图例(以及它们的标题)的位置。但连续的图例看起来像离散的。

这里是一个示例:

  1. library(tidyverse)
  2. library(geobr)
  3. library(viridis)
  4. mun <- geobr::read_municipality(showProgress = FALSE, simplified = TRUE) %>%
  5. mutate(code_state = as.numeric(code_state))
  1. ggplot() +
  2. geom_sf(data = mun,
  3. mapping = aes(geometry = geom,
  4. fill = code_state,
  5. colour = code_state)) +
  6. viridis::scale_fill_viridis(name = "Some title") +
  7. viridis::scale_color_viridis() +
  8. theme_bw() +
  9. theme(legend.position = "bottom")

在ggplot中指导图例标题的位置以及颜色和填充的映射。

  1. ggplot() +
  2. geom_sf(data = mun,
  3. mapping = aes(geometry = geom,
  4. fill = code_state,
  5. colour = code_state)) +
  6. viridis::scale_fill_viridis(name = "Some title") +
  7. viridis::scale_color_viridis() +
  8. theme_bw() +
  9. theme(legend.position = "bottom") +
  10. guides(fill = guide_legend(title.position = "top",
  11. title.hjust = 0.5))

在ggplot中指导图例标题的位置以及颜色和填充的映射。

如何将图例放在底部,将图例的标题放在图例的顶部并保持连续的颜色调色板?

英文:

I'am trying to position the legends (and their titles) of colors and fill mappings. But the continuous legend turns into what looks like a discrete one.

Here is a toy example:

  1. library(tidyverse)
  2. library(geobr)
  3. library(viridis)
  4. mun &lt;- geobr::read_municipality(showProgress = FALSE, simplified = TRUE) |&gt;
  5. mutate(code_state = as.numeric(code_state))
  1. ggplot() +
  2. geom_sf(data = mun,
  3. mapping = aes(geometry = geom,
  4. fill = code_state,
  5. colour = code_state)) +
  6. viridis::scale_fill_viridis(name = &quot;Some title&quot;) +
  7. viridis::scale_color_viridis() +
  8. theme_bw() +
  9. theme(legend.position = &quot;bottom&quot;)

在ggplot中指导图例标题的位置以及颜色和填充的映射。

  1. ggplot() +
  2. geom_sf(data = mun,
  3. mapping = aes(geometry = geom,
  4. fill = code_state,
  5. colour = code_state)) +
  6. viridis::scale_fill_viridis(name = &quot;Some title&quot;) +
  7. viridis::scale_color_viridis() +
  8. theme_bw() +
  9. theme(legend.position = &quot;bottom&quot;) +
  10. guides(fill = guide_legend(title.position = &quot;top&quot;,
  11. title.hjust = 0.5))

在ggplot中指导图例标题的位置以及颜色和填充的映射。

How to put the legend on the bottom, the title of the legend on the top of the legend and keep the continuous color pallete?

答案1

得分: 2

连续填充或颜色比例尺的默认图例是 guide="colorbar",即使用 guide_colorbar 而不是 guide_legend

  1. library(tidyverse)
  2. #&gt; Warning: package &#39;ggplot2&#39; was built under R version 4.2.3
  3. #&gt; Warning: package &#39;tibble&#39; was built under R version 4.2.3
  4. #&gt; Warning: package &#39;dplyr&#39; was built under R version 4.2.3
  5. library(geobr)
  6. #&gt; Warning: package &#39;geobr&#39; was built under R version 4.2.3
  7. #&gt; Loading required namespace: sf
  8. library(viridis)
  9. #&gt; Loading required package: viridisLite
  10. mun &lt;- geobr::read_municipality(showProgress = FALSE, simplified = TRUE) |&gt;
  11. mutate(code_state = as.numeric(code_state))
  12. #&gt; Using year 2010
  13. ggplot() +
  14. geom_sf(
  15. data = mun,
  16. mapping = aes(
  17. geometry = geom,
  18. fill = code_state,
  19. colour = code_state
  20. )
  21. ) +
  22. viridis::scale_fill_viridis(name = &quot;Some title&quot;) +
  23. viridis::scale_color_viridis() +
  24. theme_bw() +
  25. theme(legend.position = &quot;bottom&quot;) +
  26. guides(fill = guide_colorbar(
  27. title.position = &quot;top&quot;,
  28. title.hjust = 0.5
  29. ),
  30. color = guide_colorbar(
  31. title.position = &quot;top&quot;,
  32. title.hjust = 0.5
  33. )
  34. )

在ggplot中指导图例标题的位置以及颜色和填充的映射。<!-- -->

英文:

The default legend for a continuous fill or color scale is guide=&quot;colorbar&quot;, i.e. use guide_colorbar instead of guide_legend:

  1. library(tidyverse)
  2. #&gt; Warning: package &#39;ggplot2&#39; was built under R version 4.2.3
  3. #&gt; Warning: package &#39;tibble&#39; was built under R version 4.2.3
  4. #&gt; Warning: package &#39;dplyr&#39; was built under R version 4.2.3
  5. library(geobr)
  6. #&gt; Warning: package &#39;geobr&#39; was built under R version 4.2.3
  7. #&gt; Loading required namespace: sf
  8. library(viridis)
  9. #&gt; Loading required package: viridisLite
  10. mun &lt;- geobr::read_municipality(showProgress = FALSE, simplified = TRUE) |&gt;
  11. mutate(code_state = as.numeric(code_state))
  12. #&gt; Using year 2010
  13. ggplot() +
  14. geom_sf(
  15. data = mun,
  16. mapping = aes(
  17. geometry = geom,
  18. fill = code_state,
  19. colour = code_state
  20. )
  21. ) +
  22. viridis::scale_fill_viridis(name = &quot;Some title&quot;) +
  23. viridis::scale_color_viridis() +
  24. theme_bw() +
  25. theme(legend.position = &quot;bottom&quot;) +
  26. guides(fill = guide_colorbar(
  27. title.position = &quot;top&quot;,
  28. title.hjust = 0.5
  29. ),
  30. color = guide_colorbar(
  31. title.position = &quot;top&quot;,
  32. title.hjust = 0.5
  33. )
  34. )

在ggplot中指导图例标题的位置以及颜色和填充的映射。<!-- -->

huangapple
  • 本文由 发表于 2023年7月17日 21:28:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704960.html
匿名

发表评论

匿名网友

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

确定