如何在R中使用ggplot2中的多种颜色比例尺

huangapple go评论64阅读模式
英文:

How to use multiple colour scales in ggplot2 in R

问题

给定这个矩阵,在 x 轴上是潜在值 (1,2,3,4),在 y 轴上是每种药物 (RAS, BB 和 AA) 的三个水平。矩阵的每个格子包含 0 到 1 之间的值,我想用基于显示值的递增颜色渐变为格子上色,并为每种药物创建不同的图例。

# 数据框
df <- 数据框(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))))

# 每种药物不同的颜色
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"
  )
英文:

Given this matrix where on the x-axis are the latent values (1,2,3,4) and on the y-axis three levels for each drug (RAS, BB and AA). Each box of the matrix contains values between 0 and 1, I would like the boxes colored with an increasing colour gradient based on the value shown, with legends different for each drug.

Currently, for each row of each drug, the boxes are colored in the same color (RAS-ivory, BB-darkred, MRA-darkblue ). I would like to create three legends where each legend has a continuous scale of colors (from light to dark) and color the matrix according to its value.

#Dataframe
df &lt;-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(&quot;RAS&quot;,12),rep(&quot;BB&quot;,12),rep(&quot;AA&quot;,12)),
               cat_drug = paste(rep(c(0,1,2),12),sep=&quot;_&quot;,c(rep(&quot;RAS&quot;,12),rep(&quot;BB&quot;,12), rep(&quot;AA&quot;,12))))

#Different color for each drug
color_scale &lt;- c(&quot;RAS&quot; = &quot;ivory&quot;, &quot;BB&quot; = &quot;darkred&quot;, &quot;AA&quot; = &quot;darkblue&quot;)

ggplot(Psi, aes(y = cat_drug, x = state, fill = drug, label = round(value, 3))) +
  geom_raster() +
  geom_text(color = &quot;black&quot;) +
  scale_fill_manual(values = color_scale, name = &quot;Drug&quot;) +
  guides(fill = guide_colourbar(title = &quot;Drug&quot;)) +
  labs(y = &quot;Level of adherence&quot;, x = &quot;Latent state&quot;) +
  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 = &quot;right&quot;
  )

答案1

得分: 2

以下是您要翻译的内容:

"一个选项,用于在相同的美学上具有多个比例尺和图例,是使用 ggnewscale 包。然而,这样做需要额外的努力,因为我们必须使用多个 geom 层,即每个 drug 的一层,当然还有多个 scale。为了避免复制和粘贴,我通过 drug 拆分了数据,然后使用 purrr::imap 来循环遍历拆分的数据并添加图层。不幸的是,我在 geom_raster 方面遇到了一些问题,所以我切换到了 geom_tile,它还可以设置瓷砖的 height。最后请注意,我使用了 scale_fill_distiller 和一些 Brewer 颜色调色板。"

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"
  )

如何在R中使用ggplot2中的多种颜色比例尺

英文:

One option to have multiple scales and legends for the same aesthetic is to use the ggnewscale package. However, doing so requires some additional effort in that we have to use multiple geom layers, i.e. one for each of your drugs and of course multiple scales. To avoid copy & paste I split the data by drug, then use purrr::imap to loop over the splitted data and add the layers. Unfortunately I ran in some issue with geom_raster to I switched to geom_tile which also to set the height of the tiles. Finally note that I use scale_fill_distiller and some of the Brewer color palettes.

library(ggplot2)
library(ggnewscale)

df_split &lt;- split(df, df$drug)
pal_drug &lt;- c(&quot;RAS&quot; = &quot;Blues&quot;, &quot;BB&quot; = &quot;Greens&quot;, &quot;AA&quot; = &quot;Oranges&quot;)

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 = &quot;black&quot;) +
  labs(y = &quot;Level of adherence&quot;, x = &quot;Latent state&quot;) +
  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 = &quot;right&quot;
  )

如何在R中使用ggplot2中的多种颜色比例尺

答案2

得分: 2

The ggh4x package 提供了一种添加多个颜色(或填充)比例尺的方法。

英文:

The ggh4x package offers one way to add multiple color (or fill) scales


library(ggh4x)
#&gt; 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 == &#39;RAS&#39;)
              ) +
  geom_raster(aes(bbf = value),
              data = ~subset(dat, drug == &#39;BB&#39;)
              ) +
  geom_raster(aes(aaf = value),
              data = ~subset(dat, drug == &#39;AA&#39;)
              ) +
  scale_fill_multi(
    aesthetics = c(&#39;rasf&#39;,&#39;bbf&#39;,&#39;aaf&#39;),
    name = list(&#39;ivory&#39;, &#39;darkred&#39;, &#39;darkblue&#39;),
    colors = list(
      brewer_pal(palette = &#39;Greys&#39;)(4),
      brewer_pal(palette = &#39;PuRd&#39;)(4),
      brewer_pal(palette = &#39;Blues&#39;)(4)
    ),
    guide = guide_colorbar(barheight = unit(60, &#39;pt&#39;),
                           title = &quot;Drug&quot;)) +
  geom_text(color = &quot;black&quot;)  +
  labs(y = &quot;Level of adherence&quot;, x = &quot;Latent state&quot;) +
  theme_bw() +
  facet_wrap(vars(category), ncol = 1, dir = &#39;v&#39;, strip.position = &#39;left&#39;) +
  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 = &quot;right&quot;,
    strip.placement = &quot;outside&quot;
  )
#&gt; Warning in geom_raster(aes(rasf = value), data = ~subset(dat, drug == &quot;RAS&quot;)):
#&gt; Ignoring unknown aesthetics: rasf
#&gt; Warning in geom_raster(aes(bbf = value), data = ~subset(dat, drug == &quot;BB&quot;)):
#&gt; Ignoring unknown aesthetics: bbf
#&gt; Warning in geom_raster(aes(aaf = value), data = ~subset(dat, drug == &quot;AA&quot;)):
#&gt; Ignoring unknown aesthetics: aaf

如何在R中使用ggplot2中的多种颜色比例尺<!-- -->

<sup>Created on 2023-04-19 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年4月19日 22:34:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定