rggplot2ridgeline-plot

Ridge plot curves not appearing in plot in r


I'm trying to recreate this ridge plot:

https://i.sstatic.net/QAFs1.jpg

However, I can't seem to be able to get the curves to show up on the plot, As you may have guessed I'm very new to r, so I pretty much copied https://bytefish.de/blog/timeseries_databases_5_visualization_with_ggridges/ to get where I am.

Here's the code:

eustockmark_index <- fortify.zoo(EuStockMarkets)

eustockmark_index$Index=as.factor(eustockmark_index$Index)
eustockmark_index$Index=factor(eustockmark_index$Index,levels=c(1991,1992,1993,1994,1995,1996,1997,1998))
eustockmark_index$Index= trimws(eustockmark_index$Index)

eustockmark_index

ggplot(eustockmark_index, aes(x = `DAX`, y =`Index`,fill=..x..)) +
  geom_density_ridges_gradient() +
  labs(title = "DAX",
       x = " ",
       y = " ")+
  scale_fill_viridis(option = "C")

Also if any one could explain why my Y axis has NA at the top, that would be marvelous. Thanks very much!


Solution

  • This appears to recreate your plot using dplyr:

    library(datasets)
    library(tidyverse)
    library(ggridges)
    library(zoo)
    
    data <- fortify.zoo(EuStockMarkets) %>% mutate(Index = as_factor(floor(Index)))
    
    ggplot(data, aes(x = DAX, y = Index, fill = ..x..)) +
      geom_density_ridges_gradient() +
      scale_fill_viridis_c(option = "C",
                           direction = -1,
                           guide = "none") +
      labs(title = "DAX", x = "", y = "")
    

    Output

    Options direction = -1 and guide = "none" for scale_fill_viridis_c are not essential, but appear to be used in the plot shown. Comment out as required.

    Please, in the future, produce a MWE when asking questions.