gpt4 book ai didi

r - geom_point 具有不同的填充和形状图例

转载 作者:行者123 更新时间:2023-12-02 21:15:01 25 4
gpt4 key购买 nike

嗯,也许是温度的原因,或者我再次没有看到明显的......

这是我的代码:

library(ggplot2)

p <- ggplot()
p <- p + geom_point(aes(x = 1, y=1,bg = "I", group = "B"),pch = 21, size = 20, color=NA)
p <- p + geom_point(aes(x = 1, y=1.125,bg = "I", group = "B" ),pch = 22, size = 20, color=NA)
p <- p + geom_point(aes(x = 0.75, y=1.125,bg = "II", group = "A" ),pch = 22, size = 20, color=NA)
p <- p + geom_point(aes(x = 0.85, y=1.125,bg = "III", group = "A" ),pch = 22, size = 20, color=NA)
p <- p + scale_fill_manual(values= c("darkred", "darkblue", "darkgreen"), guide=guide_legend(override.aes = list(shape = 23)))
#p <- p + scale_fill_manual(values= c("darkred", "darkblue", "darkgreen"), guide=guide_legend(inherit.aes = FALSE))
p <- p + scale_shape_manual(labels = c("circle", "rectangle"),values = c(21, 22))
p

我想要实现的基本上是两个图例,一个反射(reflect)颜色,在本示例中存在三种不同的颜色(“I”、“II”和“III”)以及两种不同类型的形状“矩形”和“圆形”,永远不会超过这两种不同的形状。

不幸的是,还有一些额外的限制...我无法使用美观的颜色,因为我还使用geom_segment以某种方式连接形状,这是第二个约束,我必须使用 ggplot2。

但是我无法制作这两个图例,感谢任何帮助......

最佳答案

为什么不将所有点存储在数据框中?它非常适合:

df <- data.frame(x = c(1, 1, 0.75, 0.85), 
y = c(1, 1.125, 1.125, 1.125),
nr = c("I", "I", "II", "III"),
sh = c("B", "B", "A", "A"))

现在您可以轻松查看所需的映射:

ggplot(df, aes(x, y, fill = nr, shape = sh)) + 
geom_point(size = 20, color = NA) +
scale_shape_manual(labels = c("circle", "rectangle"), values = c(21, 22),
guide = guide_legend(override.aes = list(colour = 1))) +
scale_fill_manual(values = c("darkred", "darkblue", "darkgreen"),
guide = guide_legend(override.aes = list(shape = 23)))

enter image description here

关于r - geom_point 具有不同的填充和形状图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31270126/

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