使用连续变量更改geom_dotplot或geom_histogram的填充/颜色

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

是否可以用连续变量填充ggplot的geom_dotplot?

library(ggplot2)
ggplot(mtcars, aes(x = mpg, fill = disp)) +
  geom_dotplot()

enter image description here

这应该非常简单,但是我尝试将aes组弄乱,但没有成功。

我最大能做的就是离散化disp变量,但这不是最佳的。

ggplot(mtcars, aes(x = mpg, fill = factor(disp))) +
  geom_dotplot()

enter image description here

r ggplot2 continuous
1个回答
4
投票

好问题!您必须在group = variable中设置aes(其中variable等于用于fillcolor的同一列):

library(ggplot2)
ggplot(mtcars, aes(mpg, fill = disp, group = disp)) +
  geom_dotplot()

enter image description here

geom_dotplot就像直方图一样。完成分组后,您无法在此处轻松设置填充/颜色。要使其正常工作,必须设置group

使用geom_histogram的示例:

ggplot(mtcars, aes(mpg, fill = disp, group = disp)) +
  geom_histogram()

enter image description here

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