在 r 中组合两个图 [英] combining two plots in r

查看:41
本文介绍了在 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...

推荐答案

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

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.

除此之外,我认为代码很好地展示了有多少网格函数与低级基础 图形功能.

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天全站免登陆