Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How to : 3d Density plot with gplot and geom_density

Tags:

r

3d

density-plot

I'm trying to combine multiple density plots with overlay. ggplot and geom_density do the job, but the densities are stacked on top of each other.This is overlaid but not 3d

ggplot(all.complete, aes(x=humid_temp)) +  
  geom_density(aes(group=height, colour=height, fill=height.f, alpha=0.1)) + 
  guides(fill = guide_legend(override.aes = list(colour = NULL))) +
  labs(main="Temperature by Height", x="Temperature", y="Density")

Something similar to this is what I'm trying to achieve: Density? with 3d overlay

In my case, the years would be substituted by height.

Thanks!!!

like image 384
rafaelvalle Avatar asked Sep 23 '14 00:09

rafaelvalle


People also ask

What is a density plot Ggplot?

Data Visualization using GGPlot2. A density plot is an alternative to Histogram used for visualizing the distribution of a continuous variable. The peaks of a Density Plot help to identify where values are concentrated over the interval of the continuous variable.

How do you fill a density plot with color in R?

Change fill colors It is also possible to change manually density plot fill colors using the functions : scale_fill_manual() : to use custom colors. scale_fill_brewer() : to use color palettes from RColorBrewer package. scale_fill_grey() : to use grey color palettes.

How do I use ggMarginal?

ggExtra comes with an addin for ggMarginal() , which lets you interactively add marginal plots to a scatter plot. To use it, simply highlight the code for a ggplot2 plot in your script, and select ggplot2 Marginal Plots from the RStudio Addins menu.


1 Answers

The chart you are looking for is called a ridgelineplot. Try the ggridges package with ggplot2.

An example with temporal data:

library(viridis)
ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = `Month`, fill = ..x..)) +
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01, gradient_lwd = 1.) +
  scale_x_continuous(expand = c(0.01, 0)) +
  scale_y_discrete(expand = c(0.01, 0)) +
  scale_fill_viridis(name = "Temp. [F]", option = "C") +
  labs(title = 'Temperatures in Lincoln NE',
       subtitle = 'Mean temperatures (Fahrenheit) by month for 2016\nData: Original CSV from the Weather Underground') +
  theme_ridges(font_size = 13, grid = TRUE) + theme(axis.title.y = element_blank())

Here is the result :

enter image description here

like image 74
Benoit Lamarsaude Avatar answered Oct 13 '22 19:10

Benoit Lamarsaude