[英]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.