superimposed geom_* with ggplot2

Hi,

I want to add geom_density plot to my geom_point and I want it by year interval
In my case, should be density from 2012 to 2014, 2014 to 2016 ... maybe with facet_grid ?

Something like that :

My script & data :

list_of_packages <- c("dplyr", "ggplot2", "ggrepel")

new_packages <- list_of_packages[!(list_of_packages %in% installed.packages()[,"Package"])]

if(length(new_packages)){
  install.packages(new_packages)
} 

lapply(list_of_packages, require, character.only = TRUE)

df %>% 
  ggplot(aes(x = reception_date, y = dura)) +
  geom_point() +
  theme_bw() +
  geom_label_repel(aes(label = ifelse(duplicated_id == T, as.character(id), "")),
                   size = 3,
                   box.padding   = 0.35,
                   point.padding = 0.5,
                   segment.color = 'grey50') +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
        plot.title = element_text(hjust = 0.5, face = "bold"),
        axis.title.x = element_text(hjust = 0.5, face = "bold"),
        axis.title.y = element_text(hjust = 0.5, face = "bold"))
structure(list(id = c(101L, 107L, 108L, 109L, 115L, 117L, 118L, 
120L, 121L, 122L, 123L, 125L, 129L, 134L, 138L, 142L, 144L, 145L, 
148L, 151L, 152L, 161L, 162L, 163L, 166L, 167L, 168L, 169L, 170L, 
171L, 172L, 173L, 174L, 176L, 177L, 179L, 181L, 182L, 183L, 185L, 
188L, 190L, 192L, 193L, 195L, 196L, 197L, 201L, 205L, 208L, 209L, 
210L, 211L, 213L, 214L, 219L, 223L, 224L, 227L, 228L, 232L, 233L, 
235L, 239L, 240L, 258L, 260L, 261L, 262L, 265L, 270L, 280L, 282L, 
295L, 302L, 304L, 313L, 318L, 325L, 330L, 331L, 332L, 336L, 385L, 
394L, 394L, 395L, 407L), reception_date = structure(c(16332, 
16846, 16658, 15295, 14257, 17519, 16261, 15553, 14208, 15559, 
14880, 15714, 16268, 16476, 17708, 16528, 17564, 16861, 17375, 
17512, 15364, 16233, 15651, 17582, 16835, 17562, 15244, 16041, 
15184, 15783, 15309, 17577, 16504, 17686, 17491, 16203, 14620, 
17207, 17592, 15582, 17631, 17185, 17007, 15204, 17017, 15996, 
16451, 16779, 17462, 17029, 17618, 17513, 16617, 16315, 17207, 
16654, 16520, 15894, 16623, 16365, 16853, 16498, 15426, 16846, 
15504, 17619, 17395, 16637, 16982, 17340, 16717, 15895, 16833, 
17207, 17021, 16000, 16101, 16342, 16335, 16076, 16668, 16392, 
17154, 17115, 17599, 17599, 17115, 17448), class = "Date"), year_reception_date = c(2014, 
2016, 2015, 2011, 2009, 2017, 2014, 2012, 2008, 2012, 2010, 2013, 
2014, 2015, 2018, 2015, 2018, 2016, 2017, 2017, 2012, 2014, 2012, 
2018, 2016, 2018, 2011, 2013, 2011, 2013, 2011, 2018, 2015, 2018, 
2017, 2014, 2010, 2017, 2018, 2012, 2018, 2017, 2016, 2011, 2016, 
2013, 2015, 2015, 2017, 2016, 2018, 2017, 2015, 2014, 2017, 2015, 
2015, 2013, 2015, 2014, 2016, 2015, 2012, 2016, 2012, 2018, 2017, 
2015, 2016, 2017, 2015, 2013, 2016, 2017, 2016, 2013, 2014, 2014, 
2014, 2014, 2015, 2014, 2016, 2016, 2018, 2018, 2016, 2017), 
    duplicated_id = c(FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, 
    TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, 
    FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, 
    TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, 
    TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, 
    FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, 
    FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, 
    FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE
    ), dura = c(3823L, 4003L, 3815L, 2360L, 1200L, 4430L, 3143L, 
    2404L, 1059L, 2410L, 1731L, 2506L, 2938L, 3054L, 4194L, 3014L, 
    3991L, 3257L, 3771L, 3908L, 1699L, 2476L, 1894L, 3825L, 3048L, 
    3775L, 1730L, 2254L, 1397L, 1996L, 1522L, 3759L, 2686L, 3838L, 
    3611L, 2324L, 710L, 3297L, 3682L, 1643L, 3661L, 3185L, 3006L, 
    1173L, 2986L, 1965L, 2420L, 2718L, 3339L, 2906L, 3495L, 3390L, 
    2494L, 2131L, 3021L, 2408L, 2274L, 1618L, 2347L, 2061L, 2518L, 
    2163L, 1091L, 2481L, 1108L, 2766L, 2542L, 1784L, 2099L, 3552L, 
    1741L, 830L, 1768L, 2051L, 1741L, 691L, 730L, 942L, 904L, 
    584L, 1115L, 900L, 1632L, 1013L, 1407L, 1407L, 893L, 922L
    )), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -88L), vars = "id", drop = TRUE, indices = list(
    0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
    14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
    26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
    38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 
    50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 
    62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 
    74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84:85, 
    86L, 87L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L), biggest_group_size = 2L, labels = structure(list(
    id = c(101L, 107L, 108L, 109L, 115L, 117L, 118L, 120L, 121L, 
    122L, 123L, 125L, 129L, 134L, 138L, 142L, 144L, 145L, 148L, 
    151L, 152L, 161L, 162L, 163L, 166L, 167L, 168L, 169L, 170L, 
    171L, 172L, 173L, 174L, 176L, 177L, 179L, 181L, 182L, 183L, 
    185L, 188L, 190L, 192L, 193L, 195L, 196L, 197L, 201L, 205L, 
    208L, 209L, 210L, 211L, 213L, 214L, 219L, 223L, 224L, 227L, 
    228L, 232L, 233L, 235L, 239L, 240L, 258L, 260L, 261L, 262L, 
    265L, 270L, 280L, 282L, 295L, 302L, 304L, 313L, 318L, 325L, 
    330L, 331L, 332L, 336L, 385L, 394L, 395L, 407L)), class = "data.frame", row.names = c(NA, 
-87L), vars = "id", drop = TRUE))

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff, which includes the data (currently you have it separated out from the code itself).

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.