I want to create a heatmap-like plot for my data with custom colors and values. I read some blogs and QAs on StackOverflow, I found that scale_colour_gradientn
is the best way to do this. However, I tried my best but I cannot align the colors and the values and I still don't know why.
It seems there is something wrong in my key code:
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))
))
The following is my reproducible code.
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