0

Here is a MWE:

library(ggplot2)
library(ggridges)

ggplot(iris, aes(x=Sepal.Length, y=Species, fill=..x..)) +
geom_density_ridges_gradient()

My query is : can we not say fill = Sepal.Length?

I understand that ..x.. refers to a computed variable x and the call to geom_density_ridges_gradient will probably not see the variable in the ggplot terminology, but at the time of the ggplot incantation we CAN refer to Sepal.Length, can we?

Can someone please clarify why we need to say ..x.. and not Sepal.Length in this case? I am not fully certain of the reasoning here.

To be more precise why does this not work:

ggplot(iris, aes(x=Sepal.Length, y=Species, 
fill=Sepal.Length)) +  geom_density_ridges_gradient()

<------------I would like to rephrase my query starting here------------>

If I do :

ggplot(iris, aes(x = Sepal.Length, y = Species)) + 
geom_density_ridges_gradient(fill = Sepal.Length)

geom_density_ridges_gradient will not be able to find the variable Sepal.Length and it gives an error.

So, the right incantation should be:

ggplot(iris, aes(x = Sepal.Length, y = Species)) + 
geom_density_ridges_gradient(fill = ..x..)

geom_density_ridges_gradient should be able to find the computed variable ..x.. , but it does NOT work. Can someone please explain why this is ?

Another query:

If I do :

ggplot(iris, aes(x = Sepal.Length, y = Species,fill = Sepal.Length)) + 
geom_density_ridges_gradient()

Why does it not give me an error message saying Sepal.Length not found,
it simply ignores the fill argument and draws the output,why is this ?

What finally seems to work is this :

ggplot(iris, aes(x=Sepal.Length, y=Species, fill=..x..)) +
geom_density_ridges_gradient()

but I am not sure why is it that it works.

Basically I am confused as to where the argument corresponding to the fill should be placed.

user2338823
  • 501
  • 1
  • 3
  • 16

1 Answers1

0

I think what might be going on here is that Sepal.Length is not a category.

With this in mind, can I assume that this is what you're after? (If not, let me know, and I'll try to answer what it is that you need.)

library(tidyverse)
library(ggridges)

ggplot(
  data = iris, 
  aes(x=Sepal.Length, y=Species, fill=Species)
  ) + 
  geom_ridgeline(
    stat = "binline", 
    bins = 20, 
    draw_baseline = FALSE
  )

Running this code generates this chart:

ggridges-chart

However, if you wanted to group by Sepal.Length, as you suggest, you could add the following (from here):

ggplot(
  data = iris, 
  aes(x=Sepal.Length, y=Species, fill=Species, group=Sepal.Length)
  ) + 
  geom_ridgeline(
    stat = "binline", 
    bins = 20, 
    draw_baseline = FALSE
  )

... Which would generate the following:

ggridges-group-chart

M--
  • 25,431
  • 8
  • 61
  • 93
p0bs
  • 1,004
  • 2
  • 15
  • 22