我想在下面的示例中使用分组变量("类别")创建散点图+图例.我如何强制所有因子水平(即LETTERS[1:5]下面),即使在实际数据中缺失,也要显示在图例中(以强调它们的缺席!):
dat <- data.frame(V1 = sample(seq(1:10), 10),
V2 = sample(seq(1:10), 10),
category = factor(sample(LETTERS[1:4], 10, replace=TRUE),
LETTERS[1:5]))
ggplot(dat, aes(x=V1, y=V2)) +
geom_point(aes(size=category), shape=1)
Run Code Online (Sandbox Code Playgroud)
在我的实际脚本中,我scale_size_discrete()用来更改图例标签等.
谢谢!
使用scale_size_discrete()和添加参数drop=FALSE以显示所有级别.
ggplot(dat, aes(x=V1, y=V2)) +
geom_point(aes(size=category), shape=1)+
scale_size_discrete(drop=FALSE)
Run Code Online (Sandbox Code Playgroud)