This is my data:
dput(QTdata)
structure(list(QT = c("BMI", "BMI", "BMI", "BMI", "BMI", "BMI",
"BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI",
"BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI",
"BMI", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR",
"WHR", "WHR", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI",
"BMI", "BMI", "BMI", "WHR", "WHR", "WHR", "WHR", "WHR"), E = c("NS",
"NS", "NS", "NS", "NS", "s-BP", "s-BP", "s-BP", "s-BP", "s-BP",
"d-BP", "d-BP", "d-BP", "d-BP", "d-BP", "WC", "WC", "WC", "WC",
"WC", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD", "HD",
"ALC", "ALC", "ALC", "ALC", "ALC", "ALC", "ALC", "ALC", "ALC",
"ALC", "S", "S", "S", "S", "S", "SMK", "SMK", "SMK", "SMK", "SMK"
), QT_vs_E = c("BMI vs NS", "BMI vs NS", "BMI vs NS", "BMI vs NS",
"BMI vs NS", "BMI vs s-BP", "BMI vs s-BP", "BMI vs s-BP", "BMI vs s-BP",
"BMI vs s-BP", "BMI vs d-BP", "BMI vs d-BP", "BMI vs d-BP", "BMI vs d-BP",
"BMI vs d-BP", "BMI vs WC", "BMI vs WC", "BMI vs WC", "BMI vs WC",
"BMI vs WC", "BMI vs HD", "BMI vs HD", "BMI vs HD", "BMI vs HD",
"BMI vs HD", "WHR vs HD", "WHR vs HD", "WHR vs HD", "WHR vs HD",
"WHR vs HD", "WHR vs ALC", "WHR vs ALC", "WHR vs ALC", "WHR vs ALC",
"WHR vs ALC", "BMI vs ALC", "BMI vs ALC", "BMI vs ALC", "BMI vs ALC",
"BMI vs ALC", "BMI vs S", "BMI vs S", "BMI vs S", "BMI vs S",
"BMI vs S", "WHR vs SMK", "WHR vs SMK", "WHR vs SMK", "WHR vs SMK",
"WHR vs SMK"), Model = c("1", "2", "3", "4", "4*", "1", "2",
"3", "4", "4*", "1", "2", "3", "4", "4*", "1", "2", "3", "4",
"4*", "1", "2", "3", "4", "4*", "1", "2", "3", "4", "4*", "1",
"2", "3", "4", "4*", "1", "2", "3", "4", "4*", "1", "2", "3",
"4", "4*", "1", "2", "3", "4", "4*"), p = c(0.04964, 0.05019,
0.01182, 0.01067, 0.013, 2.9e-11, 2.27e-14, 0.00316, 0.00264,
0.005, 0.00543, 0.000326, 0.14687, 0.1481, 0.145, 2e-16, 2e-16,
2e-16, 2e-16, 0, 0.0315, 0.0315, 0.00246, 0.00256, 0.001, 0.31853,
0.34962, 0.03051, 0.03086, 0.028, 1.34e-09, 2.54e-09, 0.00732,
0.00742, 0.01, 2e-16, 2e-16, 1.85e-09, 1.85e-09, 0, 0.331885,
0.330676, 0.110613, 0.113734, 0.162, 0.3783, 0.30389, 0.88776,
0.8798, 0.875), estimate = c(0.009, 0.008982, 0.01132, 0.01148,
0.01148, 0.0304, 0.0335, 0.256, 0.0138, 0.0138, 0.01212, 0.0150775,
0.0066273, -0.0125099, -0.0125099, 0.0362498, 0.0268908, 0.0187004,
0.0186291, 0.0186291, -0.0099627, -0.0097895, 0.012549, 0.012499,
0.012499, 0.003281, 0.0032245, 0.0068639, 0.0068498, 0.0068498,
-0.0199396, -0.0198287, 0.0079702, 0.007956, 0.007956, 0.0524682,
0.0507208, 0.0237038, 0.0236999, 0.0236999, -0.0045565, -0.0045372,
-0.0063086, -0.0062531, -0.0062531, -0.0028646, -0.003402, -0.0007385,
-0.0007913, -0.0007913), number_of_analysis = c(8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4,
4, 4, 4), bonferroni_p_cutoff = c(0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913,
0.00108695652173913, 0.00108695652173913, 0.00108695652173913
), significance = c("insig", "insig", "insig", "insig", "insig",
"sig", "sig", "insig", "insig", "insig", "insig", "sig", "insig",
"insig", "insig", "sig", "sig", "sig", "sig", "sig", "insig",
"insig", "insig", "insig", "sig", "insig", "insig", "insig",
"insig", "insig", "sig", "sig", "insig", "insig", "insig", "sig",
"sig", "sig", "sig", "sig", "insig", "insig", "insig", "insig",
"insig", "insig", "insig", "insig", "insig", "insig")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -50L))
This is the code I tried:
library(ggplot2)
ggplot(data = QTdata, aes(x = QT_vs_E, y = Model, fill = p, size = estimate)) +
geom_tile(color = "white", fill = "white") +
geom_point(shape = 22, color = "black", stroke = 0) +
geom_hline(yintercept = seq_along(unique(QTdata$Model)) + 0.5, color = "black") +
geom_vline(xintercept = seq_along(unique(QTdata$QT_vs_E)) + 0.5, color = "black") +
geom_text(data = subset(QTdata, significance == "sig"),
aes(label = "*"), size = 5, vjust = 0.5, hjust = 0.5, color = "black") +
scale_fill_gradientn(colors = c("lightblue", "white", "red"),
values = c(0, 0.002, 0.05, 1), limit = c(0, max(QTdata$p)),
guide = guide_colorbar(title = "p-value")) +
scale_size(range = c(6, 12)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = "", y = "Model", fill = "p", size = "Estimate") +
ggtitle("Heatmap of QT vs E for each model")
This gives me the following plot:
I cannot see any light blue in the gradient in p-value legend. The reason is quite obvious, as I wanted the p value ranging from 0 to 0.05 to be shades of light blue and the rest be shades of red, and these intervals are in irregular range. But somehow I need to clearly show lightblue appearing in the legend as the red part (insignificant p) is less important. I need to convert the p value legend from continuous gradient scale to discrete scale.
The intervals are as follows:
- < 0.0001 lightblue (with alpha = 1) and with (*) mark
- 0.0001 to 0.05 lightblue (with alpha = 0.5)
- 0.05 to 0.1 red (with alpha = 0.5)
- 0.01 < red (with alpha = 1)
Here alpha means the transparancy of the color. Although the legend is in discrete scale, I need the colors to appear as shades (gradient scale).
Making the p value a factor or a character didn't work for me as previously discussed in R ggplot2: legend should be discrete and not continuous and Continuous to discrete legend on ggplot2 R as my desire is quite different.
Simply, what I need is to remove the continuous color bar in the p value legend shown in the image, and make it something like "Estimate" legend shown in the image, with colors (in given shades).
Thank you very much!