1

I would like to create a time series plot using ggplot2 in which the variable plotted over time. However, for each time period, I would like to plot the cumulative count for that period. For example:

set.seed(123)
frame <- data.frame(id = sort(rep(c(0:5), 5)),year = rep(c(2000:2005), 5), y = sample(0:1,30, replace = TRUE))
table(frame$year, frame$y)
ggplot(frame, aes(x = year, y = y)) + geom_point(shape = 1) # Not right

I would ultimately like it to generate a plot like this:

count<- table(frame$year, frame$y)[,2]
plot(2000:2005, count, type = "l")

enter image description here

I'm new to ggplot and any pointers would be greatly appreciated. Thanks.

1
  • what is your expected output Commented Nov 15, 2015 at 23:02

2 Answers 2

1

Try:

library(ggplot2)
library(dplyr)
frame %>% group_by(year) %>% summarise(sum = sum(y)) %>% 
ggplot(aes(x = year, y = sum)) + geom_line()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

You are essentially missing a single line from your program. You want a dataframe that returns the sum of the y variable for the year.

set.seed(123)
frame <- data.frame(id = sort(rep(c(0:5), 5)),year = rep(c(2000:2005), 5), y = sample(0:1,30, replace = TRUE))
table(frame$year, frame$y)
newFrame <-aggregate(frame$y, list(frame$year),sum)
ggplot(frame, aes(x = newFrame$Group.1, y = newFrame$x)) + geom_point(shape = 1) # Better

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.