R ggplot2:当尺寸为贴图尺寸时,什么控制图例中点的尺寸?

R ggplot2:当尺寸为贴图尺寸时,什么控制图例中点的尺寸?,r,ggplot2,R,Ggplot2,如果同时具有线型和形状美学(分别具有几何图形路径和几何图形点),则有时很难识别图例中的点,因为它们与直线重叠 这里有一个有效的例子。我有一个加权线性模型,希望将预测数据和观测数据一起绘制 iris$wts <- runif(nrow(iris), min = 0.2, max = 3) fitw <- lm(Petal.Length ~ Petal.Width * Species, data = iris, weights = wts) newdat &l

如果同时具有线型和形状美学(分别具有几何图形路径和几何图形点),则有时很难识别图例中的点,因为它们与直线重叠

这里有一个有效的例子。我有一个加权线性模型,希望将预测数据和观测数据一起绘制

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") 
iris$wts我使用post作为指导-显然,只有使用基础网格系统才能单独缩放点和线:

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"))
输出:

您可以在某些天平中使用
override.aes
,尽管我很难在同一图例中获得一个尺寸的点和另一个尺寸的线。或者,如果您只想在图例中显示点,可以在
geom\u行中使用
show.legend=F