2015-11-01 94 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")