r - ggplot2 在多个图层中定义大小时的多个图例

标签 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()
enter image description here
我的问题是传说。我想要:
  • 一个名为 collabs 的图例,它具有线条粗细和线条不透明度
  • 一个名为papers的图例,其大小为

  • 相反,我在一个图例中有论文和协作(线条大小和点大小),在一秒钟内有不透明度。我认为问题是因为我在 aes 中使用了 size两者都适用 geom_curvegeom_point ?
    例如。我想要这样的东西(在inkscape中编辑)
    enter image description here
    任何建议将不胜感激!

    最佳答案

    这可以通过 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()
    

    关于r - ggplot2 在多个图层中定义大小时的多个图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69519654/

    相关文章:

    html - 解析xml属性: strange Encoding issue

    r - 按两列值的范围查找数据框中的匹配区间

    r - scale_continuous 中的格式化程序参数在 R 2.15 中抛出错误

    r - ggplot2中同一图例中的不同图例键

    R:通过具有重叠容差的标准删除间隔

    c# - 从 H2O 模型生成 C#

    r - install_extras 不起作用?

    r - 在 ggplot 中绘制多个分组变量数据集

    r - 为 ggplot : How to avoid overriding `theme()` settings when adding external themes 编写自定义函数

    r - 在单页上放置多个 ggplot 图时日期标签重叠