[英]How to make a timeseries boxplot in R
我正在尝试使用ggplot2
创建一个时间序列ggplot2
。
我对很多人都有价值观。
我需要用我的数据按月制作一个时间箱箱图。
我认为我的问题是如何用我的数据创建一个因子(月)。
p <- ggplot(mydata, aes(factor(date), measure))
另一种方法是不必更改日期格式并进行任何排序等,只需将日期添加为分组因子,如下所示:
ggplot(mydata) + geom_boxplot(aes(x = date, y = measure, group = date))
更新:根据OP的说明,多年必须单独处理。
library(ggplot2)
#generate dummy data
date_range <- as.Date("2010/06/01") + 0:400
measure <- runif(401)
mydata <- data.frame(date_range, measure)
# create new columns for the months and years, and
# and a year_month column for x-axis labels
mydata$month <- format(date_range, format="%b")
mydata$year <- as.POSIXlt(date_range)$year + 1900
mydata$year_month <- paste(mydata$year, mydata$month)
mydata$sort_order <- mydata$year *100 + as.POSIXlt(date_range)$mon
#plot it
ggplot(mydata) + geom_boxplot(aes(x=reorder(year_month, sort_order), y=measure))
哪个产生:
希望这有助于您前进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.