结合R中的两个情节 [英] combining two plots in r

查看:122
本文介绍了结合R中的两个情节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我打算合并的两个地块:

Here are two plots I intend to combine:

首先是热图图的一半矩阵. .....................

First is half matrix of heatmap plot. ..............................

# plot 1 , heatmap plot
set.seed (123)
 myd <- data.frame ( matrix(sample (c(1, 0, -1), 500, replace = "T"), 50))

mmat <-  cor(myd)
diag(mmat) <- NA
mmat[upper.tri (mmat)] <- NA
heatmap (mmat, keep.dendro = F, Rowv = NA, Colv = NA)

我需要隐藏x和y列中的名称,并将它们放在对角线中.

I need to suppress the names in x and y columns and put them in diagonal.

第二个图,请注意,第一个图的名称/标签与第二个图(x1至X10)中的名称相对应:

The second plot, please note that names / labels in first plot corresponds name in second plot (x1 to X10):

  vard <- data.frame ( position = c(1, 10, 15, 18, 20, 23, 24, 30, 35, 40), 
          Names =paste ("X", 1:10, sep = ""))
    plot(vard$position, vard$position - vard$position,
                type = "n", axes = FALSE, xlab = "", ylab = NULL, yaxt = "n")
    polygon(c(0, max(vard$position + 0.08 * max(vard$position)),
                max(vard$position) + 0.08 * max(vard$position),
                0), 0.2 * c(-0.3, -0.3, 0.3, 0.3), col = "green4")
    segments(vard$position, -0.3, vard$position,                0.3)
    text(vard$position, 0.7, vard$position,
                    srt = 90)
    text(vard$position, -0.7, vard$Names)

我打算旋转第一个图,以便X1到X10应该与第二个图相同,并且第二个图和第一个图之间的标签之间存在联系.输出如下:

I intend rotate the first plot so that X1 to X10 should correspond to the same in the second plot and there is connection between labels in second plot to first plot. The output would look like:

我怎样才能做到这一点 ?

How can I do this ?

编辑:基于有关add = TRUE的注释.我正在尝试将多边形添加到热图图,如下所示.但是我找不到坐标..这种策略图可以稍后再翻转实际图形...非常感谢...

Edits: based on comments about add = TRUE....I am trying to add polygon to the heatmap plot, the like follows. But I could not find coordinates ..The strategy plot this way and flip the actual figure later...help much appreciated...

推荐答案

这是一个完全基于 grid 的解决方案.唯一真正涉及的位是函数convertToColors();它采用一个数值矩阵(可能包括NA)并将其转换为sRGB颜色字符串(例如"#FFFFFF),以红色到白色heat.colors()的比例表示颜色.红色表示矩阵中的最小值,白色表示最大值,NA是透明的.

Here's a fully grid-based solution. The only really involved bit is the function convertToColors(); it takes a numeric matrix (possibly including NAs) and converts it to sRGB color strings (e.g. "#FFFFFF) representing colors on a red-to-white heat.colors() scale. Red corresponds to the minimum value in the matrix, white corresponds to the maximum value, and NAs are transparent.

除此之外,我认为代码可以很好地显示出多少 grid 功能与底层 base 图形功能.

Other than that, I think the code does a decent job of showing how many grid functions are no more complicated, and considerably more consistent and flexible, than the low-level base graphics functions.

library(grid)

## Data: heatmap
set.seed (123)
myd <- data.frame ( matrix(sample (c(1, 0, -1), 500, replace = "T"), 50))
mmat <-  cor(myd)
diag(mmat) <- NA
mmat[upper.tri (mmat)] <- NA
## Data: Positions
vard <- c(1, 10, 15, 18, 20, 23, 24, 30, 35, 40)

## Construct a function to convert a numeric matrix to a matrix of color names.
## The lowest value in the matrix maps to red, the highest to white,
## and the NAs to "transparent".
convertToColors <- function(mat) {
    # Produce 'normalized' version of matrix, with values ranging from 0 to 1
    rng <- range(mat, na.rm = TRUE)
    m <- (mat - rng[1])/diff(rng)
    # Convert to a matrix of sRGB color strings
    m2 <- m; class(m2) <- "character"
    m2[!is.na(m2)] <- rgb(colorRamp(heat.colors(10))(m[!is.na(m)]), max = 255)
    m2[is.na(m2)] <- "transparent"
    return(m2)
}

## Initialize plot and prepare two viewports
grid.newpage()
heatmapViewport <- viewport(height=1/sqrt(2), width=1/sqrt(2), angle = -135) 
annotationViewport <- viewport(y = 0.7, height = 0.4)

## Plot heat map
pushViewport(heatmapViewport)
    grid.raster(t(convertToColors(mmat)), interpolate = FALSE)
upViewport()

## Precompute x-locations of text and segment elements
n <- nrow(mmat)
v_x <- vard/max(vard)
X_x <- seq(0, 1, len=n)

## Plot the annotated green bar and line segments
pushViewport(annotationViewport)
    ## Green rectangle
    grid.polygon(x = c(0,0,1,1,0), y = c(.45,.55,.55,.45,.45),
                 gp = gpar(fill = "green4"))
    pushViewport(viewport(width = (n-1)/n))
        ## Segments and text marking vard values
        grid.segments(x0 = v_x, x1 = v_x, y0 = 0.3, y1 = 0.7)
        grid.text(label = vard, x = v_x, y = 0.75, rot = 90)
        ## Text marking heatmap column names (X1-X10)
        grid.text(paste0("X", seq_along(X_x)), x = X_x, y=0.05,
                  gp = gpar(fontface="bold"))
        ## Angled lines
        grid.segments(x0 = v_x, x1 = X_x, y0 = 0.29, y1 = 0.09)
    upViewport()
upViewport()

这篇关于结合R中的两个情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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