I have data called females with two variables (timeresponse, age). I wanted to draw a boxplot of their time response based on their age in 5-year intervals. I would be thankful if you'd help out.
Asked
Active
Viewed 216 times
0
-
Please provide a minimum reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – deschen Nov 26 '20 at 11:46
-
1Apart form that, please have a look at the `cut` function which offers to split your time response into equal intervals. Howwver, if `cut` works will also depend on the data type of your input variables (i.e. are these numeric values or datetime objects?). – deschen Nov 26 '20 at 11:47
-
What have you tried so far Sara? – Matt Weller Nov 26 '20 at 11:49
1 Answers
1
Since you have not given any sample data, let's first create one
library(tidyverse)
set.seed(50)
df <- data.frame(id = 1:1000,
dates = as.Date("1990-01-01") + runif(1000)*10000,
measure = runif(1000)*50)
> (tibble(df))
# A tibble: 1,000 x 3
id dates measure
<int> <date> <dbl>
1 1 2009-05-28 27.0
2 2 2001-12-25 26.1
3 3 1995-06-24 18.6
4 4 2011-01-01 26.3
5 5 2004-01-19 17.1
6 6 1991-03-24 22.5
7 7 2009-03-01 19.2
8 8 2007-09-12 32.5
9 9 1991-02-25 24.6
10 10 1992-12-11 33.9
You can use cut()
function to group dates. the sample code may be
df %>% mutate(dategroup = cut(dates, breaks = "5 years")) %>%
ggplot() +
geom_boxplot(aes(x=dategroup, y=measure, color = dategroup))
Boxplot generated

AnilGoyal
- 25,297
- 4
- 27
- 45
-
Hi, thanks a lot. Well, I am quite new to R and couldn't figure out how to deal with my data. I am using nym.2002 data in the UsingR package. This is what I tried: `mutate(nym.2002, dategroup = cut(nym2002 $ age, breaks == 5 )) %>% ggplot() + geom_boxplot(aes(x=dategroup, y=nym2002 $ time, color = dategroup))` – Sara Raha Nov 28 '20 at 08:10
-
Please accept and upvote, if it helped, as usually desired on SO. :) – AnilGoyal Nov 28 '20 at 08:30