如何为 geom_points() 的大小提供图例并设置点本身的大小?

问题描述 投票:0回答:1

我正在尝试绘制一种气泡图,但我无法设置点的大小并有一个图例解释大小调整的工作原理。

这两个例子结合起来给了我预期的输出。第一个允许我设置点的大小。第二个打印图例。我需要两者:大小点和解释大小的图例。但即使我有一个,我也无法得到另一个。如果能够为“尺寸图例”设置标题,那就太好了。

有什么线索吗?

library("ggplot2")

x <- sample(2000, 1000, replace=T)
y <- sample(2000, 1000, replace=T)
category <- sample(c("one", "two"), 2000, replace = TRUE)
size <- sample(2000, 1000, replace=T)/50

df <- data.frame(x, y, category, size)

# first example with sized dots
dev.new()

ggplot(df, aes(x=x, y=y, fill=category))+
   geom_point(shape=21, color="black", size=df$size)+
   guides(fill=guide_legend(override.aes=list(size=40)))

# second example with the legend
dev.new()

ggplot(df, aes(x=x, y=y, fill=category, size=size))+
   geom_point(shape=21, color="black")+
   guides(fill=guide_legend(override.aes=list(size=40)))

如果有重复的问题,请链接并标记此问题。谢谢!

r ggplot2 legend geom-point
1个回答
0
投票
ggplot(df, aes(x=x, y=y, fill=category, size=size))+
  geom_point(shape=21, color="black") +
  guides(fill=guide_legend(override.aes=list(size=8), title = "New category"))

© www.soinside.com 2019 - 2024. All rights reserved.