首页 > 解决方案 > geom_beeswarm 和手动设置颜色 (scale_fill_manual)

问题描述

我正在使用 ggbeeswarm 包(带有geom_beeswarm())制作 beeswarm 图,并且我想根据因子变量中的因子水平手动填充颜色点。另外,我想设置点周围的颜色(白色)。

将颜色设置为fill = [FACTOR]内部aes()效果很好;这些点有不同的填充颜色,我可以像这样将点周围的线的颜色设置为白色geom_beeswarm(color = "white")(参见下面的代码块#1)。但是当我想用 手动设置填充颜色时scale_fill_manualcolor = "white"in 中的命令geom_beeswarm()似乎会覆盖它(参见下面的代码块 #2)。

我怎样才能a)手动设置颜色和b)将我的点周围的线涂成白色?

(所有这些都适用于geom_point(),但我geom_beeswarm()更喜欢。)

这里有一些虚拟数据和代码来说明这一切:

# Provide minimal dataframe
x <- c(rep(c("A", "B"), 10))
y <- (rnorm(20, 2, 1))
z <- c(rep(c("Treatment", "Zero"), 10))

df <- data.frame(x, y, z)

# Draw beeswarm plot
library(ggbeeswarm)

# Works fine: (code chunk #1)
ggplot(df, aes(x, y, fill = z)) +
  geom_beeswarm(color = "white",
            pch = 21)

# Does not work fine: (code chunk #2)
ggplot(df, aes(x, y, fill = z)) +
   geom_beeswarm(color = "white") +
     scale_shape_manual(values=c(21, 21)) +
     scale_fill_manual(values=c("#104E8B", "#72bcd4"))


   

标签: rggplot2

解决方案


推荐阅读