Reputation: 95
How would I be able to do a line showing the median of the sum?
What I currently have
dataset$`Created Date`<- gsub("T.*","",dataset$`Created Date`)
dataset$`Created Date`<- ymd(strptime(dataset$`Created Date`, format="%Y-%m-%d"))
names(dataset) <- gsub(" ","_",names(dataset)) #rename column to remove space
dfcount <- data.frame(count(dataset, `Created_Date`)) #create dataframe
dfcount$Created_Date <- as.POSIXlt(dfcount$Created_Date) #Convert to POSIX for weekdayfilter
Monthlywithavg <- ggplot(dfcount,aes(Month, n))+
stat_summary(fun.y = sum, geom = "line") +
scale_x_date(labels = date_format("%Y-%m"))+
stat_summary(fun.y = mean, geom = "line")
Monthlywithavg
Let me know if there's anything else I should change too.
Thanks!
Upvotes: 1
Views: 966
Reputation: 10526
Here is another alternative to CPak's, it is essentially the same.
library(ggplot2)
library(plyr)
agg = plyr::ddply(mtcars,'cyl',summarize,mpg = sum(mpg) )
ggplot(mtcars, aes(x=cyl, y=mpg)) +
stat_summary(fun.y=sum, geom="line") +
geom_hline(data = agg,aes(yintercept = median(mpg)),color="red")
Upvotes: 1
Reputation: 13591
Hopefully someone has a better answer but I think you'll have to calculate the median of the sum yourself. See this reproducible example.
library(ggplot2)
library(dplyr)
library(magrittr)
median_of_sum <- mtcars %>%
group_by(cyl) %>%
summarise(sum = sum(mpg)) %>%
ungroup() %>%
summarise(median = median(sum))
ggplot(mtcars, aes(x=cyl, y=mpg)) +
stat_summary(fun.y=sum, geom="line") +
geom_hline(data=median_of_sum, aes(yintercept=median), color="red")
Upvotes: 1