Home > Mobile >  Density curve with labelled points in ggplot2
Density curve with labelled points in ggplot2

Time:10-05

I have data showing the pitch values of a group of speakers

structure(list(`Subject no.` = c(12, 78, 4, 26, 97, 60, 34, 54, 
107, 48, 50, 115, 113, 32, 25, 30, 42, 1, 20, 102, 37, 47, 56, 
80, 33, 35, 41, 81, 94, 111, 29, 69, 101, 39, 43, 59, 90, 93, 
105, 64, 71, 73, 9, 16, 46, 21, 23, 27, 51, 84, 13, 99, 112, 
52, 103, 6, 85, 87, 40, 67, 79, 96, 114, 2, 49, 72, 38, 58, 75, 
68, 31, 62, 95, 100, 17, 36, 44, 45, 3, 15, 76, 8, 116, 63, 19, 
77, 106, 88, 28, 65, 10, 11, 24, 22, 86, 66, 74, 18, 108, 53), 
distvoice = c("L1", "L2", "L3", NA, "L4", NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, "M1", NA, NA, NA, NA, NA, NA, 
    NA, "M2", NA, NA, "M3", NA, NA, NA, NA, NA, "M4", NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "H1", NA, 
    NA, "H2", "H3", "H4", NA), f0st = c(8.56434978, 8.7741989, 
    9.18641696, 9.38890278, 9.38890278, 9.58904767, 9.78690515, 
    9.78690515, 9.78690515, 10.1759629, 10.1759629, 10.1759629, 
    10.3672614, 10.5564692, 10.7436315, 10.9287919, 10.9287919, 
    11.111993, 11.111993, 11.111993, 11.2932757, 11.4726798, 
    11.4726798, 11.4726798, 11.6502439, 11.6502439, 11.6502439, 
    11.6502439, 11.6502439, 11.6502439, 11.8260052, 11.8260052, 
    11.8260052, 12, 12, 12, 12, 12, 12, 12.1722635, 12.1722635, 
    12.1722635, 12.3428298, 12.3428298, 12.511732, 12.6790023, 
    12.6790023, 12.6790023, 12.6790023, 12.6790023, 12.8446719, 
    12.8446719, 12.8446719, 13.0087712, 13.0087712, 13.1713296, 
    13.1713296, 13.1713296, 13.3323757, 13.3323757, 13.3323757, 
    13.3323757, 13.3323757, 13.4919376, 13.4919376, 13.4919376, 
    13.6500423, 13.6500423, 13.6500423, 13.8067161, 14.1158733, 
    14.1158733, 14.1158733, 14.1158733, 14.2684059, 14.2684059, 
    14.2684059, 14.4196063, 14.7181024, 14.7181024, 14.7181024, 
    15.0115389, 15.0115389, 15.1564129, 15.3000846, 15.3000846, 
    15.3000846, 15.5838998, 15.8631371, 15.8631371, 16.0010848, 
    16.5421395, 16.5421395, 16.6748017, 16.8064552, 16.9371149, 
    17.1955129, 17.3232798, 17.4501107, 17.7010186)), row.names = c(NA, 
-100L), class = c("tbl_df", "tbl", "data.frame"))

I want to plot the density curve of the distribution of pitch values, and I want to plot the 12 speakers who have a value for distvoice as points along the x axis, to indicate where they lie in the distribution. I made a simple density plot:

ggplot(data=f0dist,
       aes(x=f0st))  
  geom_density()  
 labs(title='Distribution of pitch values for all speakers', 
       x='f0 (semitones)', y='density')

density curve plot showing the distribution of pitch values for speakers

I've tried overlaying labels and using geom_point, but I can't get it to only show points for those 12 speakers.

Any help appreciated! Ta

CodePudding user response:

Like this? The text and the density require different aesthetics - you can accomplish this by putting the aes inside the geom.

You can also try ggrepel::geom_text_repel if you find that the names overlap.

library(tidyverse)

f0dist <- structure(
  list(`Subject no.` = c(12, 78, 4, 26, 97, 60, 34, 54, 
                         107, 48, 50, 115, 113, 32, 25, 30, 42, 1, 20, 102, 37, 47, 56, 
                         80, 33, 35, 41, 81, 94, 111, 29, 69, 101, 39, 43, 59, 90, 93, 
                         105, 64, 71, 73, 9, 16, 46, 21, 23, 27, 51, 84, 13, 99, 112, 
                         52, 103, 6, 85, 87, 40, 67, 79, 96, 114, 2, 49, 72, 38, 58, 75, 
                         68, 31, 62, 95, 100, 17, 36, 44, 45, 3, 15, 76, 8, 116, 63, 19, 
                         77, 106, 88, 28, 65, 10, 11, 24, 22, 86, 66, 74, 18, 108, 53), 
       distvoice = c("L1", "L2", "L3", NA, "L4", NA, NA, NA, NA, 
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                     NA, NA, NA, NA, NA, NA, NA, "M1", NA, NA, NA, NA, NA, NA, 
                     NA, "M2", NA, NA, "M3", NA, NA, NA, NA, NA, "M4", NA, NA, 
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "H1", NA, 
                     NA, "H2", "H3", "H4", NA), 
       f0st = c(8.56434978, 8.7741989, 
                9.18641696, 9.38890278, 9.38890278, 9.58904767, 9.78690515, 
                9.78690515, 9.78690515, 10.1759629, 10.1759629, 10.1759629, 
                10.3672614, 10.5564692, 10.7436315, 10.9287919, 10.9287919, 
                11.111993, 11.111993, 11.111993, 11.2932757, 11.4726798, 
                11.4726798, 11.4726798, 11.6502439, 11.6502439, 11.6502439, 
                11.6502439, 11.6502439, 11.6502439, 11.8260052, 11.8260052, 
                11.8260052, 12, 12, 12, 12, 12, 12, 12.1722635, 12.1722635, 
                12.1722635, 12.3428298, 12.3428298, 12.511732, 12.6790023, 
                12.6790023, 12.6790023, 12.6790023, 12.6790023, 12.8446719, 
                12.8446719, 12.8446719, 13.0087712, 13.0087712, 13.1713296, 
                13.1713296, 13.1713296, 13.3323757, 13.3323757, 13.3323757, 
                13.3323757, 13.3323757, 13.4919376, 13.4919376, 13.4919376, 
                13.6500423, 13.6500423, 13.6500423, 13.8067161, 14.1158733, 
                14.1158733, 14.1158733, 14.1158733, 14.2684059, 14.2684059, 
                14.2684059, 14.4196063, 14.7181024, 14.7181024, 14.7181024, 
                15.0115389, 15.0115389, 15.1564129, 15.3000846, 15.3000846, 
                15.3000846, 15.5838998, 15.8631371, 15.8631371, 16.0010848, 
                16.5421395, 16.5421395, 16.6748017, 16.8064552, 16.9371149, 
                17.1955129, 17.3232798, 17.4501107, 17.7010186)), 
  row.names = c(NA,-100L), 
  class = c("tbl_df", "tbl", "data.frame")
)


f0dist %>%
  ggplot()  
  geom_density(aes(x = f0st))  
  geom_text(aes(x = f0st, y = 0, label = distvoice), na.rm = TRUE, angle = 90)  
  labs(title='Distribution of pitch values for all speakers', x='f0 (semitones)', y='density')

Created on 2022-10-05 by the reprex package (v2.0.1)

  • Related