如何基于R中的5年间隔创建箱线图 [英] How to create boxplot based on 5 year intervals in R

查看:290
本文介绍了如何基于R中的5年间隔创建箱线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在不同日期测量的连续变量y。我需要用显示每个5年间隔y的分布的框制作箱图。



样本数据:

  rdob<-as.Date( dob,format =%m /%d /%y)
ggplot(data =数据,aes(x = rdob,y = ageyear))+ geom_boxplot()

#警告信息:
#连续x美学-您忘了aes(group = ...)吗?



此图片是我尝试的第一张图片。我想要的是每五年间隔一个盒子,而不是每年一个盒子。

解决方案

下面是一个使用Dave2e的示例建议在日期间隔使用


I have a continuous variable y measured on different dates. I need to make boxplots with a box showing the distribution of y for each 5 year interval.

Sample data:

rdob <- as.Date(dob, format= "%m/%d/%y")
ggplot(data = data, aes(x=rdob, y=ageyear)) + geom_boxplot()

#Warning message:
#Continuous x aesthetic -- did you forget aes(group=...)?  

This image is the first one I tried. What I want is a box for every five year interval, instead of a box for every year.

解决方案

Here's an example taking Dave2e's suggestion of using cut on date intervals along with ggplot's group aesthetic mapping:

library(ggplot2)

n <- 1000

## Randomly sample birth dates and dummy up an effect that trends upward with DOB
dobs <- sample(seq(as.Date('1970/01/01'), Sys.Date(), by="day"), n)
effect <- rnorm(n) + as.numeric(as.POSIXct(dobs)) / as.numeric(as.POSIXct(Sys.Date()))
data <- data.frame(dob=dobs, effect=effect)

## boxplot w/ DOB binned to 5 year intervals
ggplot(data=data, aes(x=dob, y=effect)) + geom_boxplot(aes(group=cut(dob, "5 year")))

这篇关于如何基于R中的5年间隔创建箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆