饱和水平图以提高较低水平的对比度

时间:2018-02-22 15:01:44

标签: r plot heatmap lattice levelplot

给定一个如下所示的数据集:

D<-rep(seq(1,5),10)
T<-runif(50,1,20)
S<-c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,9,9,10,10,10,10,10)
DF<-data.frame(D,T,S)

我使用以下代码制作关卡图

library(lattice)
levelplot(T ~ S * D, data = DF,ylim=c(5,1),
xlab = "T", ylab = "S",
main = "LevelPlot", aspect=0.4,
col.regions =colorRampPalette(c('blue','red')),at=seq(0,20, length.out=120))

我想通过增加颜色的对比度来关注数据下端的细微差别(The T变量)。我能够做到这一点的一种方法是改变&#34; seq&#34;关注0-5范围内数据的论据:

levelplot(T ~ S * D, data = DF,ylim=c(5,1),
xlab = "T", ylab = "S",
main = "LevelPlot", aspect=0.4,
col.regions =colorRampPalette(c('blue','red')),at=seq(0,5, length.out=120))

现在对比度是我想要的,但我不喜欢5-20之间的所有数据都被阻止了。在其他绘图程序中,我已经能够使上限范围饱和,使得高于某个值的所有数据都由单个(最大)颜色表示。在这种情况下,大于5的所有值都将为红色(右侧的色标将反映这一点)。允许0-5值的颜色比例更详细的对比度。

这怎么可能在R?

1 个答案:

答案 0 :(得分:1)

您可以使用colorkeyat执行此操作:

my.brks <- seq(0, max(T), by = 3)
my.at <- c(0, 1, 2, 3, 4, 5, round(max(T), 0))
myColorkey <- list(at = my.brks, labels = list(at = my.brks, labels = my.at))

levelplot(
  T ~ S * D,
  data = DF,
  ylim = c(5, 1),
  xlab = "T",
  ylab = "S",
  aspect = 0.4,
  at = my.at,
  col.regions = colorRampPalette(c('blue', 'red')),
  colorkey = myColorkey
)

the specification

library(latticeExtra)
levelplot(
  T ~ S * D,
  data = DF,
  ylim = c(5, 1),
  xlab = "T",
  ylab = "S",
  aspect = 0.4,
  at = my.at,
  col.regions = colorRampPalette(c('blue', 'red')),
  colorkey = myColorkey
) + layer(panel.text(S, D, round(T, 1)), data = DF)

enter image description here