提问人:Nicole 提问时间:4/19/2023 更新时间:11/17/2023 访问量:225
如何在 R 的 ggplot2 中使用多个色阶
How to use multiple colour scales in ggplot2 in R
问:
给定这个矩阵,其中 x 轴上是每种药物(RAS、BB 和 AA)的潜在值 (1,2,3,4),y 轴上有三个水平。矩阵的每个框都包含介于 0 和 1 之间的值,我希望这些框根据显示的值以递增的颜色渐变着色,每种药物的图例都不同。
目前,对于每种药物的每一行,盒子的颜色相同(RAS-象牙色,BB-深红色,MRA-深蓝色)。我想创建三个图例,其中每个图例都有一个连续的颜色比例(从浅到深),并根据其值为矩阵着色。
#Dataframe
df <-data.frame(category=rep(c(0,1,2),12),
state=c(rep(1,3),rep(2,3),rep(3,3),rep(4,3)),
value=runif(36),
drug = c(rep("RAS",12),rep("BB",12),rep("AA",12)),
cat_drug = paste(rep(c(0,1,2),12),sep="_",c(rep("RAS",12),rep("BB",12), rep("AA",12))))
#Different color for each drug
color_scale <- c("RAS" = "ivory", "BB" = "darkred", "AA" = "darkblue")
ggplot(Psi, aes(y = cat_drug, x = state, fill = drug, label = round(value, 3))) +
geom_raster() +
geom_text(color = "black") +
scale_fill_manual(values = color_scale, name = "Drug") +
guides(fill = guide_colourbar(title = "Drug")) +
labs(y = "Level of adherence", x = "Latent state") +
theme_bw() +
theme(
axis.text.x = element_text(size = 9, angle = 0, vjust = 0.3),
axis.text.y = element_text(size = 9),
plot.title = element_text(size = 11),
legend.position = "right"
)
答:
4赞
stefan
4/19/2023
#1
为同一美学使用多个比例和图例的一种选择是使用该包。但是,这样做需要一些额外的努力,因为我们必须使用多个层,即每个层一个,当然还有多个层。为了避免复制和粘贴,我用 来拆分数据,然后用它来遍历拆分的数据并添加图层。不幸的是,我遇到了一些问题,所以我切换到了也需要设置瓷砖的模式。最后请注意,我使用了一些 Brewer 调色板。ggnewscale
geom
drug
scale
drug
purrr::imap
geom_raster
geom_tile
height
scale_fill_distiller
library(ggplot2)
library(ggnewscale)
df_split <- split(df, df$drug)
pal_drug <- c("RAS" = "Blues", "BB" = "Greens", "AA" = "Oranges")
set.seed(123)
ggplot(df, aes(y = cat_drug, x = state, label = round(value, 3))) +
purrr::imap(df_split, function(x, y) {
list(
geom_tile(data = x, aes(fill = value), height = 1),
scale_fill_distiller(
palette = pal_drug[[y]], name = y,
direction = 1, limits = c(0, 1)
),
ggnewscale::new_scale_fill()
)
}) +
geom_text(color = "black") +
labs(y = "Level of adherence", x = "Latent state") +
theme_bw() +
theme(
axis.text.x = element_text(size = 9, angle = 0, vjust = 0.3),
axis.text.y = element_text(size = 9),
plot.title = element_text(size = 11),
legend.position = "right"
)
4赞
Seth
4/19/2023
#2
该软件包提供了一种添加多个颜色(或填充)比例的方法ggh4x
library(ggh4x)
#> Loading required package: ggplot2
library(scales)
ggplot(dat, aes(y = drug, x = state, label = round(value, 3))) +
geom_raster(aes(rasf = value),
data = ~subset(dat, drug == 'RAS')
) +
geom_raster(aes(bbf = value),
data = ~subset(dat, drug == 'BB')
) +
geom_raster(aes(aaf = value),
data = ~subset(dat, drug == 'AA')
) +
scale_fill_multi(
aesthetics = c('rasf','bbf','aaf'),
name = list('ivory', 'darkred', 'darkblue'),
colors = list(
brewer_pal(palette = 'Greys')(4),
brewer_pal(palette = 'PuRd')(4),
brewer_pal(palette = 'Blues')(4)
),
guide = guide_colorbar(barheight = unit(60, 'pt'),
title = "Drug")) +
geom_text(color = "black") +
labs(y = "Level of adherence", x = "Latent state") +
theme_bw() +
facet_wrap(vars(category), ncol = 1, dir = 'v', strip.position = 'left') +
theme(
axis.text.x = element_text(size = 9, angle = 0, vjust = 0.3),
axis.text.y = element_text(size = 9),
plot.title = element_text(size = 11),
legend.position = "right",
strip.placement = "outside"
)
#> Warning in geom_raster(aes(rasf = value), data = ~subset(dat, drug == "RAS")):
#> Ignoring unknown aesthetics: rasf
#> Warning in geom_raster(aes(bbf = value), data = ~subset(dat, drug == "BB")):
#> Ignoring unknown aesthetics: bbf
#> Warning in geom_raster(aes(aaf = value), data = ~subset(dat, drug == "AA")):
#> Ignoring unknown aesthetics: aaf
创建于 2023-04-19,使用 reprex v2.0.2
评论
0赞
stefan
4/19/2023
好。Saw 也提供了一个选择。但是还没有使用过它,忘记了它。谢谢提醒。(:ggh4x
评论