在R中将透明度为1的热图放置在另一个上 [英] Place 1 heatmap on another with transparency in R

查看:132
本文介绍了在R中将透明度为1的热图放置在另一个上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R新手,有以下挑战。

我想要创建一个基本上结合了2种热图的可视化文件,以便在什么时候可以看到真正黑暗的天空(用于天文学)。为此,我希望有一个基于月出和月落时间以及月亮相位的月亮亮度可视化热图。在这一点上,我们可以绘制一个带状热图,以便在太阳升起的时候获得一些透明度。
我不确定这是否会变得可见,或者如果我需要找到其他解决方案,但是这对于进一步融入R来说似乎是一个很好的挑战。
但是我可以使用一些指针,因为我已经用所有720个值加载了大小为24(小时)x 31(天)的矩阵。当试图从矢量创建一个基本的数据帧时,我得到了行数不一致的错误。此外,我还有一些热图已经在使用,但我不确定如何将它们中的两个组合到像我描述的那样的情节中。


$ b

作为excel中当前的'heatmap'的一个例子



以及一些数据:

 月结<  - 结构(列表(X1.9.12 =结构(c(2L,2L,2L,2L,2L,2L,
2L,2L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L, ,2L,
2L,2L),.Label = c(0%,100%),class =factor),X2.9.12 =结构(c(2L,
2L ,2L,2L,2L,2L,2L,2L,2L,2L,1L,1L,1L,1L,1L,1L,1L,
1L,1L,1L,1L,2L,2L,2L) .Label = c(0%,98%),class =factor),
X3.9.12 =结构(c(2L,2L,2L,2L,2L,2L,2L,2L ,2L,
2L,2L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L
),标签= c( ,94%),class =factor),X4.9.12 =结构(c(2L,
2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L ,1L,1L,1L,1L,
1L,1L,1L,1L,1L,1L,2L,2L),标签= c (2L,2L,2L,2L,2L,2L,2L,2L,2L,$)的结构(0%,89%),class =factor),
X5.9.12 = b $ b 2L,2L,2L,2L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L
),。标签= c(0%,82结构(c(2L,
2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L) ,1L,1L,
1L,1L,1L,1L,1L,1L,1L,2L),标签= c(0%,74%),class =factor b $ b X7.9.12 =结构(c(2L,2L,2L,2L,2L,2L,2L,2L,2L,
2L,2L,2L,2L,2L,2L,1L,1L,1L ,1L,1L,1L,1L,1L,1L
),。标签= c(0%,65%),等级=因子),X8.9.12 =结构(c ,
2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,
1L,1L,1L,1L,1L,1L ,1L,1L),.Label = c(0%,56%),class =factor),
X9.9.12 =结构(c(1L,1L,1L,1L,1L ,1L,1L,1L,1L,
1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L
) = c(0%,47%),class =factor),X10.9.12 = structure( c(2L,
1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,2L,2L,2L, 2L,2L,2L),.Label = c(0%,37%),class =factor),
X11.9.12 = structure(c(2L,2L,1L, 1L,1L,1L,1L,1L,1L,
1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L
) ,标记= c(0%,28%),等级=因子),X12.9.12 =结构(c(2L,
2L,2L,1L,1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,1L,2L,2L,2L,2L,2L,2L),标签= c(0% (2L,2L,2L,2L,1L,1L,1L,1L,1L,
1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L
),。标签= c(0%,12%)因子),X14.9.12 =结构(c(2L,
2L,2L,2L,2L,2L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,1L,1L,2L,2L,2L,2L,2L).Label = c(0%,6%),class =factor),
X15.9.12 =结构(c(2L,2L,2L,2L,2L,2L,2L,1L,1L,
1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L
),。标签= c(0%,2% (1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,1L,1L,1L,1L,1L,1L,1L),.Label =0%,class =factor),
X17.9.12 = structure(c(2L, 2L,2L,2L,2L,2L,2L,2L,2L,
2L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L, b $ b),.Label = c(0%,1%),class =factor)),.Names = c(X1.9.12,
X2.9.12, X3.9.12,X4.9.12,X5.9.12,X6.9.12,X7.9.12,
X8.9.12,X9.9.12,X10.9.12 ,X11.9.12,X12.9.12,X13.9.12,
X14.9.12,X15.9.12,X16.9.12,X17.9.12), =data.frame,row.names = c(0:00:00,
1:00:00,2:00:00,3:00:00, 4:00:00,5:00:00,6:00:00,
7:00:00,8:00:00,9:00:00 ,10:00:00,11:00:00,12:00:00,
13:00:00,14:00:00,15:00: 00,16:00:00,17:00:00,18:00:00,
19:00:00,20:00 :00,21:00:00,22:00:00,23:00:00))

SUN

  September 
日出日落
1 6:52 20:26
2 6:54 20:24
3 6:56 20:22
4 6:57 20:20
5 6:59 20:17
6 7:00 20:15
7 7:02 20:13
8 7:04 20:10
9 7:05 20:08
10 7:07 20:06
11 7:08 20:05
12 7:09 20:02
13 7:11 20:00
14 7:13 19 :58
15 7:14 19:55
16 7:16 19:53
17 7:17 19:51
18 7:19 19:48
19 7:21 19:46
20 7:22 19:44
21 7:25 19:40
22 7:26 19:38
23 7:28 19: 35
24 7:30 19:33
25 7:31 19:31
26 7:33 19:28
27 7:35 19:26
28 7:36 19:24
29 7:38 19:21
30 7:40 19:19


解决方案

因此,根据我的理解,基本上有两个问题:

数据组织



最简单的方法是,如果您将所有数据放入一个 data.frame 长格式。即对于时间和日期的每个组合,您都有一行,并为月亮和阳光强度增加了一些列。

因此,我们从 melt

开始。 code> ing和修复 moon
数据:

 (月亮,id.vars =时间,变量。名称(月亮)
月亮< date,value.name =moon)
moon $ date< - sub(X(。*),\\ 1,moon $ date)
moon $月份< - 1 - as.numeric(sub(%,,moon $ moon))/ 100

现在我们将 sun 数据带到一个可比较的表格中,至少给它们赋予日期相同的标识符:

  sun $ Day < -  paste(sun $ Day,9.12,sep =。)
pre>

下一步是 merge 数据由日期 resp。 Day ,并为太阳强度设置一个与月亮强度相同的列。这可以通过将时间转换为时间格式并使用实际时间比较 Sunrise Sunset 来完成: p>

  mdf < -  merge(moon,sun,by.x =date,by.y =Day)
mdf $ time.tmp< - strptime(mdf $ time,format =%H:%M)
mdf $ Sunrise < - round(strptime(mdf $ Sunrise,format =%H: %m),units =hours)
mdf $ Sunset < - round(strptime(mdf $ Sunset,format =%H:%M),units =hours)
mdf $ sun < - ifelse(mdf $ Sunrise <= mdf $ time.tmp& mdf $ Sunset> = mdf $ time.tmp,1,0)
mdf < - mdf [c(日期,时间,月亮,太阳)]

mdf [5:10,]
日期时间月亮太阳
1.9.12 4:00: 00 0 0
1.9.12 5:00:00 0 0
1.9.12 6:00:00 0 0
1.9.12 7:00:00 0 1
1.9 .12 8:00:00 1 1
1.9.12 9:00:00 1 1



< h2>绘图

添加不同透明度的多个图层的字面意思是 ggplot2 。为了以正确的方式使用它,还需要再进行一次数据操作,以确保轴上的正确顺序: date time 必须转换为因子 s,因子水平不是按照词汇顺序排列,而是按时间排序:

< (日期,水平=唯一(日期)[订单(as.Date(唯一(日期),日期) %d。%m。%y))])
time < - factor(time,levels = unique(time)[order ),递减= TRUE)])
})

现在可以绘制:

  library(ggplot2)
ggplot(data = mdf,aes(x = date,y = time))+
geom_tile(aes(alpha = sun),fill =goldenrod1)+
geom_tile(aes(alpha = moon),fill =dodgerblue3)+
scale_alpha_continuous(moon,范围= c(0,0.5))+
theme_bw()+
主题(axis.text.x = element_text(angle = 45,hjust = 1))

给您以下结果


I'm new to R and have the following challenge;

I want to create a visualization that basically combines 2 kind of 'heatmaps' in order to visualize at what times there are truly dark skies (for astronomy). For this I want to have a heatmap that visualizes the brightness of the moon based on the moonrise and moonset times and the phase of the moon. On this then we can plot a 'band'like heatmap for the time the sun is up with some transparency. I'm not sure if this is going to work visualy or if I need to find some other solution, however this seems like a good challenge to get into R some more. But I could use some pointers as I'm stuck already loading the matrix of size 24(hours) x 31(days) with all the 720 values. When trying to create a basic data.frame from the vectors I get the error that the number of rows are inconsistent.

Furthermore I have some heatmap examples working already, but I'm not sure how to combine 2 of them in the same plot like I described.

As an illustration the current 'heatmap' as it is in excel

And some data:

MOON

moon <- structure(list(X1.9.12 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L), .Label = c("0%", "100%"), class = "factor"), X2.9.12 = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("0%", "98%"), class = "factor"), 
    X3.9.12 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L
    ), .Label = c("0%", "94%"), class = "factor"), X4.9.12 = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c("0%", "89%"), class = "factor"), 
    X5.9.12 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L
    ), .Label = c("0%", "82%"), class = "factor"), X6.9.12 = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("0%", "74%"), class = "factor"), 
    X7.9.12 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
    ), .Label = c("0%", "65%"), class = "factor"), X8.9.12 = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0%", "56%"), class = "factor"), 
    X9.9.12 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
    ), .Label = c("0%", "47%"), class = "factor"), X10.9.12 = structure(c(2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("0%", "37%"), class = "factor"), 
    X11.9.12 = structure(c(2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L
    ), .Label = c("0%", "28%"), class = "factor"), X12.9.12 = structure(c(2L, 
    2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("0%", "20%"), class = "factor"), 
    X13.9.12 = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L
    ), .Label = c("0%", "12%"), class = "factor"), X14.9.12 = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("0%", "6%"), class = "factor"), 
    X15.9.12 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L
    ), .Label = c("0%", "2%"), class = "factor"), X16.9.12 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "0%", class = "factor"), 
    X17.9.12 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L
    ), .Label = c("0%", "1%"), class = "factor")), .Names = c("X1.9.12", 
"X2.9.12", "X3.9.12", "X4.9.12", "X5.9.12", "X6.9.12", "X7.9.12", 
"X8.9.12", "X9.9.12", "X10.9.12", "X11.9.12", "X12.9.12", "X13.9.12", 
"X14.9.12", "X15.9.12", "X16.9.12", "X17.9.12"), class = "data.frame", row.names = c("0:00:00", 
"1:00:00", "2:00:00", "3:00:00", "4:00:00", "5:00:00", "6:00:00", 
"7:00:00", "8:00:00", "9:00:00", "10:00:00", "11:00:00", "12:00:00", 
"13:00:00", "14:00:00", "15:00:00", "16:00:00", "17:00:00", "18:00:00", 
"19:00:00", "20:00:00", "21:00:00", "22:00:00", "23:00:00"))

SUN

    September   
Day Sunrise Sunset
1   6:52    20:26
2   6:54    20:24
3   6:56    20:22
4   6:57    20:20
5   6:59    20:17
6   7:00    20:15
7   7:02    20:13
8   7:04    20:10
9   7:05    20:08
10  7:07    20:06
11  7:08    20:05
12  7:09    20:02
13  7:11    20:00
14  7:13    19:58
15  7:14    19:55
16  7:16    19:53
17  7:17    19:51
18  7:19    19:48
19  7:21    19:46
20  7:22    19:44
21  7:25    19:40
22  7:26    19:38
23  7:28    19:35
24  7:30    19:33
25  7:31    19:31
26  7:33    19:28
27  7:35    19:26
28  7:36    19:24
29  7:38    19:21
30  7:40    19:19

解决方案

So from what I understood, there are basically two questions:

Data organization

The easiest would be, if you'd have all data in one data.frame in long format. I.e. for each combination of time and date you have one row, with additional columns for the moon and sun intensity.

So we start with melting and fixing the moon data:

library(reshape2)
moon$time <- row.names(moon)
moon <- melt(moon, id.vars="time", variable.name="date", value.name="moon" )
moon$date <- sub("X(.*)", "\\1", moon$date)
moon$moon <- 1 - as.numeric(sub("%", "", moon$moon)) /100

Now we bring the sun data to an comparable form, by at least give them the same identifier for the date:

sun$Day <- paste( sun$Day, "9.12", sep  ="." )

Next step is to merge the data by the date resp. Day and to set a comparable column for the sun intensity as is given already for the moon intensity. This can be done by casting the times to a time format and compare Sunrise and Sunset with the actual time:

mdf <- merge( moon, sun, by.x = "date", by.y = "Day" )
mdf$time.tmp <- strptime(mdf$time, format="%H:%M")
mdf$Sunrise  <- round(strptime(mdf$Sunrise, format="%H:%M"), units = "hours")
mdf$Sunset   <- round(strptime(mdf$Sunset, format="%H:%M"), units = "hours")
mdf$sun <- ifelse( mdf$Sunrise <= mdf$time.tmp & mdf$Sunset >= mdf$time.tmp, 1, 0 )
mdf <- mdf[c("date", "time", "moon", "sun")]

mdf[ 5:10, ]
  date    time moon sun
1.9.12 4:00:00    0   0
1.9.12 5:00:00    0   0
1.9.12 6:00:00    0   0
1.9.12 7:00:00    0   1
1.9.12 8:00:00    1   1
1.9.12 9:00:00    1   1

Plotting

Adding multiple layers with different transparencies begs literally for ggplot2. In order to use this in a proper way, there is one more data manipulation necessary, which ensures the proper order on the axes: date and time have to be converted to factors with factor levels ordered not lexically, but by time:

mdf <- within( mdf, {
  date <- factor( date, levels=unique(date)[ order(as.Date( unique(date), "%d.%m.%y" ) ) ] )
  time <- factor( time,  levels=unique(time)[ order(strptime( time, format="%H:%M:%S"), decreasing=TRUE ) ] )
} )

This can be plot now:

library( ggplot2 )
ggplot( data = mdf, aes(x = date, y = time )  ) + 
  geom_tile( aes( alpha = sun  ), fill = "goldenrod1"  ) +
  geom_tile( aes( alpha = moon ), fill = "dodgerblue3" ) +
  scale_alpha_continuous( "moon", range=c(0,0.5) ) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Which gives you the following result

这篇关于在R中将透明度为1的热图放置在另一个上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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