[英]R: Change colorPalette colors in risk heatmap using ggplot
謝謝你看我的問題。 我創建了一個帶有顏色漸變的熱 map。 顏色漸變看起來不錯,但是,我希望 colors 更明確。 我在這里附上的第一張圖片
是我運行代碼時得到的。 第二張圖
是我想要得到的。 我不知道如何解決這個問題。 下面是我的代碼的一個小示例。 為了清楚起見,我希望我的代碼返回第二張圖片,其中 colors 與綠色、金色、橙色、淺紅色、紅色和深紅色明顯不同。
library(RColorBrewer)
library(dplyr)
library(ggplot2)
nRow <- 5
nCol <- 5
m3 <- matrix(c(2,2,3,3,3,1,2,2,3,3,1,1,2,2,3,1,1,2,2,2,1,1,1,1,2), nrow = 5, ncol = 5, byrow = TRUE)
myData <- m3 #matrix(rnorm(nRow * nCol), ncol = nCol)
rownames(myData) <- c("5", "4", "3", "2","1")
colnames(myData) <- c("1", "2", "3", "4","5")
longData <- reshape2::melt(myData)
colnames(longData) <- c("Likelihood", "Consequence", "value")
longData <- mutate(longData, value = Consequence * Likelihood)
cols <-function(n) {
colorRampPalette(rev(c("red4","red2","tomato2","orange","gold1","forestgreen")))(6)
}
display_risk <- mutate(longData, value = Consequence * Likelihood)
ggplot(longData,aes(x = Consequence, y = Likelihood, fill = value)) +
geom_tile() +
scale_fill_gradientn(colours = cols(6)) +
theme(axis.text.y = element_text(angle=90, hjust=1), legend.position = "none") +
scale_x_continuous(name = "Probability", breaks = seq(1,5,1), expand = c(0, 0)) +
scale_y_reverse(name= "Severity", breaks = seq(1,5,1), expand = c(0, 0)) +
geom_hline(yintercept = seq(1.5,5.5)) +
geom_vline(xintercept = seq(1.5,5.5)) +
coord_fixed()
以下是一些我嘗試但沒有運氣的答案的鏈接。
除了 map 你想要的 colors 到特定的值范圍之外,我幾乎想不出有什么不同的方法。 見下文。 請檢查我是如何減少你的代碼的,有很多不必要的調用,(我猜你是從你嘗試過不同東西的腳本中復制它的)。 另外,我更改了 colorRampPalette 調用——這是一個 function 生成器,這里不需要使用function()
。
請注意,您需要手動定義這些值,我想這將是您的研究人員決定如何呈現數據。 您需要將其縮放到 0:1 的范圍
library(RColorBrewer)
library(dplyr)
library(ggplot2)
myData <- matrix(c(2,2,3,3,3,1,2,2,3,3,1,1,2,2,3,1,1,2,2,2,1,1,1,1,2), nrow = 5, ncol = 5, byrow = TRUE)
longData <- reshape2::melt(myData)
colnames(longData) <- c("Likelihood", "Consequence", "value")
longData <- mutate(longData, value = Consequence * Likelihood)
mycols <- rev(c("red4","red2","tomato2","orange","gold1","forestgreen"))
cols <- colorRampPalette(mycols)
myvals <- c(0, 8, 9, 10, 11, 25)
scaled_val <- scales::rescale(myvals, 0:1)
ggplot(longData, aes(x = Consequence, y = Likelihood, fill = value)) +
geom_tile() +
scale_fill_gradientn(colours = cols(length(mycols)),
values = scaled_val) +
theme(axis.text.y = element_text(angle = 90, hjust = 1), legend.position = "none") +
scale_x_continuous(name = "Probability", breaks = seq(1, 5, 1), expand = c(0, 0)) +
scale_y_reverse(name = "Severity", breaks = seq(1, 5, 1), expand = c(0, 0)) +
geom_hline(yintercept = seq(1.5, 5.5)) +
geom_vline(xintercept = seq(1.5, 5.5)) +
coord_fixed()
此外,您可以定義漸變從哪里開始。 我在最近的一個帖子中展示了如何做到這一點。 請注意您想要的 output 與這些值不匹配(我已將它們疊加以證明這一點)。 另請注意,這一切顯然都是由您自己定義的——我選擇的那些值是隨機的,您可以根據自己的喜好對其進行調整。
myvals <- c(0, 6, 7, 9, 10, 11, 25)
scaled_val <- scales::rescale(myvals, 0:1)
ggplot(longData, aes(x = Consequence, y = Likelihood, fill = value)) +
geom_tile() +
geom_text(aes(label = value)) +
scale_fill_gradientn(colours = c(mycols[1], mycols),
values = scaled_val) +
theme(axis.text.y = element_text(angle = 90, hjust = 1), legend.position = "none") +
scale_x_continuous(name = "Probability", breaks = seq(1, 5, 1), expand = c(0, 0)) +
scale_y_reverse(name = "Severity", breaks = seq(1, 5, 1), expand = c(0, 0)) +
geom_hline(yintercept = seq(1.5, 5.5)) +
geom_vline(xintercept = seq(1.5, 5.5)) +
coord_fixed()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.