R: how to plot density plots with ggplot2 -
> dput(data) structure(list(vascular_pathology_m = structure(c(1l, 2l, 3l, 1l, 1l, 2l, 4l, 3l, 1l, 2l, 3l, 2l, 1l, 3l, 2l, 3l, 3l, 3l, 4l, 2l, 2l, 2l, 2l, 2l, 3l, 2l, 1l, 3l, 3l, 3l), .label = c("absent", "mild", "mild/moderate", "moderate/severe", "severe"), class = "factor"), output = c(1.01789418758932, 1.05627630598801, 1.49233946102323, 1.38192374975672, 1.13097652937671, 0.861306979571144, 0.707820561413699, 1.16628243128399, 0.983163398006992, 1.23972603843843, 0.822709564829401, 0.90516107062003, 0.79080293468606, 0.886130998081624, 1.2674953773847, 0.984695292355941, 1.1781360057546, 0.858847379047159, 0.772681010534905, 1.04401349705871, 0.998339856427367, 1.12106301647898, 0.835132782324955, 0.710123766831317, 1.01005735218463, 1.05588470743658, 0.913371462992548, 1.10995126470399, 1.18574975368509, 1.17712141366123)), .names = c("vascular_pathology_m", "output"), row.names = c(1l, 3l, 4l, 5l, 6l, 7l, 8l, 10l, 11l, 12l, 13l, 14l, 15l, 16l, 17l, 18l, 19l, 20l, 21l, 22l, 23l, 24l, 25l, 26l, 27l, 28l, 29l, 30l, 31l, 32l), class = "data.frame") > data vascular_pathology_m output 1 absent 1.0178942 3 mild 1.0562763 4 mild/moderate 1.4923395 5 absent 1.3819237 6 absent 1.1309765 7 mild 0.8613070 8 moderate/severe 0.7078206 10 mild/moderate 1.1662824 11 absent 0.9831634 12 mild 1.2397260 13 mild/moderate 0.8227096 14 mild 0.9051611 15 absent 0.7908029 16 mild/moderate 0.8861310 17 mild 1.2674954 18 mild/moderate 0.9846953 19 mild/moderate 1.1781360 20 mild/moderate 0.8588474 21 moderate/severe 0.7726810 22 mild 1.0440135 23 mild 0.9983399 24 mild 1.1210630 25 mild 0.8351328 26 mild 0.7101238 27 mild/moderate 1.0100574 28 mild 1.0558847 29 absent 0.9133715 30 mild/moderate 1.1099513 31 mild/moderate 1.1857498 32 mild/moderate 1.1771214
with above dataset, plot density plot (output on x-axis, grouped , colored severity of vascular pathology).
this example plot blog post, , instead of methods 1 2 3, in case have absent, mild, mild/moderate, moderate/severe, severe. in blog post author used function stack
re-organize data. however, since data in different format, can't seem use same approach.
this should trick you.
require(ggplot2) ggplot(data, aes(x = output, fill = vascular_pathology_m)) + geom_density(alpha = 0.3)
Comments
Post a Comment