我正在尝试使用ggplot2制作一个热图,其中包含3种类型的变量,每个变量都需要自己的独立图例/比例。
我可以将它们全部绘制在一个热图中(如下图所示),但是我很难将它们分开以拥有自己的图例。我的三个类别是“得分”,“ samp1”行和其余数据。我希望它们中的每一个都具有各自范围的独立图例。
我唯一的补充是使行分数具有绿色,黄色,红色(低,中,高)配色方案,如果可以的话,可以将其包括在内。
这是我用来创建该图的代码
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个类别来分离数据。我也希望使用此分组来创建自己的图例,但是我不确定这是否可行。
单击此处下载数据(预融化)。
先感谢您。
这可以通过如下的ggnewscale
软件包来实现:
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()
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句