ggplot2渐变如何设置自定义颜色和值?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ggplot2渐变如何设置自定义颜色和值?相关的知识,希望对你有一定的参考价值。
我想使用自定义颜色和值为我的数据创建类似热图的图。我在StackOverflow上阅读了一些博客和QA,发现scale_colour_gradientn
是执行此操作的最佳方法。但是,我尽了最大努力,但是我无法对齐颜色和值,而且我仍然不知道为什么。
似乎我的关键代码有问题:
sc <- scale_colour_gradientn(
colors = c("blue", "white", "yellow", "red"),
values = scales::rescale(c(
data$measure %>% min(na.rm = TRUE),
0,
60,
data$measure %>% max(na.rm = TRUE))
))
以下是我的可复制代码。
library(dplyr)
library(ggplot2)
data <- structure(list(signature = structure(c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("expo1", "expo2",
"expo3", "expo4", "expo5", "expo6", "expo7", "expo8"
), class = "factor"), feature = structure(c(1L, 2L, 9L, 10L,
1L, 2L, 11L, 4L, 6L, 8L, 9L, 10L, 11L, 4L, 8L, 9L, 10L, 1L, 2L,
4L, 6L, 10L, 1L, 2L, 11L, 4L, 6L, 8L, 9L, 10L, 1L, 2L, 11L, 3L,
4L, 6L, 8L, 7L, 9L, 10L, 1L, 2L, 11L, 3L, 4L, 5L, 6L, 8L, 7L,
9L, 10L, 1L, 2L, 11L, 4L, 5L, 6L, 8L, 7L, 9L, 10L), .Label = c("Cell_Cycle",
"Hippo", "MYC", "NOTCH", "NRF2", "PI3K", "TGF-Beta", "RTK-RAS",
"TP53_pathway", "WNT", "HR_pathway"), class = "factor"), count = c(923,
923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923,
923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923,
923, 923, 923, 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
959, 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
959, 959, 959, 959, 959, 959, 959, 959), measure = c(3.35, 2.58,
4.5, 1.58, 25.7, 17.2, 12.1, 12.8, 4.93, 15, 16.8, 14.1, 7.83,
7.9, 8.62, 23.5, 8.74, 10.9, 5.44, 7.49, 9, 8.04, -3.51, -2.66,
-2.8, -2.85, -2, -2.48, -4.68, -3, 18.9, 18.6, 27, 39.4, 23.6,
13.3, 18.2, 35.1, 11.3, 18, 173, 60.8, 114, 323, 67.5, 531, 78.5,
56.7, 297, 39, 59.8, 144, 55, 73.4, 59.2, 262, 67.1, 51.2, 197,
25.9, 53.8), p = c(0.0228148148148148, 0.0379866666666667, 3.21391304347826e-07,
0.0083330612244898, 0.000378888888888889, 5.78285714285714e-06,
0.0270175438596491, 7.65333333333333e-05, 0.00438044444444444,
1.8392e-05, 5.56285714285714e-11, 0.000377142857142857, 0.0416918032786885,
0.00926588235294118, 0.000426461538461538, 1.6368e-17, 0.00550468085106383,
0.0359457627118644, 0.0286758620689655, 0.00438044444444444,
0.00421581395348837, 0.00760833333333333, 0.000426461538461538,
5.85655172413793e-06, 0.000426461538461538, 2.948e-06, 0.00156933333333333,
5.24740740740741e-06, 1.0956e-19, 1.1484e-07, 3.20774193548387e-05,
4.54892307692308e-11, 2.832e-11, 0.0106276923076923, 4.95e-15,
0.000106117647058824, 4.60533333333333e-12, 0.0240428571428571,
1.21691428571429e-12, 9.3808e-15, 1.11936e-06, 4.49533333333333e-11,
2.68631578947368e-08, 0.00127921951219512, 5.027e-10, 0.00550468085106383,
7.7e-07, 9.83529411764706e-10, 0.0126520754716981, 4.98666666666667e-14,
4.532e-12, 1.21104761904762e-07, 1.1792e-11, 0.02336, 3.21391304347826e-07,
0.0087648, 0.0010648, 7.97866666666667e-11, 4.7025e-05, 1.0956e-19,
3.92088888888889e-09), Samples = structure(c(5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("(0,200]", "(200,400]",
"(400,600]", "(600,800]", "(800,1.02e+03]"), class = "factor")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -61L))
summary(data$measure)
sc <- scale_colour_gradientn(
colors = c("blue", "white", "yellow", "red"),
values = scales::rescale(c(
data$measure %>% min(na.rm = TRUE),
0,
60,
data$measure %>% max(na.rm = TRUE))
))
p <- ggplot2::ggplot(data, ggplot2::aes(
x = signature,
y = feature
))
p <- p + ggplot2::geom_point(ggplot2::aes_string(
colour = "measure",
size = "Samples"
)) + ggplot2::scale_size_discrete(drop = FALSE) +
ggplot2::scale_x_discrete(drop = FALSE)
p + sc
答案
您可以简单地从scale_colour_gradientn
中删除“蓝色”颜色,强制从“白色”到“红色”的渐变。因此,您将获得一个图表,其中低值(接近0)用“白色”着色。这是我对您的代码所做的唯一修改。
sc <- scale_colour_gradientn(
colors = c("white","yellow", "red"),
values = scales::rescale(c(
data$measure %>% min(na.rm = TRUE),
0,
60,
data$measure %>% max(na.rm = TRUE))
))
以上是关于ggplot2渐变如何设置自定义颜色和值?的主要内容,如果未能解决你的问题,请参考以下文章
R语言ggplot2可视化热力图(heatmap)自定义配置图例标签为百分比进行热力图颜色渐变显示(legend to display percentage sign)
R语言ggplot2可视化:自定义设置连续变量图例(legend)宽度(width)自定义设置连续变量图例位置(position)自定义设置连续变量图例连续渐变
R语言R原生以及ggplot2设置线条类型宽度(粗细)颜色的函数ggplot2手动自定义设置线条类型粗细颜色函数(line typesthicknesscolour)