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

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

Guide legend title position on mapping color and fill in ggplot

问题

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

这里是一个示例:

library(tidyverse)
library(geobr)
library(viridis)

mun <- geobr::read_municipality(showProgress = FALSE, simplified = TRUE) %>%
       mutate(code_state = as.numeric(code_state))
ggplot() + 
    geom_sf(data = mun,
            mapping = aes(geometry = geom,
                          fill = code_state,
                          colour = code_state)) +
    viridis::scale_fill_viridis(name = "Some title") +
    viridis::scale_color_viridis() +  
    theme_bw() +  
    theme(legend.position = "bottom") 

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

ggplot() + 
    geom_sf(data = mun,
            mapping = aes(geometry = geom,
                          fill = code_state,
                          colour = code_state)) +
    viridis::scale_fill_viridis(name = "Some title") +
    viridis::scale_color_viridis() +  
    theme_bw() +  
    theme(legend.position = "bottom") +
    guides(fill = guide_legend(title.position = "top",
                                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:

library(tidyverse)
library(geobr)
library(viridis)

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

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

ggplot() + 
    geom_sf(data = mun,
            mapping = aes(geometry = geom,
                          fill = code_state,
                          colour = code_state)) +
    viridis::scale_fill_viridis(name = &quot;Some title&quot;) +
    viridis::scale_color_viridis() +  
    theme_bw() +  
    theme(legend.position = &quot;bottom&quot;) +
    guides(fill = guide_legend(title.position = &quot;top&quot;,
                                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

library(tidyverse)
#&gt; Warning: package &#39;ggplot2&#39; was built under R version 4.2.3
#&gt; Warning: package &#39;tibble&#39; was built under R version 4.2.3
#&gt; Warning: package &#39;dplyr&#39; was built under R version 4.2.3
library(geobr)
#&gt; Warning: package &#39;geobr&#39; was built under R version 4.2.3
#&gt; Loading required namespace: sf
library(viridis)
#&gt; Loading required package: viridisLite

mun &lt;- geobr::read_municipality(showProgress = FALSE, simplified = TRUE) |&gt;
  mutate(code_state = as.numeric(code_state))
#&gt; Using year 2010

ggplot() +
  geom_sf(
    data = mun,
    mapping = aes(
      geometry = geom,
      fill = code_state,
      colour = code_state
    )
  ) +
  viridis::scale_fill_viridis(name = &quot;Some title&quot;) +
  viridis::scale_color_viridis() +
  theme_bw() +
  theme(legend.position = &quot;bottom&quot;) +
  guides(fill = guide_colorbar(
    title.position = &quot;top&quot;,
    title.hjust = 0.5
  ),
  color = guide_colorbar(
    title.position = &quot;top&quot;,
    title.hjust = 0.5
  )
  )

在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:

library(tidyverse)
#&gt; Warning: package &#39;ggplot2&#39; was built under R version 4.2.3
#&gt; Warning: package &#39;tibble&#39; was built under R version 4.2.3
#&gt; Warning: package &#39;dplyr&#39; was built under R version 4.2.3
library(geobr)
#&gt; Warning: package &#39;geobr&#39; was built under R version 4.2.3
#&gt; Loading required namespace: sf
library(viridis)
#&gt; Loading required package: viridisLite

mun &lt;- geobr::read_municipality(showProgress = FALSE, simplified = TRUE) |&gt;
  mutate(code_state = as.numeric(code_state))
#&gt; Using year 2010

ggplot() +
  geom_sf(
    data = mun,
    mapping = aes(
      geometry = geom,
      fill = code_state,
      colour = code_state
    )
  ) +
  viridis::scale_fill_viridis(name = &quot;Some title&quot;) +
  viridis::scale_color_viridis() +
  theme_bw() +
  theme(legend.position = &quot;bottom&quot;) +
  guides(fill = guide_colorbar(
    title.position = &quot;top&quot;,
    title.hjust = 0.5
  ),
  color = guide_colorbar(
    title.position = &quot;top&quot;,
    title.hjust = 0.5
  )
  )

在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:

确定