gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:05:47 25 4
gpt4 key购买 nike

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

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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com