0

我是 R 新手,面临以下挑战;

我想创建一个基本上结合了两种“热图”的可视化,以便可视化什么时候有真正的黑暗天空(用于天文学)。为此,我想要一个热图,根据月出和月落时间以及月相来可视化月亮的亮度。在此基础上,我们可以为太阳升起的时间绘制一个类似“带状”的热图,并具有一定的透明度。我不确定这是否会在视觉上起作用,或者我是否需要找到其他解决方案,但这似乎是更多地进入 R 的一个很好的挑战。但是我可以使用一些指针,因为我已经被困在使用所有 720 个值加载大小为 24(小时)x 31(天)的矩阵。尝试从向量创建基本 data.frame 时,我收到行数不一致的错误。

此外,我已经有一些热图示例正在工作,但我不确定如何将其中的 2 个组合在同一个图中,就像我描述的那样。

作为说明,当前的“热图”在 excel 中

来自excel的当前热图

还有一些数据:

月亮

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"))

太阳

    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
4

1 回答 1

4

所以据我了解,基本上有两个问题:

数据组织

最简单的方法是,如果您将所有数据都放在一个data.frame长格式中。即,对于时间和日期的每种组合,您都有一行,另外还有月亮和太阳强度的列。

所以我们从ingmelt修复moon数据开始:

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

现在我们将sun数据转换为可比较的形式,至少为它们提供相同的日期标识符:

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

下一步是merge通过相应的数据dateDay并为太阳强度设置一个可比较的列,就像已经为月球强度给出的那样。这可以通过将时间转换为时间格式并与实际时间进行比较来Sunrise完成Sunset

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

绘图

添加具有不同透明度的多个图层实际上要求ggplot2. 为了以正确的方式使用它,还需要进行一次数据操作,以确保轴上的正确顺序:date并且time必须转换为factors ,其中因子级别不是按词法排序,而是按时间排序:

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 ) ] )
} )

现在可以绘制:

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))

这给了你以下结果

在此处输入图像描述

于 2013-06-11T19:05:07.557 回答