如何为不同级别指定不同的颜色范围?

时间:2011-12-21 06:14:48

标签: r lattice na levelplot

我从[{1}}和levelplot因子中制作格子x,其范围为[0,1]:

y

这是我用来根据这些数据制作图形的R脚本:

      x     y     level                                                                                                                                                                                                                       
1 m3134 m3134 1.0000000                                                                                                                                                                                                                       
2 m3134 m416B 0.4189057                                                                                                                                                                                                                       
3 m416B m3134 0.2696508                                                                                                                                                                                                                       
4 m3134  mA20 0.3322170                                                                                                                                                                                                                       
5  mA20 m3134 0.2454191                                                                                                                                                                                                                       
6 m3134    mB 0.3176792
...

这很好用。我得到了下图:

levelplot

但是,我希望将单元格标记为#!/foo/bar/bin/Rscript --vanilla args <- commandArgs(TRUE) mtxFn <- args[1] pdfFn <- args[2] mtx <- read.table(mtxFn, col.names=c("x", "y", "level")) mtx$level[(mtx$level == 1)] <- NA library(lattice) trellis.device(dev=pdf, file=pdfFn) colors <- colorRampPalette(c('red', 'white'))(256) fig <- levelplot(level~x*y, data=mtx, col.regions=colors, xlab="", ylab="", aspect="iso", scales=list( x=list(rot=90) ), panel=function(...) { arg <- list(...) panel.levelplot(...) panel.text(mtx$x, mtx$y, round(mtx$level*100,0), cex=0.5) } ) print(fig) graphics.off(); ,而不是将单元格标记为NA,而是将所有单元格设置为10(1.00级别)和79(a 0.10的级别0.79。任何大于79的颜色都会被涂上与使用大约相同的颜色相同的颜色。或者,优选地,所述细胞将被涂成黑色,其中根本没有文字。

有没有办法用colors和格子来完成这个?


最终编辑

这并没有给出渐变的颜色,但是我足够接近我会奖励赏金,也许可以考虑levelplot作为替代。感谢您为此付出的辛勤劳动。

以下是我的剧本的最终编辑:

ggplot2

以下是此脚本生成的#! /foo/bar/bin/Rscript --vanilla args <- commandArgs(TRUE) dfFn <- args[1] pdfFn <- args[2] df <- read.table(dfFn, col.names=c("x", "y", "level"), stringsAsFactors=TRUE, colClasses=c("factor", "factor", "numeric")) df$level <- round(df$level*100, 0) # reorder cell type row-factors (in reverse of given order) df$y <- factor(df$y, levels=unique(df$y[length(df$y):1])) lowestValue <- min(df$level) secondHighestValue <- unique(sort(df$level, decreasing=TRUE))[2] n <- 10 col.seq <- seq(lowestValue, secondHighestValue, length.out=n) brks <- c(0, col.seq, Inf) cuts <- cut(df$level, breaks = brks) colors <- colorRampPalette(c("white", "red"))(length(levels(cuts))-1) colors <- c(colors, "black") cls <- rep(colors, times = table(cuts)) library(lattice) trellis.device(dev=pdf, file=pdfFn) fig <- levelplot(cuts~x*y, data = df, cuts = n, col.regions=cls, xlab="", ylab="", aspect="iso", scales=list( x=list(rot=90) ), panel=function(...) { arg <- list(...) panel.levelplot(...) panel.text(df$x, df$y, df$level, cex=0.5) }, colorkey=list(col=colorRampPalette(c("white", "red"))(length(col.seq)), at=col.seq) ) print(fig) graphics.off()

vFinal

如果我将levelplot增加到n以上,则细胞着色会再次中断,返回到鲜红色的对角线,而不是黑色(如图所示)。

1 个答案:

答案 0 :(得分:5)

这是第3版

我们再去(再次)。 :)

这很奇怪,如果我把n设置为低于15的东西,事情似乎有用吗?

enter image description here

df <- read.table("http://dl.dropbox.com/u/31495717/stackoverflow.overlaps.list.txt",
        sep = "\t", header = FALSE)
names(df) <- c("x", "y", "level")
df$level <- round(df$level*100, 0)

n <- 10
col.seq <- seq(10, 80, length.out = n)
brks <- c(0, seq(10, 80, length.out = n), 100)
cuts <- cut(df$level, breaks = brks)
colors <- colorRampPalette(c("red", "white"))(length(levels(cuts))-1)
colors <- c(colors, "black")

cls <- rep(colors, times = table(cuts))

print(levelplot(cuts~x*y,
                data = df,
                cuts = n,
                col.regions=cls,
                xlab="",
                ylab="",
                aspect="iso",
                scales=list(
                        x=list(rot=90)
                ),
                panel=function(...) {
                    arg <- list(...)
                    panel.levelplot(...)
                    panel.text(df$x, df$y, df$level, cex=0.5)
                },
                colorkey = list(col = colors, at = brks)
        ))
相关问题