0

I'm trying to recreate this ridge plot:

https://i.stack.imgur.com/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!

Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
  • 2
    `eustockmark_index` has years that are decimal numbers (e.g., 1991.496) - not rounded integers...so when you create a factor and make the levels based on whole numbers for years (e.g., 1991, 1992, ...) there will be no years that are exactly those whole numbers. If you use something like `floor` you can get rid of the decimal and get the year integer... – Ben Nov 02 '19 at 14:51
  • Please do not deface your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. – GrumpyCrouton Nov 06 '19 at 14:07

1 Answers1

2

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.

MSR
  • 2,731
  • 1
  • 14
  • 24