R等级图:根据一个变量显示绿色 - 白色 - 红色(0上的白色),但显示另一个变量的值 [英] R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable

查看:313
本文介绍了R等级图:根据一个变量显示绿色 - 白色 - 红色(0上的白色),但显示另一个变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题非常具有自描述性。我想用lattice来做一个类似heatmap的绘图,并显示数据值,就像



谢谢!

解决方案

你的问题有几个部分。让我们逐个解决它们:



1:更改标签。这可以通过改变 panel.text()的第三个参数来完成:

 <$ c (x,y,z,...){
panel.levelplot(x,y,z,...)
panel.text(x,y, round(pv.df $ p.value,2))
}

2:改变颜色比例,白色定位在0 。计算色标每段的长度应该是多长,然后分别定义每段:

  color.ramp.length < - 20 
negative.length < - round(abs(范围(pv.df $ fold.change)[1])/
diff(范围(pv.df $ fold.change))* $ b (colorRampPalette(c(seagreen,white))),其中每个颜色的颜色都是相同的颜色。 (negative.length),
colorRampPalette(c(white,firebrick))(positive.length))

(注意:您可以使用


The title is pretty much self-descriptive. I want to do a heatmap-like plot with lattice, showing the data values as well, something like in here

However, in my case, I want to color the plot according to one variable (fold.change), but show the values of another variable (p.value).

It would be optimal that the color range is green-white-red, and the white is on 0 (negative fold.change values in green, and positive ones in red).

My last question would be how to change the text size of the title and axis text, remove axis title, and rotate x axis text 45 degrees; I don't find this information in the documentation. Thanks!

This is my MWE so far:

library(lattice)
library(latticeExtra)
library(RColorBrewer)

pv.df <- data.frame(compound = rep(LETTERS[1:8], each = 3), 
                    comparison = rep(c("a/b", "b/c", "a/c"), 8), 
                    p.value = runif(24, 0, 1), 
                    fold.change = runif(24, -2, 6))

myPanel <- function(x, y, z, ...) {
    panel.levelplot(x,y,z,...)
    panel.text(x, y, round(z,1))
}

cols <- rev(colorRampPalette(brewer.pal(6, "RdYlGn"))(20))

png(filename = "test.png", height = 1000, width = 600)
print(
    levelplot(fold.change ~ comparison*compound,
              pv.df,
              panel = myPanel,
              col.regions = cols,
              colorkey = list(col = cols, 
                              at = do.breaks(range(pv.df$fold.change), 20)),
              scales = list(x = list(rot = 90)),
              main = "Total FAME abundance - TREATMENT", 
              type = "g")
)
dev.off()

Which produces this plot:

Thanks!

解决方案

There are several parts to your question. Let's address them one by one:

1: Change labels. This can be done by varying the 3rd argument for panel.text():

myPanel <- function(x, y, z, ...) {
  panel.levelplot(x, y, z, ...)
  panel.text(x, y, round(pv.df$p.value, 2))
}

2: Change color scale with white positioned at 0. Calculate how long each segment of the color scale should be, then define each segment separately:

color.ramp.length <- 20
negative.length <- round(abs(range(pv.df$fold.change)[1]) / 
                           diff(range(pv.df$fold.change)) * 
                           color.ramp.length)
positive.length <- color.ramp.length - negative.length
cols <- c(colorRampPalette(c("seagreen", "white"))(negative.length),
          colorRampPalette(c("white", "firebrick"))(positive.length))

(Note: you can use other color options from here. I just find the colors associated with "red" / "green" an eye sore.)

3: Modify axis titles / labels. Specify the relevant arguments in levelplot().

levelplot(fold.change ~ comparison*compound,
          pv.df,
          panel = myPanel,
          col.regions = cols,
          colorkey = list(col = cols, 
                          at = do.breaks(range(pv.df$fold.change), 
                                         color.ramp.length)),
          xlab = "", ylab = "",              # remove axis titles
          scales = list(x = list(rot = 45),  # change rotation for x-axis text
                        cex = 0.8),          # change font size for x- & y-axis text
          main = list(label = "Total FAME abundance - TREATMENT",
                      cex = 1.5))            # change font size for plot title

这篇关于R等级图:根据一个变量显示绿色 - 白色 - 红色(0上的白色),但显示另一个变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆