Canadian_Marine
Canadian_Marine

Reputation: 539

Using geom_density_ridges with log scale

I am plotting out some ocean depth survey data and using geom_density_ridges to display the depth distribution for different regions. My problem is that the data for each region is very spiky, which means that when the graph is plotted I get a rather unpleasant looking graph like this:

Depth distribution by region and source

What I would like to do is impose a log scale on the histogram so that it flattens out a bit, but I cannot figure out how to tell ggplot to do this.

Here's some sample code. I can't share the exact code for proprietarty reasons, but the following code is in the same format as the code that generated the above figure:

rbind(
  data.frame(x=seq(1, 1000), y=rnorm(100, 12, 0.05), z='A'),
  data.frame(x=seq(1, 1000), y=rnorm(100, 10, 0.05), z='B')
) %>%
  ggplot(aes(x=y, y=z)) + 
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01)

Which generates the following figure:

Distribution of depths by source (mock data)

How could I change this code so that the height of the histogram ridges are displayed on a logarithmic scale?

Upvotes: 0

Views: 671

Answers (1)

Matt
Matt

Reputation: 7385

Here's a quick solution using cowplot. I'm not sure how to accomplish your goal using a log scale, but changing up the plotting spreads out the ridges and makes them easier to read.

library(tidyverse)
library(ggridges)
library(cowplot)
plot_func <- function(data){
  
  data %>% 
  ggplot(aes(x=y, y=z)) + 
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01)
  
}

a <- data.frame(x=seq(1, 1000), y=rnorm(100, 12, 0.05), z='A') %>% plot_func(.)
b <- data.frame(x=seq(1, 1000), y=rnorm(100, 10, 0.05), z='B') %>% plot_func(.)

plot_grid(a,b,nrow = 2)

You can clean up your code to align the x-axis or other scaling if needed. This gives you:

enter image description here

Upvotes: 1

Related Questions