Search code examples
rggplot2density-plot

How to implement the "geom_density_ridges" function


I would like to generate with my data a similar plot as shown with this iris data set.

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(aes(fill = Species)) +
  scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))

Here you can see a cutout from my data set.

ratio = c(0, 0.05, 0.1, 0.15, 0.2, 0.25, 0, 0.05, 0.1, 0.15, 0.2, 0.25)
frequency = c(12000, 12300, 9000, 4300, 2434, 18000, 11000, 12200, 8000, 4100, 2400, 15900)
concentration = c("200", "200", "200", "200", "200", "200", "100", "100", "100", "100", "100", "100")
df = cbind(ratio, frequency, concentration)
View(df)
df = as.data.frame(df)

ggplot(df, aes(x = ratio, y = frequency)) +
  geom_density_ridges(aes(fill = df$concentration)) +
  scale_fill_manual(values = c("#00AFBB", "#E7B800"))

Unfortunately my code does not work. I don't know where my mistake is.


Solution

  • Is this what you're after?

    library(ggplot2); library(ggridges)
    ggplot(df, aes(x = ratio, y = concentration, 
                   height = frequency/10000, fill = concentration)) +
      geom_ridgeline() +
      scale_fill_manual(values = c("#00AFBB", "#E7B800"))
    

    enter image description here

    data:

    df = data.frame(stringsAsFactors = F,
                    ratio = c(0, 0.05, 0.1, 0.15, 0.2, 0.25, 0, 0.05, 0.1, 0.15, 0.2, 0.25),
                    frequency = c(12000, 12300, 9000, 4300, 2434, 18000, 11000, 12200, 8000, 4100, 2400, 15900),
                    concentration = c("200", "200", "200", "200", "200", "200", "100", "100", "100", "100", "100", "100"))