[英]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.