0

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.

stefx
  • 25
  • 10
  • 2
    Your `df` contains `factor` columns (why is `concentration` a `chr` vector in the first place?) , so you'll need to convert the `factor`s to `numeric` vectors first (e.g. using `as.numeric(as.character(...))`); secondly, you should *never* use `$` indexing inside `aes`; this can lead to some [very unexpected results](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio) when using faceting. – Maurits Evers Jun 08 '19 at 13:46
  • 1
    And lastly (and more fundamentally): `geom_density_ridges` calculates the kernel-smoothed density based on a numeric vector (see the `iris` example you give); you on the other hand seem to have calculated frequencies for certain ratios. In order to use `geom_density_ridges` you need to provide the raw `ratio` data. You should take a look at the `geom_density_ridges` (and/or base R's `density`) examples to understand what the valid syntax is. – Maurits Evers Jun 08 '19 at 14:01
  • Also don't write `aes(fill = df$concentration)`, never write `$` inside `aes`. Simply `aes(concentration)` will do. – Axeman Jun 08 '19 at 14:18
  • Thank you for the answers. When I use my raw ratio data then the calculation lasts for a very long time. In the end I got then the following error message Error: vector memory exhausted (limit reached?) – stefx Jun 08 '19 at 14:36

1 Answers1

1

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"))
Jon Spring
  • 55,165
  • 4
  • 35
  • 53