我无法使用示例数据重新创建错误,因此无法上传所有数据。但我已经展示了我的图表的样子。从这张图表中,ggplot看起来正在阅读它自己乐队的每一个日期。为什么呢?
生成图表的代码是:
ggplot(data=tEN, aes(factor(Date_MMMYYYY), Amount)) +
geom_boxplot(notch = TRUE)
以下是我的数据的示例。唯一的区别是我还有一些列。
Dates<-c("Dec-02-2015", "Dec-03-2015", "Dec-04-2015", "Dec-05-2015", "Dec-06-2015", "Dec-07-2015", "Dec-08-2015", "Dec-09-2015", "Dec-10-2015", "Dec-11-2015", "Dec-12-2015", "Dec-13-2015", "Dec-14-2015", "Dec-15-2015", "Dec-16-2015", "Dec-17-2015","Oct-01-2015", "Oct-02-2015", "Oct-03-2015", "Oct-04-2015", "Oct-05-2015", "Oct-06-2015", "Oct-07-2015", "Oct-08-2015", "Oct-09-2015", "Oct-10-2015", "Oct-11-2015", "Oct-12-2015", "Oct-13-2015", "Oct-14-2015", "Oct-15-2015", "Oct-16-2015")
x<-rnorm(length(Dates),0,1)
df <- data.frame(Dates, x)
df$Dates_MMMYYYY<-format(as.Date(df$Dates,"%b-%d-%Y"),"%b-%Y")
ggplot(data=df, aes(factor(Dates_MMMYYYY), x)) +
geom_boxplot(notch = TRUE)
知道为什么盒子图不起作用吗?
答案 0 :(得分:0)
您可以创建月份分组以提供给ggplot:
library(lubridate)
# Convert Dates to date format
df$Dates = mdy(df$Dates)
# Create a month-year grouping variable
df$monthYear = paste0(month(df$Dates, label=TRUE),"-",year(df$Dates))
# Order the levels of the month-year grouping variable
# I've created three years worth here. Adjust as needed for the range of your data.
df$monthYear = factor(df$monthYear, levels=paste0(month.abb,"-", rep(2014:2016,each=12)))
ggplot(data=df, aes(monthYear, x)) +
geom_boxplot(notch = TRUE)
您还可以使用cut
功能动态创建月中断:
ggplot(data=df, aes(cut(Dates, breaks="month"), x)) +
geom_boxplot(notch = TRUE)