Danielle
Danielle

Reputation: 795

Reorder panels in ridgeline plot

I have a dataframe like this:

set.seed(3467)

 df<- data.frame(method= c(rep("A", 1000), rep("B", 1000), rep("C", 1000)), 
        beta=c(rnorm(1000, mean=0, sd=1),rnorm(1000, mean=2, sd=1.4),rnorm(1000, mean=0, sd=0.5)))

I wish to create a ridgeline plot similar to this:

library(ggplot2)
library(ggridges)

ggplot() +
geom_rect(data = data.frame(x = 1),
        xmin = -0.391, xmax = 0.549, ymin = -Inf, ymax = Inf,
        alpha = 0.5, fill = "gray") +
 geom_density_ridges(data = df, aes(x = beta, y = method, color = method, fill = method),
                  size=0.75)+
 xlim(-5,5)+
 scale_fill_manual(values = c("#483d8b50", "#0072B250","#228b2250")) +
 scale_color_manual(values = c("#483d8b", "#0072B2", "#228b22"), guide = "none") +
 stat_density_ridges(data = df, aes(x = beta, y = method, color = method, fill = method),
                  quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), alpha = 0.6, size=0.75)+
scale_y_discrete(expand = expand_scale(add = c(0.1, 0.9)))

However, I wish to order the y-axis in the order of method= "B", "C", "A" not method= "A", "B", "C"

I have tried the following method, without success, to reorder the density plots:

library(dplyr)
df %>%
 mutate(method = fct_relevel(method, 
                        "B", "C", "A"))%>%
ggplot() +
geom_rect(data = data.frame(x = 1),
        xmin = -0.391, xmax = 0.549, ymin = -Inf, ymax = Inf,
        alpha = 0.5, fill = "gray") +
 geom_density_ridges(data = df, aes(x = beta, y = method, color = method, fill = method),
                  size=0.75)+
 xlim(-5,5)+
 scale_fill_manual(values = c("#483d8b50", "#0072B250","#228b2250")) +
 scale_color_manual(values = c("#483d8b", "#0072B2", "#228b22"), guide = "none") +
 stat_density_ridges(data = df, aes(x = beta, y = method, color = method, fill = method),
                  quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), alpha = 0.6, size=0.75)+
scale_y_discrete(expand = expand_scale(add = c(0.1, 0.9)))

Upvotes: 4

Views: 4549

Answers (2)

tjebo
tjebo

Reputation: 23737

You were nearly there. - You need to specify the levels argument within fct_relevel. (See ?fct_relevel: there is no levels argument, but ..., you have to specify its name!)

library(tidyverse)
library(ggridges)
set.seed(3467)

df<- data.frame(method= c(rep("A", 1000), rep("B", 1000), rep("C", 1000)), 
                beta=c(rnorm(1000, mean=0, sd=1),rnorm(1000, mean=2, sd=1.4),rnorm(1000, mean=0, sd=0.5)))

# here is the main change: 
df <- df %>%
  mutate(method = fct_relevel(method, levels = "B", "C", "A"))

  ggplot(df) +
  geom_density_ridges(data = df, aes(x = beta, y = method, color = method, fill = method))
#> Picking joint bandwidth of 0.225

Created on 2020-02-17 by the reprex package (v0.3.0)

Upvotes: 4

Wolfgang Arnold
Wolfgang Arnold

Reputation: 1252

If you change the method column to ordered factor, it should work:

df$method <- factor(df$method, levels = c("C", "A", "B"), ordered = TRUE)

Upvotes: 1

Related Questions