Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8874911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:50:26+00:00 2026-06-14T18:50:26+00:00

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

  • 0

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))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:50:28+00:00Added an answer on June 14, 2026 at 6:50 pm

    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?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.