下面是一个数据框架作为例子。
library(tidyverse)
library(ggpattern)
dat <- data.frame(drv = c("4", "4", "4", "4", "4", "f", "f", "f", "f", "r", "r", "r"),
class = c("compact", "midsize", "pickup", "subcompact", "suv",
"compact", "midsize", "minivan", "subcompact",
"2seater", "subcompact", "suv"),
y = c(12L, 3L, 33L, 4L, 51L, 35L, 38L, 11L, 22L, 5L, 9L, 11L))
dat
x11();dat %>% ggplot(aes(x = class, y = y ,fill = drv, pattern = drv)) +
# geom_col()+ # not necessary
geom_col_pattern() +
coord_flip() +
theme_minimal() +
scale_fill_manual(
name = "Drive",
values = c("4" = "#EEF12B",
"f" = "#D058EC",
"r" = "#FF27D5"),
labels = c(expression(italic("4"),
italic("f"),
italic("f")))
) +
scale_pattern_manual(name = "Drive",
values = c("4" = "none",
"f" = "stripe",
"r" = "none")
)+
scale_pattern_fill_manual(
values = c("f" = "#5284D9")
)我得到的情节如下:

我想合并我的两个传说,以使背景颜色,但只有f类条纹。通过我的代码,我也想定制条纹的颜色,但没有成功。
有什么建议吗?
发布于 2022-10-26 21:05:43
要合并传奇,您必须对scale_pattern_manual使用与scale_fill_manual相同的标签。
library(ggplot2)
library(ggpattern)
ggplot(dat, aes(x = class, y = y, fill = drv, pattern = drv)) +
geom_col_pattern() +
coord_flip() +
theme_minimal() +
scale_fill_manual(
name = "Drive",
values = c(
"4" = "#EEF12B",
"f" = "#D058EC",
"r" = "#FF27D5"
),
labels = c(expression(
italic("4"),
italic("f"),
italic("f")
))
) +
scale_pattern_manual(
name = "Drive",
values = c(
"4" = "none",
"f" = "stripe",
"r" = "none"
),
labels = c(expression(
italic("4"),
italic("f"),
italic("f")
))
) +
scale_pattern_fill_manual(
values = c("f" = "#5284D9")
)

https://stackoverflow.com/questions/74213979
复制相似问题