如何在R中制作时间箱箱图

fan*_*cci 7 r ggplot2

我正在尝试使用时间序列箱图ggplot2.

我对很多人都有价值观.

我需要用我的数据按月制作一个时间箱箱图.

我认为我的问题是如何用我的数据创建一个因子(月).

p <- ggplot(mydata, aes(factor(date), measure))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

dom*_*_oh 9

另一种方法是不必更改日期格式并进行任何排序等,只需将日期添加为分组因子,如下所示:

ggplot(mydata) + geom_boxplot(aes(x = date, y = measure, group = date))


Ram*_*han 5

更新:根据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))
Run Code Online (Sandbox Code Playgroud)

其产生: 在此输入图像描述

希望这可以帮助您前进。