gpt4 book ai didi

r - 从两个数据框构建绘图时的两个图例

转载 作者:行者123 更新时间:2023-12-04 09:33:20 24 4
gpt4 key购买 nike

我有两个这样的数据框:

library(ggplot2)

set.seed(1)
x1 = rnorm(100)
y1 = rnorm(100)

x2 = rnorm(100)
y2 = rnorm(100)

df1 = data.frame(x=x1, y=y1, col1 = rep(1:4, each = 25))
df2 = data.frame(x=x2, y=y2, col2 = rep(5:8, each = 25))
我绘制了这个数据:
ggplot() +
geom_point(aes(x = x1, y = y1, colour = as.factor(col1)), data = df1, size = 1, shape = 19) +
geom_point(aes(x = x2, y = y2, colour = as.factor(col2)), data = df2, size = 2, shape = "\u2605")
结果是这样的:
enter image description here
如何为每个数据框获得两个单独的图例?
图例还应正确反射(reflect)所使用的形状,并让我为每个图例指定图例标题。
我知道 this solution存在但它很旧并且需要可以填充的符号,我想使用自定义的unicode符号。

最佳答案

这可以通过 ggnewscale 实现允许具有相同美学的额外比例和图例的包。尝试这个:

library(ggplot2)

set.seed(1)
x1 = rnorm(100)
y1 = rnorm(100)

x2 = rnorm(100)
y2 = rnorm(100)

df1 = data.frame(x=x1, y=y1, col1 = rep(1:4, each = 25))
df2 = data.frame(x=x2, y=y2, col2 = rep(5:8, each = 25))

ggplot() +
geom_point(aes(x = x1, y = y1, colour = as.factor(col1)), data = df1, size = 1, shape = 19) +
ggnewscale::new_scale_color() +
geom_point(aes(x = x2, y = y2, colour = as.factor(col2)), data = df2, size = 2, shape = "\u2605")

关于r - 从两个数据框构建绘图时的两个图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62718912/

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