ggridges - height of the rides

Hi people,
I'm trying to construct a graphic with ggridges as following:

image

But, for each element in the y-axis I have different values. For instance the amount of A624 is 5.504 and for A674 I have just 21.

I'd like to control the heigth of the ridges by thes amount to make proportional. That means the last one would be less tall than the first one.

How could I do this? Thank for those who can help

library(tidyverse)
library(ggridges)
library(viridis)

#
dt <- read_csv("https://raw.githubusercontent.com/ronycoelho/databases/master/data_councils.csv")

# Count each element
councils_qt <- dt %>% 
  count(councils) %>% 
  arrange(desc(n)) %>% 
  mutate (councils_qt = paste0(councils, " (", n, ")"))

# Join to make labels
dt <- dt %>% 
  full_join(councils_qt)

# Plot
plot <- ggplot(dt, 
               aes(x=years, y=reorder(councils_qt, -years, FUN = mean)))+
  geom_density_ridges(aes(fill=reorder(councils_qt, -years, FUN = mean)), 
                      show.legend = F, alpha = .7, panel_scaling = F) +
  scale_fill_viridis_d(option = "B", direction = -1,
                           begin = .1, end = .9)+
  scale_x_continuous(limits = c(1988, 2018), 
                     breaks = c(1990, 1994, 1998, 2002, 2006, 2010, 2014))+
  #scale_y_discrete(labels = c())+
  geom_vline(xintercept = c(1990, 1994, 1998, 2002, 2006, 2010, 2014), linetype= "dotdash") +
  ylab("") + xlab("")+
  theme_classic()+
  theme(axis.text.x = element_text(size = 14, face = "bold"),
        axis.text.y = element_text(size = 14, face = "bold"))

plot

The {ggridges} package draws density curves which are by definition scaled to an area of 1. Thus, the height is an outcome from your data and shouldn't be changed. You likely need a different approach which does not use density curves at all.

1 Like

Thank you, Z3tt! You are rigth! I have to find other way.
I've tried to use the ´´´height´´´ argument, like that:

ggplot(dt, aes(x=years, 
               y=reorder(n, -years, FUN = mean),
               height = n ))+
    geom_density_ridges()

It apply the proportion, but I lose the ridges. Thanks, anyway! (and, if you have any other suggestion...)

Yes, I have played around with all this a long time ago, No, for now I have no other suggestion. I think you end up using some area charts for each with y mapped to n.

If this topic is closed for you "please remember to mark the answer, which solves your particular challenge."

It is not perfect, but it is a way! Give me the efect that I want. Thank you for the conversation!

ggplot(dt, aes(x=years))+
  geom_bar(aes(y = ..count../sum(..count..)))+
  geom_density(aes(y = ..density../5))+
  scale_x_continuous(limits = c(1988, 2018), 
                     breaks = c(1990, 1994, 1998, 2002, 2006, 2010, 2014))+
  geom_vline(xintercept = c(1990, 1994, 1998, 2002, 2006, 2010, 2014),
             linetype= "dotdash")+
  facet_wrap(~ councils_qt, nrow = 14, strip.position = "left")+
  labs(x = "", y = "")+
  theme_classic()+
  theme(
  axis.text.y =   element_blank(),
  axis.line.y.left = element_blank(),
  axis.ticks.y.left = element_blank(),  
  strip.text.y.left = element_text(angle = 0),
  strip.placement = "inside",
  strip.text.x = element_blank()
)
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.