R ggplot热图,其中多行在同一图形上具有单独的图例 [英] R ggplot heatmap with multiple rows having separate legends on the same graph

查看:50
本文介绍了R ggplot热图,其中多行在同一图形上具有单独的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggplot2制作一个热图,其中包含3种类型的变量,每个变量都需要自己的独立图例/比例.

I'm trying to make one heatmap using ggplot2 that contains 3 types of variables where each need their own independent legend/scale.

我能够将它们全部绘制在一个热图中(如下图所示),但是我很难将它们分开以拥有自己的图例.我的三个类别是行"Score","samp1"和"samp1".其余的数据.我希望它们中的每一个都有自己的独立图例以及各自的范围.

I am able to plot them all in one heatmap (pictured below), but I am having trouble separating them to have their own legend. My three categories are the row "Score", "samp1" and the rest of the data. I would like each of these to have their own independent legends with their respective ranges.

我唯一的补充是,如果可能的话,该行分数具有绿色,黄色,红色(低,中,高)配色方案.

My only addition would be to have the row score have a green,yellow,red (low, mid,high) color scheme, if that is possible to include in this question.

这是我用来创建该图的代码

This is the code I am using to create that graph

library(ggplot2)
test_data <- read.csv("test_table.csv", row.names = 1)

ggplot(test_data, aes(x=sample, y=id, fill = value)) + 
  geom_raster() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), # lables vertical
        strip.text.y = element_blank()) +  #remove facet bar on y 
  scale_fill_gradient(low = "darkblue", high = "lightblue") +
  ggtitle("test table") +
  facet_grid(rows = vars(test_data$category), 
             cols = vars(test_data$group), scales = "free", space="free_y") #facets to add gaps 

我已使用构面按样本和上述3类来分离数据.我也希望使用此分组来创建自己的图例,但不确定是否可行.

I have used facets to separate the data by sample and by the 3 categories I described above. I was hoping to use this grouping to create their own legends as well, but I am not sure if this is possible.

点击此处以下载数据(预融化的.)

Click here to download the data (pre-melted).

谢谢.

推荐答案

这可以通过 ggnewscale 包来实现,如下所示:

This could be achieved via the ggnewscale package like so:

library(ggplot2)
library(dplyr)
library(ggnewscale)

ggplot() +
  geom_raster(data = filter(test_data, category == "1 score"), aes(x = sample, y = id, fill = value)) +
  scale_fill_gradient2(low = "green", mid = "yellow", high = "red", midpoint = 4, name = "Score") +
  new_scale_fill() +
  geom_raster(data = filter(test_data, category == "2 samp1"), aes(x = sample, y = id, fill = value)) +
  scale_fill_gradient(low = "darkblue", high = "lightblue", name = "Sample1") +
  new_scale_fill() +
  geom_raster(data = filter(test_data, category == "3 samp2"), aes(x = sample, y = id, fill = value)) +
  scale_fill_gradient(low = "darkblue", high = "lightblue", name = "Sample2") +
  ggtitle("test table") +
  facet_grid(
    rows = vars(category),
    cols = vars(group), scales = "free", space = "free_y"
  ) +
  theme(
    axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
    strip.text.y = element_blank()
  )

这篇关于R ggplot热图,其中多行在同一图形上具有单独的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆