2

I have a dataset with a few organisms, which I would like to plot on my y-axis, against date, which I would like to plot on the x-axis. However, I want the fluctuation of the curve to represent the abundance of the organisms. I.e I would like to plot a time series with the relative abundance separated by the organism to show similar patterns with time.

However, of course, plotting just date against an organism does not yield any information on the abundance. So, my question is, is there a way to make the curve represent abundance using ggridges?

Here is my code for an example dataset:

set.seed(1)
Data <- data.frame(
  Abundance = sample(1:100),
  Organism = sample(c("organism1", "organism2"), 100, replace = TRUE)
)
Date = rep(seq(from = as.Date("2016-01-01"), to = as.Date("2016-10-01"), by = 
'month'),times=10)
Data <- cbind(Date, Data)

ggplot(Data, aes(x = Abundance, y = Organism)) + 
  geom_density_ridges(scale=1.15, alpha=0.6, color="grey90")

This produces a plot with the two organisms, however, I want the date on the x-axis and not abundance. However, this doesn't work. I have read that you need to specify group=Date or change date into julian day, however, this doesn't change the fact that I do not get to incorporate abundance into the plot.

Does anyone have an example of a plot with date vs. a categorical variable (i.e. organism) plotted against a continuous variable in ggridges?

I really like to output from ggridges and would like to be able to use it for these visualizations. Thank you in advance for your help!

Cheers, Anni

Jon Spring
  • 55,165
  • 4
  • 35
  • 53

1 Answers1

2

To use geom_density_ridges, it'll help to reshape the data to show observations in separate rows, vs. as summarized by Abundance.

library(ggplot2); library(ggridges); library(dplyr)
# Uncount copies the row "Abundance" number of times
Data_sum <- Data %>%
  tidyr::uncount(Abundance)

ggplot(Data_sum, aes(x = Date, y = Organism)) + 
  ggridges::geom_density_ridges(scale=1, alpha=0.6, color="grey90")

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • This made all the difference! I have never come across the uncount function before. Thank you so very much! – Anni Djurhuus Oct 14 '18 at 00:23
  • 1
    @AnniDjurhuus: If this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This will help future readers who might run into the same problem. It will also give some reputation to both the answerer and yourself. There is no obligation to do this. – Tung Oct 14 '18 at 04:15