R 图的总和和平均数?

R 图的总和和平均数?,r,R,我怎样才能画一条线来显示总和的中位数呢 我现在拥有的 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 rem

我怎样才能画一条线来显示总和的中位数呢

我现在拥有的

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

dataset$`Created Date`希望有人能给出更好的答案,但我认为您必须自己计算总和的中位数。参见这个可复制的示例

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") 
库(ggplot2)
图书馆(dplyr)
图书馆(magrittr)
总和的中位数%
组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别
总结(总和=总和(mpg))%>%
解组()%>%
总结(中位数=中位数(总和))
ggplot(平均车数,平均排气量(x=气缸,y=英里数))+
统计汇总(fun.y=sum,geom=“line”)+
geom_hline(数据=总和的中位数,aes(yintercept=中位数),color=“红色”)

这里是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")

嘿,我试过这么做,但是得到一个没有为“Date”对象定义的错误总数。你能发布一个小样本数据给我们使用吗?