gpt4 book ai didi

r - ggplot2:当大小是​​映射美学时,什么控制图例中点的大小?

转载 作者:行者123 更新时间:2023-12-05 01:16:34 28 4
gpt4 key购买 nike

当您同时拥有线型和形状美感(分别使用 geom_path 和 geom_point)时,有时可能很难辨认出图例中的点,因为它们与线重叠。

这是一个工作示例。我有一个加权线性模型,想要绘制预测数据和观察数据。

iris$wts <- runif(nrow(iris), min = 0.2, max = 3) 
fitw <- lm(Petal.Length ~ Petal.Width * Species, data = iris,
weights = wts)

newdat <- data.frame(
Petal.Width = rep(seq(from = min(iris$Petal.Width),
to = max(iris$Petal.Width), length.out = 100),
3),
Species = c(rep("setosa", 100), rep("versicolor", 100), rep("virginica", 100))
)
newdat$Petal.Length <- predict(fitw, newdata = newdat)

library(ggplot2)
ggplot(data = newdat, aes(y = Petal.Length, x = Petal.Width,
colour = Species)) +
geom_path(aes(linetype = Species), size = 1.1) +
geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length, shape = Species,
size = wts), show.legend = TRUE) +
scale_shape_discrete(name = "Species") +
scale_size_identity(guide = "none")

给予:

plot

很难说出符号如何映射到因子水平。我可以让线条不那么粗,但当然我可能不想那样做。如果我没有将大小映射到 wts,我可以只使用 geom_point 中的 size 参数。但由于大小映射到 wts,图例的点形状表示似乎回落到默认值,无论线大小是否已更改。

有没有办法改变图例中点的大小以达到形状/颜色的美感?

最佳答案

我用了this发布指导 - 显然只能使用底层网格系统分别缩放点和线:

library(ggplot2)
ggplot(data = newdat, aes(y = Petal.Length, x = Petal.Width,
colour = Species)) +
geom_path(aes(linetype = Species), size = 1.1) +
geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length, shape = Species,
size = wts), show.legend = TRUE) +
scale_shape_discrete(name = "Species") +
scale_size_identity(guide = "none")


library(grid)

grid.ls(grid.force()) # To get the names of all the grobs in the ggplot

# The edit - to set the size of the point in the legend to 4 mm
grid.gedit("key-3-1-2.4-2-4-2", size = unit(5, "mm"))
grid.gedit("key-4-1-2.5-2-5-2", size = unit(5, "mm"))
grid.gedit("key-5-1-2.6-2-6-2", size = unit(5, "mm"))

输出: enter image description here

关于r - ggplot2:当大小是​​映射美学时,什么控制图例中点的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51576147/

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