I am looking for a package in ggplot to make the following plot.
I know about ggridges but it doesnt have 3 dimensions.
I am looking for a package in ggplot to make the following plot.
I know about ggridges but it doesnt have 3 dimensions.
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())
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
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)
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()