[英]Continuous gradient color & fixed scale heatmap ggplot2
I'm switching from Mathematica to R but I'm finding some difficulties with visualizations. 我正在从Mathematica切换到R,但我发现可视化有些困难。
I'm trying to do a heatmap as follows: 我正在尝试按如下方式进行热图:
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)
And I get this: 我得到了这个:
But I need something like this: 但是我需要这样的东西:
That is, I would like that the color is continuous (degraded) over the surface of the plot instead of discrete for each square. 也就是说,我希望颜色在绘图表面上连续(降级)而不是每个正方形的离散。 I've seen in other SO questions that some people interpolate de data but I think there should be an easier way to do it inside the ggplot call (in Mathematica is done by default).
我在其他SO问题中看到有些人插入de数据,但我认为在ggplot调用中应该有一种更简单的方法(默认情况下在Mathematica中完成)。
Besides, I would like to lock the color scale such that the 0 is always white (separating therefore between warm colors for positive values and cold for negative ones) and the color distribution is always the same across plots independently of the range of the data (since I will use the same plot structure for several datasets) 此外,我想锁定色标,使得0总是白色(因此在暖色之间分离为正值,冷色之间为负值),并且颜色分布在各个图中始终相同,与数据范围无关(因为我将对几个数据集使用相同的绘图结构)
You can use geom_raster
with interpolate=TRUE
: 您可以将
geom_raster
与interpolate=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()
To get the same color mapping to values of pi0
across all of your plots, set the limits
argument of scale_fill_gradient2
to be the same in each plot. 要在所有绘图中获得与
pi0
值相同的颜色映射,请将scale_fill_gradient2
的limits
参数设置为在每个绘图中相同。 For example, if you have three data frames called short
, short2
, and short3
, you can do this: 例如,如果您有三个名为
short
, short2
和short3
数据框, short2
可以执行以下操作:
# Get range of `pi0` across all data frames
pi0.rng = range(lapply(list(short, short2, short3), function(s) s$pi0))
Then set limits=pi0.rng
in scale_fill_gradient2
in all of your plots. 然后在所有绘图中的
scale_fill_gradient2
中设置limits=pi0.rng
。
I would adjust your scale_fill_gradient2
: 我会调整你的
scale_fill_gradient2
:
scale_fill_gradient2('pi0', low = "blue", mid = "white", high = "red", midpoint = 0)
to make plot colours directly comparable add consistent limits
to each plot: 使绘图颜色直接可比,为每个绘图添加一致的
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.