英文:
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() +
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))
如何将图例放在底部,将图例的标题放在图例的顶部并保持连续的颜色调色板?
英文:
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 <- 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() +
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))
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)
#> Warning: package 'ggplot2' was built under R version 4.2.3
#> Warning: package 'tibble' was built under R version 4.2.3
#> Warning: package 'dplyr' was built under R version 4.2.3
library(geobr)
#> Warning: package 'geobr' was built under R version 4.2.3
#> Loading required namespace: sf
library(viridis)
#> Loading required package: viridisLite
mun <- geobr::read_municipality(showProgress = FALSE, simplified = TRUE) |>
mutate(code_state = as.numeric(code_state))
#> Using year 2010
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_colorbar(
title.position = "top",
title.hjust = 0.5
),
color = guide_colorbar(
title.position = "top",
title.hjust = 0.5
)
)
<!-- -->
英文:
The default legend for a continuous fill or color scale is guide="colorbar"
, i.e. use guide_colorbar
instead of guide_legend
:
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.2.3
#> Warning: package 'tibble' was built under R version 4.2.3
#> Warning: package 'dplyr' was built under R version 4.2.3
library(geobr)
#> Warning: package 'geobr' was built under R version 4.2.3
#> Loading required namespace: sf
library(viridis)
#> Loading required package: viridisLite
mun <- geobr::read_municipality(showProgress = FALSE, simplified = TRUE) |>
mutate(code_state = as.numeric(code_state))
#> Using year 2010
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_colorbar(
title.position = "top",
title.hjust = 0.5
),
color = guide_colorbar(
title.position = "top",
title.hjust = 0.5
)
)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论