【发布时间】:2013-06-20 18:04:05
【问题描述】:
受 ggplot2 挑战的 latticist 需要帮助:在直方图中请求变量 per-facet 中断的语法是什么?
library(ggplot2)
d = data.frame(x=c(rnorm(100,10,0.1),rnorm(100,20,0.1)),par=rep(letters[1:2],each=100))
# Note: breaks have different length by par
breaks = list(a=seq(9,11,by=0.1),b=seq(19,21,by=0.2))
ggplot(d, aes(x=x) ) +
geom_histogram() + ### Here the ~breaks should be added
facet_wrap(~ par, scales="free")
应特殊要求,并说明为什么我不是 ggplot 的忠实粉丝,lattice 版本
library(lattice)
d = data.frame(x=c(rnorm(100,10,0.1),rnorm(100,20,0.1)),par=rep(letters[1:2],each=100))
# Note: breaks have different length by par
myBreaks = list(a=seq(8,12,by=0.1),b=seq(18,22,by=0.2))
histogram(~x|par,data=d,
panel = function(x,breaks,...){
# I don't know of a generic way to get the
# grouping variable with histogram, so
# this is not very generic
par = levels(d$par)[which.packet()]
breaks = myBreaks[[par]]
panel.histogram(x,breaks=breaks,...)
},
breaks=NULL, # important to force per-panel compute
scales=list(x=list(relation="free")))
【问题讨论】:
-
作为一名格子后卫..你能分享格子版本吗..
-
您可以在 ggplot2 之外进行分箱,例如将
hist与ddply或data.table或...结合使用,并制作条形图。 -
是的,这看起来像是要走的方法。由于这是另一位作者的包裹,因此需要一些工作。 github.com/xfim/ggmcmc/pull/17