日历时间系列与R [英] Calendar Time Series with R
本文介绍了日历时间系列与R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用ggplot2制作这日历时间系列图表?
解决方案
#使日历时间系列图
pre>
#在屏幕上呈现的版本可能看起来超出规模,保存的版本应该更好
CalendarTimeSeries < - function(
DateVector = 1,
ValueVector = c(1,2),
SaveToDisk = FALSE
){
if(length(DateVector)!= length(ValueVector)){
stop('DateVector length不同于ValueVector length')
}
require(ggplot2)
require(scales)
require(data。表)
#预处理============================ ==============================
DateValue< - data.table(
ObsDate = DateVector,
IndexValue = ValueVector
)
DateValue [,Yr:= as.integer(strftime(ObsDate,'%Y'))]
DateValue [,MthofYr:= as.integer(strftime(ObsDate,'%m'))]
DateValue [,WkofYr:= 1 + as.integer(strftime(ObsDate,'%W'))]
DateValue [,DayofWk:= as.integer(strftime(ObsDate,'%w'))]
DateValue [DayofWk == 0L,DayofWk:= 7L]
#Heatmap-ish布局以粉碎日期上的颜色块====== =======
p1< - ggplot(
data = DateValue [,list(WkofYr,DayofWk)],
aes(
x = WkofYr ,
y = DayofWk
)
)+
geom_tile(
data = DateValue,
aes(
fill = IndexValue
) ,
color ='black'
)+
scale_fill_continuous(low =green,high =red)+
theme_bw b $ b plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank
)+
facet_grid(。〜Yr,drop = TRUE,scales ='free_x',space ='free_x')
#为月份更改添加边框================= =====================
#垂直边框(跨周)------------ --------------------------
setkeyv(DateValue,c(Yr,DayofWk,WkofYr ,MthofYr))
DateValue [,MonthChange:= c(0,diff(MthofYr))]
MonthChangeDatasetAcrossWks < - DateValue [MonthChange == 1]
MonthChangeDatasetAcrossWks [,WkofYr:= WkofYr - 0.5]
if(nrow(MonthChangeDatasetAcrossWks)> 0){
p1< - p1 +
geom_tile(
data = MonthChangeDatasetAcrossWks,
color ='black',
width = .2
)
}
#水平边框(一周内)----------------------------- ------
setkeyv(DateValue,c(Yr,WkofYr,DayofWk,MthofYr))
DateValue [,MonthChange:= c ,diff(MthofYr))]
MonthChangeDatasetWithinWk < - DateValue [MonthChange == 1& (!DayofWk%in%c(1))]
#MonthChangeDatasetWithinWk < - DateValue [MonthChange == 1]
MonthChangeDatasetWithinWk [,DayofWk:= DayofWk - 0.5]
if(nrow(MonthChangeDatasetWithinWk)> 0){
p1 <-p1 +
geom_tile(
data = MonthChangeDatasetWithinWk,
color ='black',
width = 1,
height = .2
)
}
b
$ b#添加轴标签和排序Y轴Mon-Sun ==========================
MonthLabels < - DateValue [,
list(meanWkofYr = mean(WkofYr)),
by = c('MthofYr')
]
MonthLabels [,MthofYr := month.abb [MthofYr]]
p1 < - p1 +
scale_x_continuous(
breaks = MonthLabels [,meanWkofYr],
labels = MonthLabels [,MthofYr],
expand = c(0,0)
)+
scale_y_continuous(
trans ='reverse',
breaks = c(1:7),
label = c('Mon','Tue','Wed','Thu','Fri','Sat','Sun'),
expand = c(0,0)
)
#如果要求,请保存到磁盘=========== ================================
if(SaveToDisk){
ScalingFactor = 10
ggsave(
p1,
file ='CalendarTimeSeries.png',
height = ScalingFactor * 7,
width = ScalingFactor * 2.75 * nrow DateValue [,list(Yr,MthofYr)])),
units ='mm'
)
}
p1
}
#一些数据
VectorofDates = seq(
as.Date(1/11/2013,%d /%m / %Y),
as.Date(31/12/2014,%d /%m /%Y),
days
)
VectorofValues = runif(length(VectorofDates))
#plot
(ThePlot < - CalendarTimeSeries(VectorofDates,VectorofValues,TRUE))
>
How to make calendar time series charts like this with ggplot2? I couldn't find anything so I went ahead and wrote it up.
解决方案# Makes calendar time series plot # The version rendered on the screen might look out of scale, the saved version should be better CalendarTimeSeries <- function( DateVector = 1, ValueVector = c(1,2), SaveToDisk = FALSE ) { if ( length(DateVector) != length(ValueVector) ) { stop('DateVector length different from ValueVector length') } require(ggplot2) require(scales) require(data.table) # Pre-processing ============================================================ DateValue <- data.table( ObsDate = DateVector, IndexValue = ValueVector ) DateValue[, Yr := as.integer(strftime(ObsDate, '%Y'))] DateValue[, MthofYr := as.integer(strftime(ObsDate, '%m'))] DateValue[, WkofYr := 1 + as.integer(strftime(ObsDate, '%W'))] DateValue[, DayofWk := as.integer(strftime(ObsDate, '%w'))] DateValue[DayofWk == 0L, DayofWk := 7L] # Heatmap-ish layout to chalk out the blocks of colour on dates ============= p1 <- ggplot( data = DateValue[,list(WkofYr, DayofWk)], aes( x = WkofYr, y = DayofWk ) ) + geom_tile( data = DateValue, aes( fill = IndexValue ), color = 'black' ) + scale_fill_continuous(low = "green", high = "red") + theme_bw()+ theme( plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank() ) + facet_grid(.~Yr, drop = TRUE, scales = 'free_x', space = 'free_x') # adding borders for change of month ======================================== # vertical borders ( across weeks ) -------------------------------------- setkeyv(DateValue,c("Yr","DayofWk","WkofYr","MthofYr")) DateValue[,MonthChange := c(0,diff(MthofYr))] MonthChangeDatasetAcrossWks <- DateValue[MonthChange==1] MonthChangeDatasetAcrossWks[,WkofYr := WkofYr - 0.5] if ( nrow(MonthChangeDatasetAcrossWks) > 0 ) { p1 <- p1 + geom_tile( data = MonthChangeDatasetAcrossWks, color = 'black', width = .2 ) } # horizontal borders ( within a week ) ----------------------------------- setkeyv(DateValue,c("Yr","WkofYr","DayofWk","MthofYr")) DateValue[,MonthChange := c(0,diff(MthofYr))] MonthChangeDatasetWithinWk <- DateValue[MonthChange==1 & (! DayofWk %in% c(1))] # MonthChangeDatasetWithinWk <- DateValue[MonthChange==1] MonthChangeDatasetWithinWk[,DayofWk := DayofWk - 0.5] if ( nrow(MonthChangeDatasetWithinWk) > 0 ) { p1 <- p1 + geom_tile( data = MonthChangeDatasetWithinWk, color = 'black', width = 1, height = .2 ) } # adding axis labels and ordering Y axis Mon-Sun ============================ MonthLabels <- DateValue[, list(meanWkofYr = mean(WkofYr)), by = c('MthofYr') ] MonthLabels[,MthofYr := month.abb[MthofYr]] p1 <- p1 + scale_x_continuous( breaks = MonthLabels[,meanWkofYr], labels = MonthLabels[, MthofYr], expand = c(0, 0) ) + scale_y_continuous( trans = 'reverse', breaks = c(1:7), labels = c('Mon','Tue','Wed','Thu','Fri','Sat','Sun'), expand = c(0, 0) ) # saving to disk if asked for =============================================== if ( SaveToDisk ) { ScalingFactor = 10 ggsave( p1, file = 'CalendarTimeSeries.png', height = ScalingFactor* 7, width = ScalingFactor * 2.75 * nrow(unique(DateValue[,list(Yr, MthofYr)])), units = 'mm' ) } p1 } # some data VectorofDates = seq( as.Date("1/11/2013", "%d/%m/%Y"), as.Date("31/12/2014", "%d/%m/%Y"), "days" ) VectorofValues = runif(length(VectorofDates)) # the plot (ThePlot <- CalendarTimeSeries(VectorofDates, VectorofValues, TRUE))
这篇关于日历时间系列与R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文