ggplot、ggridges 中的倒序
Reverse order of breaks in ggplot, ggridges
我有一个包含长度(整数)和年份(因子)的数据集,我想使用 ggridges
绘制它。这是一个包含整数和因子数据的类似数据集。如何更改 y 轴上 Species(即因子)的顺序?
library(ggplot2)
library(ggridges)
library(viridis)
library(datasets)
order <- c("setosa", "versicolor", "virginica")
ggplot(iris, aes(x = Sepal.Length, y = Species, fill = ..x..), order(Species)) +
geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
scale_fill_viridis(name = "Sepal.Length", option = "A") +
theme_ridges() +
labs(title = 'Sepal Length distributions for irises')
此处,order(Species)
或order(order)
不起作用。
我试过了:
scale_y_reverse(breaks=order), expand = c(0.01, 0))
但后来意识到这是针对连续变量的(尝试将年份作为数字 - 没有用)。
这是你想要的吗?我将 mutate(Species = factor(Species, levels = rev(myorder)))
添加到您的代码
library(dplyr)
library(ggplot2)
library(ggridges)
library(viridis)
library(datasets)
myorder <- c("setosa", "versicolor", "virginica")
iris <- iris %>%
mutate(Species = factor(Species, levels = rev(myorder)))
ggplot(iris, aes(x = Sepal.Length, y = Species, fill = ..x..), Species) +
geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
scale_fill_viridis(name = "Sepal.Length", option = "A") +
theme_ridges() +
labs(title = 'Sepal Length distributions for irises')
#> Picking joint bandwidth of 0.181
编辑:另一种更简单的方法是使用 forcats
包
中的 fct_rev()
library(forcats)
library(ggplot2)
library(ggridges)
ggplot(iris, aes(x = Sepal.Length, y = fct_rev(Species), fill = ..x..), Species) +
geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
scale_fill_viridis_c(name = "Sepal.Length", option = "A") +
theme_ridges() +
labs(title = 'Sepal Length distributions for irises')
#> Picking joint bandwidth of 0.181
由 reprex package (v0.2.1.9000)
创建于 2018-09-27
我有一个包含长度(整数)和年份(因子)的数据集,我想使用 ggridges
绘制它。这是一个包含整数和因子数据的类似数据集。如何更改 y 轴上 Species(即因子)的顺序?
library(ggplot2)
library(ggridges)
library(viridis)
library(datasets)
order <- c("setosa", "versicolor", "virginica")
ggplot(iris, aes(x = Sepal.Length, y = Species, fill = ..x..), order(Species)) +
geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
scale_fill_viridis(name = "Sepal.Length", option = "A") +
theme_ridges() +
labs(title = 'Sepal Length distributions for irises')
此处,order(Species)
或order(order)
不起作用。
我试过了:
scale_y_reverse(breaks=order), expand = c(0.01, 0))
但后来意识到这是针对连续变量的(尝试将年份作为数字 - 没有用)。
这是你想要的吗?我将 mutate(Species = factor(Species, levels = rev(myorder)))
添加到您的代码
library(dplyr)
library(ggplot2)
library(ggridges)
library(viridis)
library(datasets)
myorder <- c("setosa", "versicolor", "virginica")
iris <- iris %>%
mutate(Species = factor(Species, levels = rev(myorder)))
ggplot(iris, aes(x = Sepal.Length, y = Species, fill = ..x..), Species) +
geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
scale_fill_viridis(name = "Sepal.Length", option = "A") +
theme_ridges() +
labs(title = 'Sepal Length distributions for irises')
#> Picking joint bandwidth of 0.181
编辑:另一种更简单的方法是使用 forcats
包
fct_rev()
library(forcats)
library(ggplot2)
library(ggridges)
ggplot(iris, aes(x = Sepal.Length, y = fct_rev(Species), fill = ..x..), Species) +
geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
scale_fill_viridis_c(name = "Sepal.Length", option = "A") +
theme_ridges() +
labs(title = 'Sepal Length distributions for irises')
#> Picking joint bandwidth of 0.181
由 reprex package (v0.2.1.9000)
创建于 2018-09-27