ggplot反向轴顺序为因子

时间:2018-06-15 14:43:35

标签: r ggplot2 ggridges

这是基本情节,按月计算从下到上一到十二。我想订购它们从上到下一到十二。

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

Capture.png

这两种解决方案都会产生错误。什么是正确的解决方案?

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

错误:提供给连续刻度的离散值。此外:   警告消息:1:在Ops.factor(x)中:' - '没有意义   因素。 2:变换在连续中引入无限值   y轴。

以及

# 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))))
  

is.factor(x)中的错误:object' month'找不到

1 个答案:

答案 0 :(得分:1)

尝试scale_y_discrete(limits = rev(levels(as.factor(weather$month))))

相关问题