r - ggplot2 中的方面累积总和

标签 r

我想安排几个ggplot2-plots。
它适用于直方图,使用以下代码:

df<-NULL
df$Temp<-rnorm(mean=20,sd=3,n=100) 
df$Modul<-rep(seq(1,4,1),25)
df<-as.data.frame(df)   

qplot(Temp, data=df, geom="histogram",binwidth=1)+
    facet_grid(Modul ~ .)

enter image description here

现在我想要累积直方图,我跟着 this recipy .
但它给了我错误的总和:
qplot(Temp, data=df, geom="histogram",binwidth=1)+
geom_histogram(aes(y=cumsum(..count..)),binwidth=1)+
facet_grid(Modul ~ .)

enter image description here

虽然我大致了解正在发生的事情,但我还不够专业,无法解决这个问题。
任何提示?

此致,
约亨

最佳答案

这可能是这里的顺序问题:我认为在将函数应用于内部生成的变量(此处为 stat "bin"引擎)之前,您不能进行分面。因此,正如其他答案中所述,您需要在外面进行计算。

我会 :

  • 使用 geom_histogram通过统计内部引擎获取创建数据
  • 使用生成的数据在 ggplot2 之外按组计算累积计数。
  • 绘制新数据的条形图

  • enter image description here
    p <- ggplot(df,aes(x=Temp))+
      geom_histogram(binwidth=1)+facet_grid(Modul~.)
    
    dat <-  ggplot_build(p)$data[[1]]
    library(data.table)
    ggplot(setDT(dat)[,y:=cumsum(y),"PANEL"],aes(x=x)) +
      geom_bar(aes(y=y,fill=PANEL),stat="identity")+facet_grid(PANEL~.) +
      guides(title="Modul")
    

    关于r - ggplot2 中的方面累积总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26116531/

    相关文章:

    r - dplyr 中的 mutate_each/summarise_each : how do I select certain columns and give new names to mutated columns?

    R:向表达式添加文本

    r - 生成逆排列

    R数据帧: headers based on existing row containing text and numbers

    删除字符串中的运行长度重复数字和 NA

    regex - 查找转义字符(不包括引号和反引号)

    r - 关闭 R Shiny App 时断开与 PostgreSQL 的连接

    R错误类型 "Subscript out of bounds"

    r - `caret` 中的训练错误率

    c++ - Rcpp并行: Converting CharacterMatrix to RMatrix<T>