7

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 
Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
user-2147482565
  • 453
  • 7
  • 16

1 Answers1

8

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.

neilfws
  • 32,751
  • 5
  • 50
  • 63
  • 1
    How would you choose an appropriate banwidth? – dzegpi Dec 03 '21 at 15:01
  • 1
    @dzegpi I have come across a method for that once, called the "plug-in" method (Wand, 1995). I haven't read the paper neither did I read papers mentioning this technique, but a quick google scholar search will bring up some. Anyway, this method (up to you to decide if you deem it suitable to your data) is implemented in the KernSmooth package in R with the function 'dpih'. – DiveFreedom Jun 22 '22 at 12:40