0

Fairly new to R so please excuse the crude code! I have a population of marked insects which I follow monthly. The marking level (ppm) decays over time. I would like to show a monthly density plot using ggridges, where the first month (9) showing the background (non-marked) population appears together with every following month to show the difference in signal between the marked and unmarked populations. Not sure how to get that background density plot to show with every months' plot. This is what I've tried:


        df2 <- df1 %>% filter(!is.na(ppm)) %>% 
           mutate(Month = factor(Month, levels=c(9,10,11,12,1,2,3,4,5,6,7,8))) %>%
           filter(Month %in% c(7,6,5,3,11,10,9)) %>% 
           mutate(month2 = "9")

        df1 %>% filter(!is.na(ppm)) %>% 
           mutate(Month = factor(Month, levels=c(9,10,11,12,1,2,3,4,5,6,7,8))) %>%
           filter(Month %in% c(7,6,5,3,11,10,9)) %>% 
           ggplot(., aes(x = ppm, y = Month, height = ..density.., fill=Month)) +
           geom_density_ridges(scale = 2.8, stat = "density", alpha=0.8) +
           scale_y_discrete(expand = c(0.01, 0)) +
           scale_x_log10(limits=c(125, 190), breaks=seq(125, 190, 10)) +
           scale_fill_manual(values=c("royalblue", "red3", "darkred", "firebrick3", "tomato2", 
               "indianred1", "mediumpurple3")) +
           theme_ridges() + theme(legend.position = "none") +
           geom_density_ridges(data=df2, aes(x = ppm, y = month2, height = ..density..), scale = 2.8, 
                stat = "density", alpha=0.5, fill='grey')
RoMan
  • 1
  • 1
    Hey RoMan, is it possible to supply a minimal data set e.g. a subset of `df1`? You can use `dput` function to copy data to the clipboard. When asking questions on SO you get an answer much quicker if you are able to provide some data that highlights the bug/challenge. – Jonny Phelps Oct 04 '19 at 15:06
  • Sorry about that. Let's try this: – RoMan Oct 07 '19 at 15:05
  • Month ppm 9 135.3 9 137.7 9 139.1 9 137.7 9 130.2 9 135.4 9 139.3 9 133.3 9 138.3 9 132.7 10 306.7 10 271.7 10 242.8 10 221.9 10 243.8 10 260.6 10 286.3 10 268.4 10 218.8 10 213.1 11 247.8 11 324.6 11 142.5 11 145.0 11 275.2 11 226.3 11 289.1 11 139.7 11 345.8 11 136.0 3 142.8 3 141.4 3 143.6 3 138.1 3 143.0 3 144.6 3 144.5 3 149.5 3 140.0 3 145.3 6 128.7 6 129.2 6 136.1 6 133.4 6 134.8 6 132.4 6 127.9 6 146.3 6 134.6 6 134.1 – RoMan Oct 07 '19 at 15:41

0 Answers0