R ggplot:分组变量图例中的分组和标签的大小

xdnvmnnf  于 2023-10-13  发布在  其他
关注(0)|答案(2)|浏览(137)

我想在我的图的底部一个装箱变量discrete_var的图例。把垃圾桶放在一排。问题是这样做会导致不同大小的箱子和标签彼此太近。我如何设置箱子的大小,或者旋转标签以放置在图例箱子下面并垂直于图例箱子(这可能会起作用)?

shp |>
  ggplot() +
  geom_sf(aes(fill = discrete_var)) +
  scale_fill_brewer(palette = 'Purples', direction = -1, na.value="grey", drop=FALSE) +
  coord_sf(expand = FALSE) +
  theme(legend.position = "bottom") +
  guides(fill=guide_legend(nrow=1,byrow=TRUE, label.position = "bottom"))

这就是我想要的,但在图的底部:

41zrol4v

41zrol4v1#

使用legend.key.width = unit(2, "cm")的一种方法是加宽图例键宽度:

library(tidyverse)
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE

map <- map_data("world")

mapcols <- map |>
  count(region) |>
  mutate(
    discrete_var = cut(40 * n / max(n), breaks = seq(0, 40, 5)),
    discrete_var = gsub("\\((\\d+),(\\d+)\\]", "\\1 to \\2", discrete_var)
  )

shp <-   st_as_sf(maps::map('world', plot = FALSE, fill = TRUE))

shp |> 
  left_join(mapcols, by = join_by(ID == region)) |> 
  ggplot() +
  geom_sf(aes(fill = discrete_var)) +
  scale_fill_brewer(palette = 'Purples', direction = -1, na.value="grey", drop=FALSE) +
  coord_sf(expand = FALSE) +
  theme(legend.position = "bottom", legend.key.width = unit(2, "cm")) +
  guides(fill=guide_legend(nrow=1,byrow=TRUE, label.position = "bottom"))

或者使用legend.text = element_text(angle = 45, vjust = 0.5)将文本放置在一个Angular :

shp |> 
  left_join(mapcols, by = join_by(ID == region)) |> 
  ggplot() +
  geom_sf(aes(fill = discrete_var)) +
  scale_fill_brewer(palette = 'Purples', direction = -1, na.value="grey", drop=FALSE) +
  coord_sf(expand = FALSE) +
  theme(legend.position = "bottom", legend.text = element_text(angle = 45, vjust = 0.5)) +
  guides(fill=guide_legend(nrow=1,byrow=TRUE, label.position = "bottom"))

最后,为了使其垂直,将最后一行替换为theme(legend.position = "bottom", legend.direction = "vertical")

shp |> 
  left_join(mapcols, by = join_by(ID == region)) |> 
  ggplot() +
  geom_sf(aes(fill = discrete_var)) +
  scale_fill_brewer(palette = 'Purples', direction = -1, na.value="grey", drop=FALSE) +
  coord_sf(expand = FALSE) +
  theme(legend.position = "bottom", legend.direction = "vertical")

watbbzwu

watbbzwu2#

另一种选择是在guide内部增加keywidth

shp |>
  ggplot() +
  geom_sf(aes(fill = discrete_var)) +
  scale_fill_brewer(palette = 'Purples', direction = -1, 
                    na.value = "grey", drop = FALSE) +
  coord_sf(expand = FALSE) +
  theme(legend.position = "bottom") +
  guides(fill = guide_legend(nrow = 1, byrow = TRUE, label.position = "bottom",
                             keywidth = unit(2, 'cm')))

使用数据

library(rnaturalearth)
library(sf)
library(tidyverse)

set.seed(1)

shp <- ne_countries(returnclass = 'sf') %>%
  mutate(discrete_var = cut(runif(n(), -6, 17),
                            breaks = c(-6, -3, 0, 3, 6, 8, 10, 12, 14, 17),
                            labels = c('< -3', '-3 to 0', '0 to 3',
                                       '3 to 6', '6 to 8', '8 to 10',
                                       '10 to 12', '12 to 14', '>14'))) %>%
  mutate(discrete_var = if_else(name %in% c('Antarctica', 'Greenland'), NA, 
                               discrete_var))

相关问题