4

This is the base plot, with months ordered bottom-to-top one through twelve. I want to order them top-to-bottom one through twelve.

library(tidyverse)
library(nycflights13)
library(ggridges)
ggplot(weather %>% filter(temp > 50), aes(x = temp, y = as.factor(month))) + 
  geom_density_ridges()

Capture.png

Both of these solutions yield errors. What is the correct solution?

# BROKEN SOLUTION 1    
ggplot(weather %>% filter(temp > 50), aes(x = temp, y = as.factor(month))) + 
  geom_density_ridges() + 
  scale_y_continuous(trans = "reverse")

Error: Discrete value supplied to continuous scale. In addition: Warning messages: 1: In Ops.factor(x) : ‘-’ not meaningful for factors. 2: Transformation introduced infinite values in continuous y-axis.

and also

# BROKEN SOLUTION 2
ggplot(weather %>% filter(temp > 50), aes(x = temp, y = as.factor(month))) + 
  geom_density_ridges() + 
  scale_y_discrete(limits = rev(levels(as.factor(month))))

Error in is.factor(x) : object 'month' not found

Glorfindel
  • 20,880
  • 13
  • 75
  • 99
stackinator
  • 4,789
  • 7
  • 32
  • 75

1 Answers1

9

Try scale_y_discrete(limits = rev)

Gregor Thomas
  • 119,032
  • 17
  • 152
  • 277
Thor6
  • 611
  • 6
  • 8