Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 2d Density Weights

I'm trying to plot some data with 2d density contours using ggplot2 in R.

I'm getting one slightly odd result.

First I set up my ggplot object:

p <- ggplot(data, aes(x=Distance,y=Rate, colour = Company))

I then plot this with geom_points and geom_density2d. I want geom_density2d to be weighted based on the organisation's size (OrgSize variable). However when I add OrgSize as a weighting variable nothing changes in the plot:

This:

p+geom_point()+geom_density2d()

Gives an identical plot to this:

p+geom_point()+geom_density2d(aes(weight = OrgSize))

However, if I do the same with a loess line using geom_smooth, the weighting does make a clear difference.

This:

p+geom_point()+geom_smooth()

Gives a different plot to this:

p+geom_point()+geom_smooth(aes(weight=OrgSize))

I was wondering if I'm using density2d inappropriately, should I instead be using contour and supplying OrgSize as the 'height'? If so then why does geom_density2d accept a weighting factor?

Code below:

require(ggplot2)

 Company <- c("One","One","One","One","One","Two","Two","Two","Two","Two")
 Store <- c(1,2,3,4,5,6,7,8,9,10)
 Distance <- c(1.5,1.6,1.8,5.8,4.2,4.3,6.5,4.9,7.4,7.2)
 Rate <- c(0.1,0.3,0.2,0.4,0.4,0.5,0.6,0.7,0.8,0.9)
 OrgSize <- c(500,1000,200,300,1500,800,50,1000,75,800)

 data <- data.frame(Company,Store,Distance,Rate,OrgSize)

 p <- ggplot(data, aes(x=Distance,y=Rate))

 # Difference is apparent between these two
 p+geom_point()+geom_smooth()
 p+geom_point()+geom_smooth(aes(weight = OrgSize))

 # Difference is not apparent between these two
 p+geom_point()+geom_density2d()
 p+geom_point()+geom_density2d(aes(weight = OrgSize))
like image 846
Tumbledown Avatar asked Oct 16 '25 15:10

Tumbledown


1 Answers

geom_density2d is "accepting" the weight parameter, but then not passing to MASS::kde2d, since that function has no weights. As a consequence, you will need to use a different 2d-density method.

(I realize my answer is not addressing why the help page says that geom_density2d "understands" the weight argument, but when I have tried to calculate weighted 2D-KDEs, I have needed to use other packages besides MASS. Maybe this is a TODO that @hadley put in the help page that then got overlooked?)

like image 127
IRTFM Avatar answered Oct 18 '25 08:10

IRTFM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!