Search code examples
rggplot2ggridgesridgeline-plot

How to adjust bandwidth for ridgeplots in R


When using the geom_density_ridges function in the ggridges package, it always picks a bandwidth for all the densities in the plot. But is there a way to adjust the bandwidth that it chooses?

I currently have some code to make a ridge plot, but the bandwidth is too low for the bottom density. I would like to adjust it so that it is smoother and less rough.

enter image description here

Here is my code:

hier_plot <- ggplot(hier_df, aes(x=x, y=as.factor(beta), fill = factor(beta))) +
  theme(axis.title = element_text(size = 15),
        axis.text = element_text(size = 15),
        legend.text = element_text(size = 10),
        panel.background = element_rect(fill = "#fffffC")) +
  labs(y = expression(beta), x = 'x', expression(beta), fill = expression(beta)) +
  geom_density_ridges(scale = 2.5) +
  scale_x_continuous(expand = c(0.01, 0)) +
  scale_y_discrete(expand = c(0.05, 0)) +
  scale_fill_brewer(palette = 'Reds')

hier_plot 

Solution

  • Pretty sure you can just add it as an argument to geom_density_ridges() e.g.

    + geom_density_ridges(bandwidth = 0.1)
    

    The argument is passed to the underlying function stat_density_ridges.