簡體   English   中英

連續漸變色和固定比例熱圖ggplot2

[英]Continuous gradient color & fixed scale heatmap ggplot2

我正在從Mathematica切換到R,但我發現可視化有些困難。

我正在嘗試按如下方式進行熱圖:

short 
   penetration scc          pi0
1            0   0  0.002545268
2            5   0 -0.408621176
3           10   0 -0.929432006
4           15   0 -1.121309680
5           20   0 -1.587298317
6           25   0 -2.957853131
7           30   0 -5.123329738
8            0  50  1.199748327
9            5  50  0.788581883
10          10  50  0.267771053
11          15  50  0.075893379
12          20  50 -0.390095258
13          25  50 -1.760650073
14          30  50 -3.926126679
15           0 100  2.396951386
16           5 100  1.985784941
17          10 100  1.464974112
18          15 100  1.273096438
19          20 100  0.807107801
20          25 100 -0.563447014
21          30 100 -2.728923621

mycol <- c("navy", "blue", "cyan", "lightcyan", "yellow", "red", "red4")

ggplot(data = short, aes(x = penetration, y = scc)) +
  geom_tile(aes(fill = pi0)) +
  scale_fill_gradientn(colours = mycol)

我得到了這個:

在此輸入圖像描述

但是我需要這樣的東西: 在此輸入圖像描述

也就是說,我希望顏色在繪圖表面上連續(降級)而不是每個正方形的離散。 我在其他SO問題中看到有些人插入de數據,但我認為在ggplot調用中應該有一種更簡單的方法(默認情況下在Mathematica中完成)。

此外,我想鎖定色標,使得0總是白色(因此在暖色之間分離為正值,冷色之間為負值),並且顏色分布在各個圖中始終相同,與數據范圍無關(因為我將對幾個數據集使用相同的繪圖結構)

您可以將geom_rasterinterpolate=TRUE

ggplot(short , aes(x = penetration, y = scc)) +
  geom_raster(aes(fill = pi0), interpolate=TRUE) +
  scale_fill_gradient2(low="navy", mid="white", high="red", 
                       midpoint=0, limits=range(short$pi0)) +
  theme_classic()

在此輸入圖像描述

要在所有繪圖中獲得與pi0值相同的顏色映射,請將scale_fill_gradient2limits參數設置為在每個繪圖中相同。 例如,如果您有三個名為shortshort2short3數據框, short2可以執行以下操作:

# Get range of `pi0` across all data frames
pi0.rng = range(lapply(list(short, short2, short3), function(s) s$pi0))

然后在所有繪圖中的scale_fill_gradient2中設置limits=pi0.rng

我會調整你的scale_fill_gradient2

scale_fill_gradient2('pi0', low = "blue", mid = "white", high = "red", midpoint = 0)

使繪圖顏色直接可比,為每個繪圖添加一致的limits

scale_fill_gradient2('pi0', low = "blue", mid = "white", high = "red", midpoint = 0, limits=c('your lower limit','your upper limit'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM