简体   繁体   中英

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 the above dataset, I would like to plot a density plot (Output on the x-axis, grouped and colored by the severity of the vascular pathology). 在此处输入图片说明

This is an example plot from this blog post , and instead of methods 1 2 3, in my case I would have Absent, Mild, Mild/Moderate, Moderate/Severe, Severe. In that blog post the author used the function stack to re-organize the data. However, since my data is in a different format, I can't seem to use the same approach.

This should do the trick for you.

require(ggplot2)
ggplot(data, aes(x = Output, fill = Vascular_Pathology_M)) +
  geom_density(alpha = 0.3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM