1

I am looking for a package in ggplot to make the following plot.

enter image description here

I know about ggridges but it doesnt have 3 dimensions.

Peter
  • 11,500
  • 5
  • 21
  • 31
ghs101
  • 103
  • 6
  • You might try `ggrgl`. It's a bit experimental but looks powerful enough for what you want. – dash2 Apr 24 '21 at 13:38
  • Try package `plot3D` although this is not a `ggplot` related package. I'm not sure if `ggplot` has a 3D extension, maybe this link will be a pointer: https://stackoverflow.com/questions/45052188/how-to-plot-3d-scatter-diagram-using-ggplot – Peter Apr 24 '21 at 15:08

2 Answers2

1

You could also cheat the x axes and gridlines with ggridges to get the same effect:

library(ggridges)
library(ggplot2)

slant_factor = 5   # How many units x shift per category? Won't work with zero

intercepts = (20 * (0:4)) / slant_factor + 0.8
ggplot(lincoln_weather, 
       aes(x = `Mean Temperature [F]` - as.numeric(Month)*slant_factor, 
           y = Month, fill = stat(x))) +
  
  geom_abline(slope = -1 / slant_factor , 
              intercept = intercepts,
              color = "gray90") +
  
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01, gradient_lwd = 1.) +
  
  scale_x_continuous(expand = c(0, 0),
                     breaks = 20 * (0:4)) +
  scale_y_discrete(expand = expand_scale(mult = c(0.01, 0.25))) +
  scale_fill_viridis_c(name = "Temp. [F]", option = "C") +
  labs(
    title = 'Temperatures in Lincoln NE',
    subtitle = 'Mean temperatures (Fahrenheit) by month for 2016'
  ) +
  theme_ridges(font_size = 13, grid = TRUE) + 
  theme(axis.title.y = element_blank(), panel.grid.major.x = element_blank())

enter image description here

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

two possible options to solve your problem (if I understood it correctly) are the following:

# dummy data
df <- data.frame(grps = factor(sort(rep(c("a","b","c"), 10))),
                 seq = rep(1:10, 3),
                 vlr = c(c(1:6, 4:1), c(1:6, 4:1) + 1, c(1:6, 4:1) + 2))

using plotly:

library(plotly)

fig <- plot_ly(df, x = ~vlr, y = ~seq, z = ~grps, type = 'scatter3d', mode = 'lines',
  transforms = list(
    list(
      type = 'groupby',
      groups = df$grps)))

fig # you have to rotate it

enter image description here

using rayshader to make a line plot into 3D though it will turn into a barchart (the trick for it to work is: the value that defines the height goes into the color asthetic)

library(ggplot2)
library(rayshader)

gl <- ggplot2::ggplot(df, aes(x = grps, y = seq, group = grps, color = vlr)) + 
    ggplot2::geom_line()

rayshader::plot_gg(gl)

enter image description here

ggridges can handle the third dimension when using geom_ridgeline() and informing the height parameter but it seems a bit limited (though I did not read the documentation and how to polish and improve this type of chart:

library(ggplot2)
library(ggridges)

ggplot2::ggplot(df, aes(x = seq, height = vlr, y = grps, color = grps)) + 
  ggridges::geom_ridgeline()

enter image description here

DPH
  • 4,244
  • 1
  • 8
  • 18
  • how do you see the figure you made with ploty? – ghs101 Apr 25 '21 at 08:22
  • @ghs101 not sure I understood the question correctly: I assigned the plot to a variable ("fig"), to visualize it you have to call the variable itself by just typing the name and hitting enter – DPH Apr 25 '21 at 13:09
  • for some strange reason it doesn't show up in my viewer. I also can't open it in a browser. – ghs101 Apr 26 '21 at 05:11
  • @ghs101 I just ran the plotly lines in a clean R session with the dummy data (outside of R-Studio) and it works smootly. Maybe it is just a matter of needing to update/reinstall ggplot2 and plotly or possibly even R itself to the latest version?! Do you get any warnings? Does the graph show using the dummy data but not your own data? – DPH Apr 26 '21 at 15:54