繁体   English   中英

组内的ggplot scale_fill_manual

[英]ggplot scale_fill_manual within groups

我正在尝试使用scale_fill_manual ggplot的各个条形的颜色,但是由于条形组内(此处为“条件”),因此遇到了麻烦。 我该如何克服这个问题?

这是我的数据集:

df <- data.frame(
"condition" = c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B"), 
"type" = c("Apple", "Apple", "Apple", "Apple", "Pear", "Pear", "Pear", "Pear", "Orange", "Orange", "Orange", "Orange", "Tomato", "Tomato", "Tomato", "Tomato", "Tomato", "Berry", "Berry", "Berry"), 
"ripeness" = c("bad","bad", "good", "good", "bad", "bad", "good", "good", "bad","bad", "good", "good", "bad", "bad", "good", "good", "bad","bad", "good", "good"), 
count = c(19, 4, 7, 2, 7, 7, 1, 13, 16, 16, 31, 13, 30, 12, 39, 17, 1, 36, 4, 27)
)

以及我用于 plot 的代码:

plot <- ggplot(df, aes(x=type, y=count, fill=ripeness)) +
  geom_bar(stat="summary", width = .7, position = position_dodge(width = .75)) +
  theme_light()

#the below is non-working code
plot + scale_fill_manual(values=c("black","blue","black","green","black","stripes-2","black","stripes-3","black", "yellow"))

如何指定各个条的 colors? 出于解释的目的,我写了颜色名称,但会将这些替换为 html 颜色代码。 后来,我还打算使用 ggpattern ( https://coolbutuseless.github.io/package/ggpattern/articles/geom-gallery-geometry.html#colour-example-1 ) 纹理我的两个条形图如果太难,需要在这个帖子中解决。

我正在尝试在 r 中重新创建此 plot:

在此处输入图像描述

一种方法是在 dataframe 中添加一列 colors 并使用scale_fill_identity

library(dplyr)
library(ggplot2)

colors <- c("black","blue","black","green","black","orange",
            "black","pink","black", "yellow")

df %>%
  distinct(type, ripeness) %>%
  mutate(color = colors) %>%
  inner_join(df, by = c('type', 'ripeness')) %>%
  ggplot(aes(x=type, y=count, fill=color)) +
  geom_bar(stat="summary", width = .7, position = position_dodge(width = .75)) +
  theme_light() +
  scale_fill_identity()

在此处输入图像描述

PS - 我无法制作"stripes-2""stripes-3"颜色,因此将其更改为其他随机颜色。

这是一种使用两种美学(颜色和填充)来获得两个图例的方法。 即使可以在 {ggplot2} 中重现 plot,在我看来,使用两种美学也无助于更好地可视化数据。

library(ggplot2)
library(tidyverse)

df <- data.frame(
"condition" = c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", 
  "A", "B", "A", "B", "A", "B", "A", "B"), 
"type" = c("Apple", "Apple", "Apple", "Apple", "Pear", "Pear", "Pear", "Pear", 
  "Orange", "Orange", "Orange", "Orange", "Tomato", "Tomato", "Tomato", 
  "Tomato", "Tomato", "Berry", "Berry", "Berry"), 
"ripeness" = c("bad","bad", "good", "good", "bad", "bad", "good", "good", 
  "bad","bad", "good", "good", "bad", "bad", "good", "good", "bad","bad",
  "good", "good"), 
count = c(19, 4, 7, 2, 7, 7, 1, 13, 16, 16,
  31, 13, 30, 12, 39, 17, 1, 36, 4, 27)
)

# to get the same order of types as in the given Figure
df$type <- factor(df$type, 
  levels = c("Apple", "Pear", "Orange", "Tomato", "Berry"))
df |> 
  ggplot() +
  # first, plot the bars for "good" and map type to fill aesthetics
  # Change width and position to that that corresponds to good when fill is 
  # mapped to ripeness
  geom_bar(data = df |> filter(ripeness == "good"), 
    aes(x=type, y=count, fill = type),
    stat="summary", fun = mean,
    position = position_nudge(x = 0.225), width = 0.45, color = "white") +
  # second, plot the bars for "bad" and map ripeness to color asthetics
  # Change width and position to that that corresponds to good when fill is 
  # mapped to ripeness. We need to use a different aesthetics to get two
  # legends
  geom_bar(data = df |> filter(ripeness == "bad"), 
    aes(x=type, y=count, color = ripeness), 
    stat="summary", fun = mean,
    position = position_nudge(x = -0.225), width = 0.45) +
  # Legend for the color aesthetics (with no name)
  scale_color_manual(name = "", values = "white", breaks = "bad") +
  # Legend for the fill aesthetics
  # Colors for the bars are given in the paramters values
  scale_fill_manual(name = "good", values = hcl.colors(5), 
    breaks = levels(df$type)) +
  # Reproduce the axis in the figure
  scale_y_continuous(breaks = seq(0, 35, by = 5), limits = c(0,40), 
    expand = c(0,0)) + 
  scale_x_discrete(expand = c(0.13,0)) + 
  labs(x = "", y = "frequency") +
  theme_classic() +
  # Avoid ticks in x axis as in the example
  theme(axis.ticks.x = element_blank())

代表 package (v2.0.1) 于 2022 年 1 月 23 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM