将 ggplot 图例与不同的标签结合起来

Combine ggplot legends with varying labels

我们想创建一个 graph/legend,它使用形状对一个因素进行编码,使用颜色对另一个因素进行编码,并使图例像 table,一个因素的列和另一个因素的行。诀窍是,两列中的图例标签必须不同,就像这个模型:

这是测试数据和一次失败的尝试。因子“fromto”代表列; “匹配”行,链接应该具有相同颜色的两个元素。

library(ggplot2)
library(sf)

# Map limits
XLIM <- c( -112.2, -104 )
YLIM <- c( 33.8, 39.8 )

# DUMMY DATA
DAT <- "fromto match label lat long
Procurement flaghi 1-2 39.73921 -103.99030
Procurement flaglo 2-3 39.06831 -107.56443
Procurement pine 3-4 35.09633 -105.64020
Procurement taos 4-5 35.19934 -110.65155
Procurement crip 5-6 38.57335 -110.54645
Deployment flaghi 6-7 39.73921 -104.99030
Deployment flaglo 7-8 39.06831 -108.56443
Deployment pine 8-9 35.09633 -106.64020
Deployment taos 9-10 35.19934 -111.65155
Deployment crip 10-11 38.57335 -109.54645"

DAT <- read.table( text=DAT, header=TRUE, stringsAsFactors=FALSE )
DAT <- st_as_sf(x = DAT, 
                coords = c("long", "lat"),
                crs = 4326 )
DAT$fromto <- factor(DAT$fromto)
DAT$match <- factor(DAT$match)
DAT$label <- factor(DAT$label)

p <- ggplot() +

geom_sf( DAT, mapping=aes( shape=fromto, color=match ), size=3 ) +
  scale_shape_manual( values=c(16,3), name=NULL ) +
  scale_color_manual( values=c("red", "orange", "blue", "black", "green"),
                      labels=DAT$label, name=NULL ) +
  coord_sf( xlim=XLIM, ylim=YLIM, crs=26912, default_crs=4326 )
p

使用ggnewscale你可以试试...

library(ggplot2)
library(sf)
library(ggnewscale)

# Map limits
XLIM <- c( -112.2, -104 )
YLIM <- c( 33.8, 39.8 )

# DUMMY DATA
DAT <- "fromto match label lat long
Procurement flaghi 1-2 39.73921 -103.99030
Procurement flaglo 2-3 39.06831 -107.56443
Procurement pine 3-4 35.09633 -105.64020
Procurement taos 4-5 35.19934 -110.65155
Procurement crip 5-6 38.57335 -110.54645
Deployment flaghi 6-7 39.73921 -104.99030
Deployment flaglo 7-8 39.06831 -108.56443
Deployment pine 8-9 35.09633 -106.64020
Deployment taos 9-10 35.19934 -111.65155
Deployment crip 10-11 38.57335 -109.54645"

DAT <- read.table(text = DAT, header = TRUE)
DAT <- st_as_sf(x = DAT, 
                coords = c("long", "lat"),
                crs = 4326 )

# split data for to enable dual legend based on colour
dat_p <- DAT[DAT$fromto == "Procurement", ]
dat_d <- DAT[DAT$fromto == "Deployment", ]

ggplot() +
  geom_sf(dat_p, mapping = aes(color = match), shape = 3 , size=3 ) +
  scale_color_manual(values = c("red", "orange", "blue", "black", "green"),
                     labels = dat_p$label,
                     name = "Procurement" ) +
  new_scale_colour()+
  geom_sf(dat_d, mapping = aes(color = match), size=3 ) +
  scale_color_manual( values = c("red", "orange", "blue", "black", "green"),
                      labels = dat_d$label,
                      name = "Deployment" ) +
  coord_sf(xlim = XLIM, 
           ylim = YLIM, 
           crs = 26912,
           default_crs = 4326) +
  theme(legend.direction = "vertical",
        legend.box = "horizontal",
        legend.position = c(1.025, 0.55),
        legend.justification = c(0, 1))

reprex package (v2.0.1)

于 2021-12-24 创建