r - 将日期添加到日历热图 R

标签 r plot calendar heatmap lattice

我正在使用 Paul Bleicher's calenderHeat.R code 绘制日历热图这是我的情节:

我想知道如何将日期添加到绘图中,而不是空白网格。这是我的示例数据:

structure(list(Date = c("2014-10-01", "2014-09-30", "2014-09-29", 
"2014-09-26", "2014-09-25", "2014-09-24", "2014-09-23", "2014-09-22", 
"2014-09-19", "2014-09-18", "2014-09-17", "2014-09-16", "2014-09-15", 
"2014-09-12", "2014-09-11", "2014-09-10", "2014-09-09", "2014-09-08", 
"2014-09-05", "2014-09-04", "2014-09-03", "2014-09-02", "2014-08-29", 
"2014-08-28", "2014-08-27", "2014-08-26", "2014-08-25", "2014-08-22", 
"2014-08-21", "2014-08-20", "2014-08-19", "2014-08-18", "2014-08-15", 
"2014-08-14", "2014-08-13", "2014-08-12", "2014-08-11", "2014-08-08", 
"2014-08-07", "2014-08-06", "2014-08-05", "2014-08-04", "2014-08-01", 
"2014-07-31", "2014-07-30", "2014-07-29", "2014-07-28", "2014-07-25", 
"2014-07-24", "2014-07-23", "2014-07-22", "2014-07-21", "2014-07-18", 
"2014-07-17", "2014-07-16", "2014-07-15", "2014-07-14", "2014-07-11", 
"2014-07-10", "2014-07-09", "2014-07-08", "2014-07-07", "2014-07-03", 
"2014-07-02", "2014-07-01", "2014-06-30", "2014-06-27", "2014-06-26", 
"2014-06-25", "2014-06-24", "2014-06-23", "2014-06-20", "2014-06-19", 
"2014-06-18", "2014-06-17", "2014-06-16", "2014-06-13", "2014-06-12", 
"2014-06-11", "2014-06-10", "2014-06-09", "2014-06-06", "2014-06-05", 
"2014-06-04", "2014-06-03", "2014-06-02", "2014-05-30", "2014-05-29", 
"2014-05-28", "2014-05-27", "2014-05-23", "2014-05-22", "2014-05-21", 
"2014-05-20", "2014-05-19", "2014-05-16", "2014-05-15", "2014-05-14", 
"2014-05-13", "2014-05-12", "2014-05-09", "2014-05-08", "2014-05-07", 
"2014-05-06", "2014-05-05", "2014-05-02", "2014-05-01", "2014-04-30", 
"2014-04-29", "2014-04-28", "2014-04-25", "2014-04-24", "2014-04-23", 
"2014-04-22", "2014-04-21", "2014-04-17", "2014-04-16", "2014-04-15", 
"2014-04-14", "2014-04-11", "2014-04-10", "2014-04-09", "2014-04-08", 
"2014-04-07", "2014-04-04", "2014-04-03", "2014-04-02", "2014-04-01", 
"2014-03-31", "2014-03-28", "2014-03-27", "2014-03-26", "2014-03-25", 
"2014-03-24", "2014-03-21", "2014-03-20", "2014-03-19", "2014-03-18", 
"2014-03-17", "2014-03-14", "2014-03-13", "2014-03-12", "2014-03-11", 
"2014-03-10", "2014-03-07", "2014-03-06", "2014-03-05", "2014-03-04", 
"2014-03-03", "2014-02-28", "2014-02-27", "2014-02-26", "2014-02-25", 
"2014-02-24", "2014-02-21", "2014-02-20", "2014-02-19", "2014-02-18", 
"2014-02-14", "2014-02-13", "2014-02-12", "2014-02-11", "2014-02-10", 
"2014-02-07", "2014-02-06", "2014-02-05", "2014-02-04", "2014-02-03", 
"2014-01-31", "2014-01-30", "2014-01-29", "2014-01-28", "2014-01-27", 
"2014-01-24", "2014-01-23", "2014-01-22", "2014-01-21", "2014-01-17", 
"2014-01-16", "2014-01-15", "2014-01-14", "2014-01-13", "2014-01-10", 
"2014-01-09", "2014-01-08", "2014-01-07", "2014-01-06", "2014-01-03", 
"2014-01-02", "2013-12-31", "2013-12-30", "2013-12-27", "2013-12-26", 
"2013-12-24", "2013-12-23", "2013-12-20", "2013-12-19", "2013-12-18", 
"2013-12-17", "2013-12-16", "2013-12-13", "2013-12-12", "2013-12-11", 
"2013-12-10", "2013-12-09", "2013-12-06", "2013-12-05", "2013-12-04", 
"2013-12-03", "2013-12-02", "2013-11-29", "2013-11-27", "2013-11-26", 
"2013-11-25", "2013-11-22", "2013-11-21", "2013-11-20", "2013-11-19", 
"2013-11-18", "2013-11-15", "2013-11-14", "2013-11-13", "2013-11-12", 
"2013-11-11", "2013-11-08", "2013-11-07", "2013-11-06", "2013-11-05", 
"2013-11-04", "2013-11-01", "2013-10-31", "2013-10-30", "2013-10-29", 
"2013-10-28", "2013-10-25", "2013-10-24", "2013-10-23", "2013-10-22", 
"2013-10-21", "2013-10-18", "2013-10-17", "2013-10-16", "2013-10-15", 
"2013-10-14", "2013-10-11", "2013-10-10", "2013-10-09", "2013-10-08", 
"2013-10-07", "2013-10-04", "2013-10-03", "2013-10-02", "2013-10-01", 
"2013-09-30", "2013-09-27", "2013-09-26", "2013-09-25", "2013-09-24", 
"2013-09-23", "2013-09-20", "2013-09-19", "2013-09-18", "2013-09-17", 
"2013-09-16", "2013-09-13", "2013-09-12", "2013-09-11", "2013-09-10", 
"2013-09-09", "2013-09-06", "2013-09-05", "2013-09-04", "2013-09-03", 
"2013-08-30", "2013-08-29", "2013-08-28", "2013-08-27", "2013-08-26", 
"2013-08-23", "2013-08-22", "2013-08-21", "2013-08-20", "2013-08-19", 
"2013-08-16", "2013-08-15", "2013-08-14", "2013-08-13", "2013-08-12"
), Adj.Close = c(45.9, 46.36, 46.44, 46.41, 46.04, 47.08, 46.56, 
47.06, 47.52, 46.68, 46.52, 46.76, 46.24, 46.7, 47, 46.84, 46.76, 
46.47, 45.91, 45.26, 44.96, 45.09, 45.43, 44.88, 44.87, 45.01, 
45.17, 45.15, 45.22, 44.95, 45.33, 44.83, 44.51, 44, 43.81, 43.25, 
42.93, 42.93, 42.96, 42.47, 42.81, 43.1, 42.59, 42.89, 43.31, 
43.62, 43.7, 44.22, 44.12, 44.59, 44.55, 44.56, 44.41, 44.25, 
43.81, 42.19, 41.88, 41.83, 41.43, 41.41, 41.52, 41.73, 41.54, 
41.64, 41.61, 41.44, 41.99, 41.46, 41.77, 41.49, 41.73, 41.42, 
41.25, 41.39, 41.42, 41.24, 40.97, 40.33, 40.61, 40.85, 41.01, 
41.22, 40.95, 40.07, 40.04, 40.54, 40.69, 40.09, 39.76, 39.94, 
39.87, 39.85, 40.1, 39.43, 39.5, 39.58, 39.35, 39.99, 40.17, 
39.44, 39.02, 39.12, 38.9, 38.55, 38.91, 39.17, 39.47, 39.87, 
39.98, 40.33, 39.38, 39.34, 39.17, 39.46, 39.41, 39.48, 39.87, 
39.23, 38.66, 38.69, 38.84, 39.94, 39.3, 39.28, 39.34, 40.47, 
40.81, 40.87, 40.45, 39.77, 38.84, 39.27, 39.81, 39.97, 39.63, 
39.8, 38.75, 39.03, 37.55, 37.2, 37.39, 37.77, 37.52, 37.32, 
37.4, 37.65, 37.61, 37.9, 37.28, 37.81, 37.36, 36.98, 37.05, 
37.19, 37.48, 37.25, 37.02, 36.93, 36.85, 36.84, 36.7, 36.41, 
36.05, 35.81, 35.44, 35.09, 35.6, 35.73, 37.06, 36.1, 35.91, 
35.53, 35.29, 36.05, 35.32, 35.19, 35.43, 35.63, 36.13, 36.01, 
35.05, 34.26, 35.3, 34.8, 35.03, 35.66, 35.39, 36.15, 36.4, 36.64, 
36.53, 36.53, 36.67, 36.32, 35.87, 36.05, 35.51, 35.83, 35.77, 
36.13, 35.94, 36.46, 36.84, 37.33, 37.92, 37.57, 37.22, 38.14, 
37.52, 37.66, 37.35, 36.83, 36.58, 36.87, 36.8, 36.63, 36.32, 
35.99, 36.16, 36.78, 36.96, 37.1, 36.32, 36.54, 36.73, 36.45, 
37.12, 35.62, 34.94, 34.54, 34.42, 34.55, 34.53, 34.58, 34.73, 
32.78, 32.82, 33.62, 34.01, 33.99, 33.95, 33.67, 33.53, 33.49, 
33.18, 32.82, 32.15, 32.09, 32.37, 32.94, 32.92, 32.97, 32.64, 
32.35, 32.34, 31.86, 31.6, 31.55, 31.83, 31.88, 32.7, 32.39, 
32.01, 31.89, 32.11, 31.78, 31.83, 31.49, 30.78, 30.28, 30.36, 
30.33, 30.99, 32.47, 32.61, 32.1, 32.33, 33.2, 33.78, 31.49, 
30.73, 30.74, 30.51, 30.91, 30.9, 31.45, 31.33, 31.73)), .Names = c("Date", 
"Adj.Close"), class = "data.frame", row.names = c(NA, -288L))

以及生成绘图的代码:

calendarHeat(stock.data$Date, stock.data$Adj.Close, varname="MSFT Adjusted Close")

我在互联网上进行了搜索并自己尝试过,但我得到的最接近的解决方案是添加如下字母的解决方案:

p6 <- extra.calendarHeat(dates= stock.data$Date, values = stock.data$Adj.Close,
                     pvalues = stock.data$Volume,
                     varname="MSFT Adjusted Close  \n Volume as LETTERS symbols",
                     pch.symbol = letters,
                     color='r2b')

enter image description here

请帮我添加月份的日期。提前致谢。

最佳答案

这不是一个可扩展的函数。但是,您可以做一些手术来插入您喜欢的行为。假设您所在的系统可以从 https 地址获取文件,您可以这样做

source("https://raw.githubusercontent.com/iascchen/VisHealth/master/R/calendarHeat.R")

或者您可以使用httr

library(httr)
cat(content(GET("https://raw.githubusercontent.com/iascchen/VisHealth/master/R/calendarHeat.R"), "text"), file="calendarHeat.R")
source("calendarHeat.R")

这将为您提供该文件的原始版本。现在我们可以做一些改变

#copy
calendarHeat2<-calendarHeat

#insert line to calulate day number
bl<-as.list(body(calendarHeat2))
body(calendarHeat2) <- as.call(c(
    bl[1:14], 
    quote(caldat$dom <- as.numeric(format(caldat$date.seq, "%d"))),
    bl[-(1:14)]
))

#change call to level plot
lp<-as.list(body(calendarHeat2)[[c(32,2,3)]])
lp$dom <- quote(caldat$dom)
lp$panel <- quote(function(x,y,subscripts,dom,...) {
    panel.levelplot(x,y,subscripts=subscripts,...)
    panel.text(x[subscripts],y[subscripts],labels=dom[subscripts])
})
body(calendarHeat2)[[c(32,2,3)]]<-as.call(lp)

现在我们可以使用这个新版本的函数来添加日期名称

calendarHeat2(stock.data$Date, stock.data$Adj.Close, varname="MSFT Adjusted Close")

您可以通过更改我们上面创建的自定义面板函数来调整代码,以调整日期名称的打印。

enter image description here

当然,这个编辑是非常脆弱的。如果源函数发生任何变化,我们的手术可能会失败,因为我们正在按索引提取代码块。因此,为了安全起见,在让函数按照您喜欢的方式工作后,您可能应该 dump() 您的 calendarHeat2source() 版本> 根据需要。

关于r - 将日期添加到日历热图 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26171068/

相关文章:

r - 在传单簇选项中定义函数

r - 使用 ggvis R 为线图创建辅助 y 轴

Python fiddle 图

python - pandas 0.21.0 时间戳与 matplotlib 的兼容性问题

android - 如何自动将事件添加到日历?

r - 在 write 函数中 append 文本 [R]

r - 通过连续的冒号从字符串中提取字符

r - 在 survdiff 调用中使用 survfit 对象的公式

ajax - 日历中 primefaces ajax 上的事件日期选择不起作用

Android 日历获取当前日期不工作