修改ggplot2中每个图例图标的大小

问题描述 投票:0回答:1

我正在使用 ggplot/usmap 库将高度倾斜的数据绘制到地图上。

由于数据如此倾斜,我创建了不均匀的区间括号。见下文。

我的代码:

library(dplyr)
library(tidyverse)
library(usmap)
library(ggplot2)
library(readxl)
library(rgdal)

plot_usmap(regions = "states",
           # fill = 'orange',
           labels = TRUE) +
  
  geom_point(data = grant_sh,
             size = 5,
             aes(x = x,
                 y = y,
                 color = funding_cat)) +

  theme(
    legend.title = element_text(size = 16),
    #change legend title font size
    legend.text = element_text(size = 14),
    #change legend text font size
    legend.position = 'left',
    plot.title = element_text(size = 22),
    plot.subtitle = element_text(size = 16)
  )  + #+
  scale_color_manual(
    values = c('#D4148C',  # pink muesaum
               '#049CFC',   #library,blue
               '#1C8474',
               '#7703fC'),
    name = "Map Key",
    labels = c(
      '$1,500 - $4,000 (n = 7)',
      '$4,001 - $6,000 (n = 12)',
      '$6,001 - $20,000 (n = 6)',
      '$20,001 - $40,000 (n = 25)'
    )
  ) +
  guides(colour = guide_legend(override.aes = list(size = 3)))

电流输出:

enter image description here

所需的输出:我想调整图例键以反映每个间隔的大小。因此,例如 1500-400 将是最小的图标,20,001-40,000 将是最大的。

我想这样做,以便观众立即知道间隔不均匀。任何实现这一结果的解决方案都将受到高度赞赏!

在下面的示例中查看每个间隔旁边的符号/椭圆如何表示间隔的范围。

enter image description here

r ggplot2 usmap
1个回答
1
投票

创建这种图例的一个选择是将其作为第二个图并使用例如将其粘合到主图上。

patchwork

注意:特别是使用地图作为主图和导出大小(如果有)时,此方法需要一些调整来定位图例,例如在我下面的代码中,在拼凑设计中添加了一个辅助行,以将图例向上移动。

更新:更新代码以在标签中包含计数。添加了第二种方法来使用

geom_col
和单独的数据框制作图例。

library(dplyr, warn = FALSE)
library(usmap)
library(ggplot2)
library(patchwork)

# Make example data
set.seed(123)

cat1 <- c(1500, 4001, 6001, 20001)
cat2 <- c(4000, 6000, 2000, 40000)
n = c(7, 12, 6, 25)
funding_cat <- paste0("$", cat1, " - $", cat2, " (n=", n, ")")
funding_cat <- factor(funding_cat, levels = rev(funding_cat))

grant_sh <- utils::read.csv(system.file("extdata", "us_states_centroids.csv", package = "usmapdata"))
grant_sh$funding_cat = sample(funding_cat, 51, replace = TRUE, prob = n / sum(n))

# Make legend plot
grant_sh_legend <- data.frame(
  funding_cat = funding_cat,
  n = c(7, 12, 6, 25)
)

legend <- ggplot(grant_sh, aes(y = funding_cat, fill = funding_cat)) +
  geom_bar(width = .6) +
  scale_y_discrete(position = "right") +
  scale_fill_manual(
    values = c('#D4148C',
               '#049CFC',
               '#1C8474',
               '#7703fC')
  ) +
  theme_void() +
  theme(axis.text.y = element_text(hjust = 0),
        plot.title = element_text(size = rel(1))) +
  guides(fill = "none") +
  labs(title = "Map Key")

map <- plot_usmap(regions = "states",
                  labels = TRUE) +
  geom_point(data = grant_sh,
             size = 5,
             aes(x = x,
                 y = y,
                 color = funding_cat)) +
  theme(
    legend.position = 'none',
    plot.title = element_text(size = 22),
    plot.subtitle = element_text(size = 16)
  )  + #+
  scale_color_manual(
    values = c('#D4148C',  # pink muesaum
               '#049CFC',   #library,blue
               '#1C8474',
               '#7703fC'),
    name = "Map Key",
    labels = c(
      '$1,500 - $4,000 (n = 7)',
      '$4,001 - $6,000 (n = 12)',
      '$6,001 - $20,000 (n = 6)',
      '$20,001 - $40,000 (n = 25)'
    )
  ) +
  guides(colour = guide_legend(override.aes = list(size = 3)))

# Glue together
design <- "
#B
AB
#B
"

legend + map + plot_layout(design = design, heights = c(5, 1, 1), widths = c(1, 10))

使用

geom_bar
计数是根据您的数据集
grant_sh
计算的。第二个选项是手动计算计数或使用手动创建的数据框,然后使用
geom_col
作为图例图:

grant_sh_legend <- data.frame(
  funding_cat = funding_cat,
  n = c(7, 12, 6, 25)
)

legend <- ggplot(grant_sh, aes(y = funding_cat, n = n, fill = funding_cat)) +
  geom_col(width = .6) +
  scale_y_discrete(position = "right") +
  scale_fill_manual(
    values = c('#D4148C',
               '#049CFC',
               '#1C8474',
               '#7703fC')
  ) +
  theme_void() +
  theme(axis.text.y = element_text(hjust = 0),
        plot.title = element_text(size = rel(1))) +
  guides(fill = "none") +
  labs(title = "Map Key")
© www.soinside.com 2019 - 2024. All rights reserved.