我如何能够显示总和的中位数?
我目前拥有什么
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
让我知道是否还有其他事情我也应该改变。
谢谢!
答案 0 :(得分:1)
希望有人有更好的答案,但我认为你必须自己计算总和的中位数。请参阅此可重现的示例。
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")
答案 1 :(得分:1)
这是CPak的另一种替代方案,它基本上是相同的。
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")