【问题标题】:Change fill/colour for geom_dotplot or geom_histogram with a continuous variable使用连续变量更改 geom_dotplot 或 geom_histogram 的填充/颜色
【发布时间】:2019-11-29 20:43:20
【问题描述】:

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

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

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

我能做的最大值是离散化 disp 变量,但它不是最优的。

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

【问题讨论】:

    标签: r ggplot2 continuous


    【解决方案1】:

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

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

    geom_dotplot in away 就像一个直方图。分组完成后,您无法轻松设置填充/颜色。要使其工作,您必须设置group

    使用geom_histogram的示例:

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

    【讨论】:

    • 嗯,太好了!由于新的点未被分箱,我设法用geom_dotplot(method = "histodot", binwidth = 1) 再次将它们分箱。谢谢!
    猜你喜欢
    • 2018-12-06
    • 2021-03-30
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多