分类数据的直方图用ggplot2细分为子类别?

时间:2013-11-30 17:36:49

标签: r ggplot2

method <- c(rep("method1", 25), rep("method2", 80), rep("method3", 177))
exc <- c(1, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 1, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
pr <- c(9.778158e+01, 2.383512e+00, 7.017030e-02, 5.895421e-02, 9.979526e+01, 9.996787e+01, 7.162376e+01, 8.286646e-01, 3.059980e-01, 5.724184e-02, 7.823576e-01, 2.301170e-01, 1.622905e+00, 1.689215e+00, 9.845610e+01, 1.123755e+01, 3.875266e-01, 3.167882e+00, 4.120089e+01, 1.052839e+00, 6.381193e-01, 1.757222e+01, 9.984169e+01, 1.493086e+00, 9.998443e+01, 3.005090e-01, 8.079704e+01, 9.032584e-03, 2.828906e-02, 7.804222e+01, 1.763882e-01, 3.713532e-02, 5.388052e-02, 5.403792e-02, 1.249938e-01, 1.998921e-01, 4.402396e-03, 3.342248e-03, 1.759018e-02, 2.420178e-02, 4.375781e-04, 6.137413e-03, 6.512810e+01, 4.668736e-01, 1.045915e-02, 1.513374e-01, 8.237510e-02, 9.512328e-02, 6.231771e-02, 2.316358e-02, 9.073479e-02, 1.911786e-02, 2.136473e-02, 4.618955e-01, 1.019962e+00, 5.266899e-02, 1.339400e-02, 3.811348e-02, 8.515192e+00, 3.003492e-02, 7.733972e-03, 5.277713e-01, 4.700602e-02, 8.439378e-03, 5.790735e-03, 1.029814e-02, 1.994390e-02, 1.083500e+00, 5.702771e-02, 1.247394e-02, 1.464561e-01, 3.259409e-02, 6.687443e-03, 1.139584e-02, 2.847002e-02, 2.377251e+00, 2.834861e+01, 4.786969e-01, 1.458309e-03, 1.174660e-01, 4.023188e-03, 2.273696e-01, 2.558717e-01, 2.559825e-03, 7.208975e+01, 1.195069e+00, 8.448632e+01, 2.057061e-02, 6.576548e-03, 3.235016e+01, 4.556932e-02, 4.185363e-01, 7.901099e+00, 1.921646e-02, 2.226815e-01, 2.566391e-01, 1.899886e-01, 9.862253e+01, 9.937875e-02, 1.956194e-02, 2.190072e-02, 9.479701e-03, 9.008552e+01, 1.590398e-01, 5.293134e-02, 1.412295e-02, 6.126795e-03, 1.617179e-02, 9.723586e+01, 2.291093e-01, 3.501661e-02, 2.065283e-01, 8.174733e+01, 9.902566e-02, 2.623761e-03, 7.848924e-03, 5.918991e+01, 4.847605e-03, 9.998977e+01, 2.598032e-03, 6.423861e+01, 5.152965e-03, 7.230036e+01, 3.566760e-01, 6.076995e-02, 5.944848e-02, 2.120722e-02, 4.765753e-03, 1.923560e-02, 3.962496e-02, 2.374236e-01, 2.629203e-02, 2.163854e+00, 1.699109e+00, 1.253692e-01, 6.023272e-01, 1.287564e-02, 4.878132e+01, 2.272958e-01, 3.276120e-01, 8.198424e+01, 9.638494e+01, 9.549004e-02, 2.739015e-03, 3.203505e-02, 4.889431e-03, 2.291124e+01, 1.524237e-02, 6.829877e+00, 2.879291e-02, 9.585328e-02, 5.195482e-02, 4.460379e+00, 1.788282e-01, 7.170554e-02, 1.260122e-02, 2.214409e-02, 3.507428e+00, 4.991801e-03, 6.293137e-02, 5.844928e-03, 3.248548e+00, 2.591625e-01, 3.426773e-02, 1.774687e-01, 9.756274e+01, 7.076785e+01, 6.956855e-02, 2.147921e-01, 3.928065e-01, 4.460488e-02, 1.764221e-01, 5.277367e-02, 4.594422e-02, 1.060427e-01, 1.114139e+00, 3.626841e+01, 2.771743e-03, 1.589661e-02, 7.022587e-02, 3.651647e-03, 1.252429e+01, 4.773260e-01, 5.942872e-02, 2.483184e+01, 1.420190e-01, 3.652907e-03, 9.955749e-03, 1.111343e-01, 6.028820e+01, 4.564798e-02, 8.623486e-03, 4.440423e-03, 2.574466e-01, 3.370628e-02, 7.822098e-03, 1.608214e-02, 2.638099e-02, 8.873891e+01, 4.317737e-01, 1.735514e-02, 1.277323e-01, 5.741704e+01, 2.375506e-02, 5.359338e-02, 1.709943e+00, 3.987456e-01, 1.505662e-02, 2.816364e-02, 9.348211e-02, 4.637968e-02, 4.608277e-02, 2.236911e+01, 1.326066e+00, 1.613083e+01, 3.346656e+00, 2.611929e-02, 9.568675e+01, 2.724418e-02, 2.839759e-03, 9.563275e-03, 9.963634e+01, 9.959123e+01, 6.270345e-01, 4.705309e-02, 5.428094e-02, 1.648435e-02, 8.296800e-02, 4.055409e-02, 1.621184e-02, 1.314828e-01, 7.527521e+01, 5.230978e-02, 2.607093e-01, 6.399820e+01, 1.088062e-02, 9.429669e-02, 1.953214e-01, 1.474039e-01, 9.416921e+01, 2.380873e-03, 2.471462e+00, 3.633414e-02, 1.125673e+00, 1.344756e-01, 1.064287e+00, 8.415448e-01, 1.344756e-01, 2.472510e+01, 1.972818e-01, 2.448721e-02, 8.257077e-02, 6.035718e-02, 5.909623e+00, 3.034434e-02, 5.397369e-01, 7.197956e+01, 2.213201e-01, 1.816337e-01, 3.026522e+00, 4.670512e-03, 6.729115e-02, 8.528516e-02, 8.076168e-03, 2.702415e-01, 4.297185e-03, 2.110990e-02, 8.785337e-02, 1.729627e-01, 7.216669e-03, 1.097661e-02, 3.911452e-02, 2.573924e-02, 4.284408e-01, 1.453148e-01, 2.295089e-02, 2.240505e-01, 1.232520e-01, 1.360767e+00, 5.738281e-01, 6.136543e-02, 3.749292e-02)
loc <- c(18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 20, 18, 19, 20, 19, 19, 20, 20, 19, 19, 18, 20, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15)
data <- data.frame(method, pr, exc, loc)

如何创建直方图/密度图,其中ggplot2的数据$ pr分为类别($ method),每个类别划分为子类别($ exc),用$ loc着色?

1 个答案:

答案 0 :(得分:2)

您可以使用分面:

ggplot(data)+ 
    geom_histogram(aes(x=pr,fill=factor(loc))) +
    facet_grid(method~exc)

enter image description here