如何区分条形图中的组但仍保持渐变模式?

How to differentiate groups in a bar plot but still keep the gradient pattern?

如何逐组更改填充图案或颜色?我当前的代码只更改了大纲;我想按性别更改填充图案或填充颜色组(不是轮廓),但还要保留我的眼睛填充渐变。

colors <- c("Green", "Blue", "Hazel", "Brown")
data <- data.frame(HairEyeColor)
data$Eye <- as.numeric(factor(data$Eye, labels = 1:4))
data <- data[c(5,6,12,15,17,22,27,28), ]

ggplot(data, aes(x = Hair, y = Freq, fill = Eye, group = Sex)) + 
  geom_bar(stat = "identity", position = position_dodge(), aes(colour = Sex)) +
  scale_fill_continuous(low = "blue", high = "green")

ggplot2 包中的条形图目前不支持 填充模式 (据我所知,其他包也不支持)。 然而,很少有解决方案可以帮助您轻松发现性别和眼睛的差异,您可以考虑:

1.Using 不同(更浅)fill 颜色,更粗的条形边界和 theme_bw():

 ggplot(data, aes(x = Hair, y = Freq, fill = Eye, group = Sex)) + 
  geom_bar(stat = "identity", position = position_dodge(), aes(colour = Sex), size=2) +
  scale_fill_continuous(low = "white", high = "grey") + theme_bw()

  1. 合并两列:Sex 和 Eye 以获得将用作 fill 参数的新因子列:

    data$Sex_Eye <- paste(data$Sex, data$Eye, sep="_") ggplot(data, aes(x = Hair, y = Freq, fill = Sex_Eye)) + geom_bar(stat = "identity", position = position_dodge()) + theme_bw()

  1. 使用 geom_jitter() 而不是 geom_bar() 并将形状参数设置为 Sex:

ggplot(data, aes(x = Hair, y = Freq, colour = Eye, shape = Sex)) + geom_jitter(size=5) + scale_colour_continuous(low = "blue", high = "green") + theme_bw()