Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a continuous density heatmap of 2D scatter data in R?

Tags:

I can generate a density plot of 1D data with:

qplot(mydatapoints, geom='density') 

I've also seen plenty of examples of heatmap grids, but these are more akin to histograms for 1D data in that data goes into discrete buckets instead of showing a smooth curve.

Can I plot something similar to the 1D density but for 2D data, with (say) something like hue/saturation/lightness to represent the density?

like image 788
Yang Avatar asked Aug 16 '11 03:08

Yang


People also ask

What is 2d density plot?

Density can be represented in the form of 2D density graphs or density plots. A 2d density chart displays the relationship between 2 numeric variables, where one variable is represented on the X-axis, the other on the Y axis, like for a scatterplot.

How to create a heatmap in R?

The most common function for creating heatmaps in R is the heatmap () function, which is already provided by the base installation of R. The heatmap function is applied as shown below:

How do you make a heat map in Excel?

The heatmap function. The heatmap function has the form of heatmap (x, scale, na.rm, col, labRow, labCol, main) and it produces a heat map of the data. x is the numeric matrix containing the values being used in creating the heat map. col is the color palette to be used by the heat map.

How do I plot a heatmap in Plotly?

The plotly package contains the plot_ly function, which can be used to draw a heatmap by specifying type = “heatmap”: Figure 6: Default Heatmap in plotly Package. Again, the patter is the same, but the general plot style is different.

What is a heat map in data science?

Data science has many graphical ways of displaying data, and each provides unique types of information about the data being presented. If you want to see how a field of data is spread over a given area, a heat map is a useful tool. Heat maps illustrate the distribution of data in color changes over the surface that is being illustrated.


2 Answers

I think you want a 2D density estimate, which is implemented by kde2d in the MASS package.

df <- data.frame(x=rnorm(10000),y=rnorm(10000)) 

via MASS and base R:

k <- with(df,MASS:::kde2d(x,y)) filled.contour(k) 

via ggplot (geom_density2d() calls kde2d())

library(ggplot2) ggplot(df,aes(x=x,y=y))+geom_density2d() 

I find filled.contour more attractive, but it's a big pain to work with if you want to modify anything because it uses layout and takes over the page layout. Building on Brian Diggs's answer, which fills in colours between the contours: here's the equivalent with different alpha levels, with transparent points added for comparison.

ggplot(df,aes(x=x,y=y))+   stat_density2d(aes(alpha=..level..), geom="polygon") +   scale_alpha_continuous(limits=c(0,0.2),breaks=seq(0,0.2,by=0.025))+   geom_point(colour="red",alpha=0.02)+   theme_bw() 

enter image description here

like image 68
Ben Bolker Avatar answered Sep 28 '22 16:09

Ben Bolker


Combining two other answers (one pointing to geom_density2d and one giving sample data and scale_colour_gradient):

df <- data.frame(x=rnorm(10000),y=rnorm(10000)) ggplot(df,aes(x=x,y=y))+     stat_density2d(aes(fill=..level..), geom="polygon") +     scale_fill_gradient(low="blue", high="green") 
like image 36
Brian Diggs Avatar answered Sep 28 '22 15:09

Brian Diggs