ggplot 因素的反轴顺序
ggplot reverse axis order for factors
这是基本图,按从下到上 从一月到十二月的顺序排列。我想按顺序 从上到下 一到十二。
library(tidyverse)
library(nycflights13)
library(ggridges)
ggplot(weather %>% filter(temp > 50), aes(x = temp, y = as.factor(month))) +
geom_density_ridges()
这两种解决方案都会产生错误。正确的解决方案是什么?
# 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.
还有
# 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
尝试scale_y_discrete(limits = rev)
这是基本图,按从下到上 从一月到十二月的顺序排列。我想按顺序 从上到下 一到十二。
library(tidyverse)
library(nycflights13)
library(ggridges)
ggplot(weather %>% filter(temp > 50), aes(x = temp, y = as.factor(month))) +
geom_density_ridges()
这两种解决方案都会产生错误。正确的解决方案是什么?
# 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.
还有
# 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
尝试scale_y_discrete(limits = rev)