2015-11-01 64 views
1

我想要面对一些加权直方图。这里是我的代码:使用ggplot2分割加权直方图

data <- structure(list(Weight = c(0.0111111, 0.0148148, 0.0222222, 0.00740741, 0.0222222, 0.285714, 0.133333, 0.133333, 0.0111111, 0.133333, 0.0148148, 0.133333, 0.0148148, 0.00740741, 0.00740741, 0.0111111, 0.00740741, 0.00740741, 0.00740741, 0.0222222, 0.00740741, 0.0148148, 0.00740741, 0.00740741, 0.00740741, 0.00740741, 0.00740741, 0.00740741, 0.00740741, 0.0148148, 0.00740741, 0.00740741, 0.0111111, 0.00740741, 0.133333, 0.00740741, 0.0222222, 0.0111111, 0.0111111, 0.133333), Times = c(7.49972, 3.03889, 3.09306, 1.59472, 2.55778, 0.263611, 2.88, 5.03944, 5.19833, 6.95722, 6.98889, 6.99778, 6.87361, 6.91361, 6.95056, 0.1, 6.34778, 5.78722, 6.95139, 0.145278, 0.0727778, 6.9275, 4.17222, 6.86694, 5.65028, 7.40306, 7.40306, 6.05111, 8.14944, 7.06806, 5.165, 7.62111, 0.174167, 7.40333, 0.189444, 6.95556, 6.96417, 0.213889, 6.96222, 0.3075), Distance = c(468.302, 261.584, 260.88, 124.67, 187.5, 10.6738, 184.596, 281.336, 286.554, 251.474, 252.63, 253.131, 250.407, 248.836, 251.438, 2.22492, 233.747, 237.356, 250.211, 3.77001, 1.48003, 251.094, 231.467, 250.386, 217.358, 368.035, 368.035, 356.509, 552.115, 255.06, 284.697, 469.364, 4.06013, 370.554, 5.21968, 251.989, 252.485, 5.40303, 249.193, 7.58435)), .Names = c("Weight", "Times", "Distance"), row.names = c(NA, 40L), class = "data.frame") 

mdata = melt(data) 

ggplot(mdata, aes(x=value)) + 
geom_histogram(weights = data$Weight) + 
facet_wrap(~variable, scales = "free") 

主要生产:

enter image description here

我的问题是,我不希望显示“体重”,但使用此列实际重量其他两个直方图(”时间“和”距离“)。什么是正确的方法来做到这一点?

+1

这是你需要什么? 'mdata = melt(data,id ='Weight'); ()+ ggplot(mdata,aes(x = value,weight = Weight))+ geom_histogram()+ facet_wrap(〜variable,scales =“free”)' – user20650

回答

2

不包括重量熔体

library(ggplot2) 
library(reshape2) 
mdata <- melt(data, measure.vars = c("Times", "Distance")) 

ggplot(mdata, aes(x = value, weights = Weight)) + 
    geom_histogram() + 
    facet_wrap(~variable, scales = "free")