19

No matter what I try I cannot create a ridgeline plot using ggridges. Using a dataframe graphing_dataframe that looks as follows:

str(graphing_dataframe)
summary(graphing_dataframe)

> str(graphing_dataframe)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   14 obs. of  3 variables:
 $ id    : chr  "00343" "00343" "00343" "00343" ...
 $ week  : num  14 1 2 3 4 5 6 7 8 9 ...
 $ rating: num  14 4 12 8 14 19 16 16 7 8 ...
 - attr(*, "spec")=
  .. cols(
  ..   id = col_character(),
  ..   week = col_double(),
  ..   rating = col_double()
  .. )
> summary(graphing_dataframe)
      id                 week           rating     
 Length:14          Min.   : 1.00   Min.   : 4.00  
 Class :character   1st Qu.: 4.25   1st Qu.: 8.00  
 Mode  :character   Median : 7.50   Median :10.50  
                    Mean   : 7.50   Mean   :11.43  
                    3rd Qu.:10.75   3rd Qu.:15.50  
                    Max.   :14.00   Max.   :19.00 

My data is:

structure(list(id = c("00343", "00343", "00343", "00343", "00343", 
"00343", "00343", "00343", "00343", "00343", "00343", "00343", 
"00343", "00343"), week = c(14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13), rating = c(14, 4, 12, 8, 14, 19, 16, 16, 7, 8, 9, 
18, 9, 6)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-14L), spec = structure(list(cols = list(id = structure(list(), class = c("collector_character", 
"collector")), week = structure(list(), class = c("collector_double", 
"collector")), rating = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector"))), class = "col_spec"))

My code is:

ggplot(graphing_dataframe, 
       aes(x = rating, y = week, fill = ..x..)
       ) +
  geom_density_ridges()

Picking joint bandwidth of 2.53
Error: geom_density_ridges requires the following missing aesthetics: y

I've tried using unlist per this question but that doesn't work either.

Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
Frank B.
  • 1,813
  • 5
  • 24
  • 44
  • 3
    Can you post your data in a copy-and-paste format (e.g., using `dput` or `dump`)? – Dan Jan 11 '19 at 13:18
  • 2
    Are you sure you want density ridges? Usually these functions are used if you have a grouping variable, and a density. Do you just want a line plot of (week, rating)? I can't visualise another suitable output with this data, unless the original data is a lot bigger. – Jonny Phelps Jan 11 '19 at 14:47

6 Answers6

19

Seems as if the error message is a bit misleading, according to the documentation you need to add a group aesthetic for numerical variables:

The grouping aesthetic does not need to be provided if a categorical variable is mapped onto the y axis, but it does need to be provided if the variable is numerical.

so adding group = week to ggplot(aes(…)) ought to do the trick. (Just had the same issue myself.)

chrka
  • 191
  • 1
  • 3
  • 1
    I had the same problem, I did as you explained and it works!! – GiacomoDB Mar 21 '22 at 10:01
  • worked for me as well, super helpful. This was my final code: `ggplot(df, aes(x = independent_variable, y = grouping_variable, group = grouping_variable)) + geom_density_ridges()` – MegaBytten Jan 20 '23 at 13:04
10

This question seems to be at the top of the list when googling this error message, so I thought I'd chime in: the other reason you can get this error is if you have your x and y switched:

For instance, with the above data (using id instead), this works:

ggplot(graphing_dataframe, 
        aes(x = rating, y = id)) +
  geom_density_ridges()

While this throws the error:

ggplot(graphing_dataframe, 
        aes(x = id, y=rating)) +
  geom_density_ridges()
NWaters
  • 1,163
  • 1
  • 15
  • 27
6

I have also noticed , if y is a factor this error will not pop up. try this :)

ggplot(graphing_dataframe, 
        aes(x = id, y=as.factor(rating))) +
  geom_density_ridges()
Thevandalyst
  • 79
  • 1
  • 2
4

As @JonnyPhelps commented my data is not compatible with a ridgeline plot (or vice versa).

Frank B.
  • 1,813
  • 5
  • 24
  • 44
2

y needs to be a categorical variable. You can try:

ggplot(graphing_dataframe, 
   aes(x = rating, y = as.factor(week), fill = ..x..))+
geom_density_ridges()

or

ggplot(graphing_dataframe, 
   aes(x = rating, y = as.character(week), fill = ..x..))+
geom_density_ridges()
0

I have had succes with either of these two models inspired by the above discussion and the documentation: ggplot(CSVdotcsv, aes(x = TSH, y = as.factor(t))) + #as factor provides the legend for hour ggplot(CSVdotcsv, aes(x = TSH, y = t, group = t)) + # important to add group =

glensbo
  • 31
  • 1
  • 6