当尺寸在多个图层中定义时,ggplot2 多个图例

fle*_*lee 4 r ggplot2

我正在绘制一个协作者网络,其中点大小根据一个国家/地区撰写的文章数量进行缩放,点之间的线代表协作,线宽和不透明度根据协作数量进行缩放。例如

library(tidyverse)

# data for lines
df_links <- structure(list(from = c("Argentina", "Argentina", "Canada",
                        "Austria", "Austria", "Italy", "Austria",
                        "Italy", "New Zealand"),
               to = c("Canada", "Germany", "Germany", "Italy",
                      "New Zealand", "New Zealand", "Panama",
                      "Panama", "Panama"), 
               collabs = c(1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L),
               x = c(-64, -64, -106, 15, 15, 13, 15, 13, 175),
               y = c(-38, -38, 56, 48, 48, 42, 48, 42, -41),
               x_end = c(-106, 10, 10, 13, 175, 175, -81, -81, -81),
               y_end = c(56, 51, 51, 42,-41, -41, 9, 9, 9)),
          row.names = c(NA, -9L),
          class = c("tbl_df", "tbl", "data.frame"))

# data for points 
df_points <- structure(list(name = c("Argentina", "Austria", "Australia", 
                        "Canada", "Germany", "France", "United Kingdom", 
                        "Italy", "New Zealand", "Panama", "Venezuela"),
               papers = c(1L, 1L, 1L, 22L, 3L, 2L, 1L, 1L, 2L, 1L, 1L),
               x = c(-64, 15, 134, -106, 10, 2, -3, 13, 175, -81, -67),
               y = c(-38, 48, -25, 56, 51, 46, 55, 42, -41, 9, 6)),
          row.names = c(NA, -11L),
          class = c("tbl_df", "tbl", "data.frame"))

#plot
ggplot() + 
  geom_curve(data = df_links,
             aes(x = x, y = y,
                 xend = x_end,
                 yend = y_end,
                 size = collabs,
                 alpha = collabs),
             curvature = 0.33) +
  geom_point(data = df_points,
             aes(x = x,
                 y = y,
                 size = papers),
             colour = "red") +
  coord_fixed(xlim = c(-150, 180), ylim = c(-55, 80)) +
  theme_void()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我的问题是传说。我想:

  1. 名为 collabs 的图例,具有线条粗细和线条不透明度
  2. 名为 Papers 的图例,大小为磅值

相反,我在一个图例中拥有论文和协作(线条大小和点大小),并且在一秒钟内具有不透明度。我认为问题是因为我aes在 和 中geom_curve都使用了内部尺寸geom_point

例如我想要这样的东西(在 inkscape 中编辑) 在此输入图像描述

任何建议将不胜感激!

ste*_*fan 5

ggnewscale这可以通过允许具有相同美感的多个比例和图例的包来实现:

library(tidyverse)
library(ggforce)
library(ggnewscale)

ggplot() + 
  geom_curve(data = df_links,
             aes(x = x, y = y,
                 xend = x_end,
                 yend = y_end,
                 size = collabs,
                 alpha = collabs),
             curvature = 0.33) +
  new_scale("size") +
  geom_point(data = df_points,
             aes(x = x,
                 y = y,
                 size = papers),
             colour = "red") +
  coord_fixed(xlim = c(-150, 180), ylim = c(-55, 80)) +
  theme_void()
Run Code Online (Sandbox Code Playgroud)