繁体   English   中英

r中的绘图板布局热图

[英]plot plate layout heatmap in r

我试图用R绘制板布局热图。板布局只是8(行)×12(列)圆(井)。 行由字母和数字标记。 每个井需要填充一些颜色强度取决于定性或定量变量。 板块布局如下所示:

在此输入图像描述

这是一个小数据集:

 set.seed (123)
platelay <- data.frame (rown = rep (letters[1:8], 12), coln = rep (1:12, each = 8),
colorvar = rnorm (96, 0.3, 0.2))

   rown coln     colorvar
1     a    1  0.187904871
2     b    1  0.253964502
3     c    1  0.611741663
4     d    1  0.314101678
5     e    1  0.325857547
6     f    1  0.643012997
7     g    1  0.392183241
8     h    1  0.046987753
9     a    2  0.162629430
10    b    2  0.210867606
11    c    2  0.544816359
12    d    2  0.371962765
13    e    2  0.380154290
14    f    2  0.322136543
15    g    2  0.188831773
16    h    2  0.657382627
17    a    3  0.399570096
18    b    3 -0.093323431
19    c    3  0.440271180
20    d    3  0.205441718
21    e    3  0.086435259
22    f    3  0.256405017
23    g    3  0.094799110
24    h    3  0.154221754
25    a    4  0.174992146
26    b    4 -0.037338662
27    c    4  0.467557409
28    d    4  0.330674624
29    e    4  0.072372613
30    f    4  0.550762984
31    g    4  0.385292844
32    h    4  0.240985703
33    a    5  0.479025132
34    b    5  0.475626698
35    c    5  0.464316216
36    d    5  0.437728051
37    e    5  0.410783531
38    f    5  0.287617658
39    g    5  0.238807467
40    h    5  0.223905800
41    a    6  0.161058604
42    b    6  0.258416544
43    c    6  0.046920730
44    d    6  0.733791193
45    e    6  0.541592400
46    f    6  0.075378283
47    g    6  0.219423033
48    h    6  0.206668929
49    a    7  0.455993024
50    b    7  0.283326187
51    c    7  0.350663703
52    d    7  0.294290649
53    e    7  0.291425909
54    f    7  0.573720457
55    g    7  0.254845803
56    h    7  0.603294121
57    a    8 -0.009750561
58    b    8  0.416922750
59    c    8  0.324770849
60    d    8  0.343188314
61    e    8  0.375927897
62    f    8  0.199535309
63    g    8  0.233358523
64    h    8  0.096284923
65    a    9  0.085641755
66    b    9  0.360705728
67    c    9  0.389641956
68    d    9  0.310600845
69    e    9  0.484453494
70    f    9  0.710016937
71    g    9  0.201793767
72    h    9 -0.161833775
73    a   10  0.501147705
74    b   10  0.158159847
75    c   10  0.162398277
76    d   10  0.505114274
77    e   10  0.243045399
78    f   10  0.055856458
79    g   10  0.336260696
80    h   10  0.272221728
81    a   11  0.301152837
82    b   11  0.377056080
83    c   11  0.225867994
84    d   11  0.428875310
85    e   11  0.255902688
86    f   11  0.366356393
87    g   11  0.519367803
88    h   11  0.387036298
89    a   12  0.234813683
90    b   12  0.529761524
91    c   12  0.498700771
92    d   12  0.409679392
93    e   12  0.347746347
94    f   12  0.174418785
95    g   12  0.572130490
96    h   12  0.179948083

有包装可以随时做吗? 是否可以在base或ggplot2或其他可以实现此目标的包中编写函数。

使用ggplot2更改足够大小的点的颜色。 注意我已经实现了@ TylerRinkler的建议,但是在对ggplot的调用中。 我也删除了轴标签

ggplot(platelay, aes(y = factor(rown, rev(levels(rown))),x = factor(coln))) + 
     geom_point(aes(colour = colorvar), size =18)  +theme_bw() +
     labs(x=NULL, y = NULL)

在此输入图像描述

还有一个基本图形方法,它可以让你的x轴高于图

# plot with grey colour dictated by rank, no axes or labels
with(platelay, plot( x=as.numeric(coln), y= rev(as.numeric(rown)), pch= 19, cex = 2, 
 col = grey(rank(platelay[['colorvar']] ) / nrow(platelay)), axes = F, xlab= '', ylab = ''))
# add circular outline
with(platelay, points( x=as.numeric(coln), y= rev(as.numeric(rown)), pch= 21, cex = 2))
# add the axes
axis(3, at =1:12, labels = 1:12)
axis(2, at = 1:8, labels = LETTERS[8:1])
# the background grid
grid()
# and a box around the outside
box()

在此输入图像描述

对于笑声和圣诞节的欢呼,这里有一个使用基本R绘图功能的版本。 虽然很可能有更好的解决方案。

dev.new(width=6,height=4)

rown <- unique(platelay$rown)
coln <- unique(platelay$coln)

plot(NA,ylim=c(0.5,length(rown)+0.5),xlim=c(0.5,length(coln)+0.5),ann=FALSE,axes=FALSE)
box()

axis(2,at=seq_along(rown),labels=rev(rown),las=2)
axis(3,at=seq_along(coln),labels=coln)

colgrp <- findInterval(platelay$colorvar,seq(min(platelay$colorvar),max(platelay$colorvar),length.out=10))
colfunc <- colorRampPalette(c("green", "blue"))
collist <- colfunc(length(unique(colgrp))) 

symbols(platelay$coln,
        factor(platelay$rown, rev(levels(platelay$rown))),
        circles=rep(0.2,nrow(platelay)),
        add=TRUE,
        inches=FALSE,
        bg=collist[colgrp])

结果图像:

在此输入图像描述

这里是使用@mnel和网格解决方案的ggplot2解决方案的解决方案

在此输入图像描述

这里是给定解决方案的代码

d <- ggplot(platelay, aes(y=rown,x=factor(coln))) + 
  geom_point(aes(colour = colorvar), size =18) + theme_bw()

我使用ggplot生成的数据

data <- ggplot_build(d)$data[[1]]

 x <- data$x
 y <- data$y
 grid.newpage()
 pushViewport(plotViewport(c(4, 4, 2, 2)),
               dataViewport(x, y))

网格有一个椭圆形的geom

 grid.ellipse(x, y,size=20,  ar = 2,angle=0,gp =gpar(fill=data$colour))
 grid.xaxis(at=c(labels=1:12,ticks=NA),gp=gpar(cex=2))
 grid.yaxis(at = 1:8,label=rev(LETTERS[1:8]),gp=gpar(cex=2))

 grid.roundrect(gp=gpar(fill=NA))

我添加网格:

gpgrid <- gpar(col='grey',lty=2,col='white')
grid.segments(unit(1:12, "native") ,unit(0, "npc"), unit(1:12, "native"),unit(1, "npc"),gp=gpgrid)
grid.segments(unit(0, "npc"), unit(1:8, "native"), unit(1, "npc"),unit(1:8, "native"),gp=gpgrid)
upViewport()

这个答案是@thelatemail答案的补充,它解释了(8,12)= 96格式的平面图。

为了构造(32,48)= 1536格式,AZ的单个数字是不充分的。 因此,需要扩展诸如AA,AB,AC,AD ... ZZ之类的字母,并且可以通过将字母连接到如下的级别变量来扩展到三个或更多个数字。

levels = c(LETTERS, c(t(outer(LETTERS, LETTERS, paste, sep = "")))))

@thelatemail答案可以改进为1536版格式的双位数字,如下所示

rown = rep (c(LETTERS, c(t(outer(LETTERS[1], LETTERS[1:6], paste, sep = "")))),

symbols(platelay$coln,
        factor(platelay$rown, 
               levels = rev(c(LETTERS, c(t(outer(LETTERS[1], LETTERS[1:6], paste, sep = "")))))),
        circles=rep(0.45,nrow(platelay)),
        add=TRUE,
        inches=FALSE,
        bg=collist[colgrp])

符号函数内的级别变量应该具有按字母顺序排序的字符,然后是double,然后是triple ...等等。

例如,如果符号函数中的级别低于错误的级别顺序,则它将使用不正确的颜色表示进行绘图。

订单不正确:A,AA,AB,AC,AD,AE,AF,B,C,D,... Z

正确的顺序:A,B,C,D,E,...... Z,AA,AB,AC,AD,AE,AF

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM