在下面的例子中,我的目标是A和B有相同的调色板,C有不同的调色板,D和E有相同的调色板.
# Number of IDs
n_ids <- 10
# Generate IDs
ids <- paste0("ID_", 1:n_ids)
# Number of periods per ID
n_periods <- 3
# Generate periods
periods <- rep(c("P1", "P2", "P3"), each = n_ids)
# Number of categories
n_cats <- 5
# Generate categories
categories <- rep(LETTERS[1:n_cats], times = n_ids * n_periods)
# Generate values
values <- rnorm(n_ids * n_periods * n_cats, 100, 15)
# Assemble into dataframe
df <- data.frame(ID = ids,
Period = periods,
Category = categories,
Value = values)
# Plot
p <- ggplot(df, aes(x=Period, y=Value, fill=Period)) +
geom_boxplot() +
geom_jitter(width=0.2,alpha=0.4, size=1) +
facet_wrap(~Category, ncol=6, scales='free') +
labs(title="Repetitive Behavior Score", x="Group", y="Value")
# Color vector
colors <- c("#1F78B4", "#E69F00", "#33A02C", "#FB9A99")
# Map colors to categories
color_map <- c(
"A" = "#1F78B4",
"B" = "#1F78B4",
"C" = "#E69F00",
"D" = "#33A02C",
"E" = "#33A02C"
)
# Set fill scale
p +
scale_fill_manual(values = color_map)
但他们都只是回归灰色.以前有人试过这个吗?我知道scale_fill
和scale_color
可以做到,但我找不到小平面面板的例子.