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

  • SEARCH
  • Home
  • 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 7755791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:39:10+00:00 2026-06-01T12:39:10+00:00

I am trying to create a scatterplot with binned x-axis for binary data. When

  • 0

I am trying to create a scatterplot with binned x-axis for binary data. When I use geom_point with binary y, the plot is pretty useless (see figure 1). As shown in figure 2, I want to bin the data based on the values of the x-axis and then plot the avg x and avg y within each bin using geom_point (mapping the the number of obs in each bin to the size of the point). I can do this by aggregating the data but I was wondering whether ggplot can do it directly. I played around with stat_bindot etc. but wasn’t able to find a solution. Any ideas? Below is some code.

Thanks!

# simulate data
n=1000
y=rbinom(n,1,0.5)
x=runif(n)
data=data.frame(x,y)

# figure 1 - geom_point with binary data, pretty useless!
ggplot(data,aes(x=x,y=y)) + geom_point() + ylim(0,1)

# let's create an aggregated dataset with bins
bin=cut(data$x,seq(0,1,0.05))
# I am sure the aggregation can be done in a better way...
data.bin=aggregate(data,list(bin),function(x) { return(c(mean(x),length(x)))})

# figure 2 - geom_point with binned x-axis, much nicer!
ggplot(data.bin,aes(x=x[,1],y=y[,1],size=x[,2])) + geom_point() + ylim(0,1)

Figures 1 and 2:


  • 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-01T12:39:11+00:00Added an answer on June 1, 2026 at 12:39 pm

    As @Kohske said, there is no direct way to do that in ggplot2; you have to pre-summarize the data and pass that to ggplot. Your approach works, but I would have done it slightly differently, using the plyr package instead of aggregate.

    library("plyr")
    data$bin <- cut(data$x,seq(0,1,0.05))
    data.bin <- ddply(data, "bin", function(DF) {
      data.frame(mean=numcolwise(mean)(DF), length=numcolwise(length)(DF))
    })
    ggplot(data.bin,aes(x=mean.x,y=mean.y,size=length.x)) + geom_point() + 
      ylim(0,1)
    

    enter image description here

    The advantage, in my opinion, is that you get a simple data frame with better names this way, rather than a data frame where some columns are matrices. But that is probably a matter of personal style than correctness.

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

Sidebar

Related Questions

I'm currently trying to use the core-plot library to graph some data I get
I'm trying to use ggplot2 to create and label a scatterplot. The variables that
I'm trying to plot two sets of data wrt to time on the same
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
(I'm new to html5... so use small words) I'm trying to animate a scatterplot
I am trying to create a simple scatter plot with PlotSymbols for when I
I'm trying create a regex that verifies an xml entity name is valid (see
I am trying create a data.frame from which to create a graph. I have
I am trying create a WCF Data Services ServiceOperation that does grouping on the
I have been trying to plot a map on axmapcontrol and use the same

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.