我想安排几个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 ~ .)
现在,我想要累积直方图,因此我遵循了这一原则。但这给了我错误的总和:
qplot(Temp, data=df, geom="histogram",binwidth=1)+
geom_histogram(aes(y=cumsum(..count..)),binwidth=1)+
facet_grid(Modul ~ .)
尽管我大致了解正在发生的事情,但我不足以解决此问题。有什么提示吗?
最好的问候,Jochen
这里可能是顺序问题:我认为您不能在将函数应用于内部生成的变量(此处为stat“ bin”引擎)之前进行分面。因此,正如在其他答案中提到的那样,您需要在外部进行计算。
我会 :
geom_histogram
通过统计内部引擎获取创建数据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")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句