Oma*_*iri 4 r continuous ggplot2
是否可以用连续变量填充 ggplot 的 geom_dotplot?
library(ggplot2)
ggplot(mtcars, aes(x = mpg, fill = disp)) +
geom_dotplot()
Run Code Online (Sandbox Code Playgroud)
这应该很简单,但我试过搞乱 aes 组,但没有成功。
我能做的最大事情是离散化 disp 变量,但它不是最优的。
ggplot(mtcars, aes(x = mpg, fill = factor(disp))) +
geom_dotplot()
Run Code Online (Sandbox Code Playgroud)
好问题!您必须设置group = variable
在aes
(其中variable
等于您用于fill
or的同一列color
):
library(ggplot2)
ggplot(mtcars, aes(mpg, fill = disp, group = disp)) +
geom_dotplot()
Run Code Online (Sandbox Code Playgroud)
geom_dotplot
in away 就像一个直方图。分组完成后,您无法在那里轻松设置填充/颜色。要使其工作,您必须设置group
.
使用示例geom_histogram
:
ggplot(mtcars, aes(mpg, fill = disp, group = disp)) +
geom_histogram()
Run Code Online (Sandbox Code Playgroud)