我遇到的问题肯定是微不足道的,但我无法解决它。我需要的是
geom_point()
有自己的形状、颜色和尺寸。但是,如果我设置它,图例中符号的大小不会改变。
所以,假设我想绘制这个:
library("ggplot2")
x <- rnorm(2000, 0, 1)
y <- rnorm(2000, 0, 1)
category <- sample(c("one", "two"), 2000, replace = TRUE)
size <- rnorm(2000, 0, 1)*10
df <- data.frame(x, y, category, size)
dev.new()
ggplot(df, aes(x=x, y=y, color=category)) +
geom_point(size = df$size) +
guides(color = guide_legend(override.aes = list(size = 10)))
但是,如果我想改变我的
geom_point()
的美感,我无法设置图例中点的大小。换句话说,这不再起作用了:
dev.new()
ggplot(df, aes(x=x, y=y, fill=category)) +
geom_point(shape=21, colour="black", size = df$size) +
guides(color = guide_legend(override.aes = list(size = 10)))
我觉得我应该向指南传递更多参数,但也许不是。该怎么办?
我找不到我的问题的任何答案。如果您这样做,请将其标记为重复并链接。谢谢!
编辑:
预期的输出由我提供的第二个图表示(其中我设置了
geom_point()
的美感),但是我需要图例中的点像我想要的那样大。
编辑2:
使用
sample
而不是 rnorm
(如评论中所建议)给出了同样的问题,似乎:
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)
dev.new()
ggplot(df, aes(x=x, y=y, color=category)) +
geom_point(size = df$size) +
guides(color = guide_legend(override.aes = list(size = 10)))
dev.new()
ggplot(df, aes(x=x, y=y, fill=category)) +
geom_point(shape=21, colour="black", size = df$size) +
guides(color = guide_legend(override.aes = list(size = 10)))
也许这有帮助?
library(ggplot2)
set.seed(213)
x <- rnorm(2000, 0, 1)
y <- rnorm(2000, 0, 1)
category <- sample(c("one", "two"), 2000, replace = TRUE)
size <- rnorm(2000, 0, 1)*10
ggplot(df, aes(x = x, y=y, fill = category)) +
geom_point(shape = 21, aes(size = size)) +
guides(color = guide_legend(override.aes = list(size = 5)),
fill = guide_legend(override.aes = list(size = 5))) +
scale_size(guide = "none")