簡體   English   中英

如何在R中制作時間箱箱圖

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

哪個產生: 在此輸入圖像描述

希望這有助於您前進。

我創建了一個函數來創建你需要的圖。

功能是:

ts_plot_season <- function(x = x) {
season <- cycle(x)
season.factor <- factor(season)
ggplot() + 
  geom_boxplot(mapping = aes(x = season.factor,
                             y = x)) +
  labs(x = "Periodo", y =  "Serie")
}

Fox例子:

 ts_plot_season(AirPassengers)

時間序列的Boxplot

我希望這有幫助。 我知道這個問題很老,但我在網上找不到一些上帝的答案。 所以我認為這對某人有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM