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 8282071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:14:56+00:00 2026-06-08T10:14:56+00:00

Using ggplot2 , I want to create a histogram where anything above X is

  • 0

Using ggplot2, I want to create a histogram where anything above X is grouped into the final bin. For example, if most of my distribution was between 100 and 200, and I wanted to bin by 10, I would want anything above 200 to be binned in “200+”.

# create some fake data    
id <- sample(1:100000, 10000, rep=T)
visits <- sample(1:1200,10000, rep=T)

#merge to create a dataframe
df <- data.frame(cbind(id,visits))

#plot the data
hist <- ggplot(df, aes(x=visits)) + geom_histogram(binwidth=50)

How can I limit the X axis, while still representing the data I want limit?

  • 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-08T10:14:57+00:00Added an answer on June 8, 2026 at 10:14 am

    Perhaps you’re looking for the breaks argument for geom_histogram:

    # create some fake data    
    id <- sample(1:100000, 10000, rep=T)
    visits <- sample(1:1200,10000, rep=T)
    
    #merge to create a dataframe
    df <- data.frame(cbind(id,visits))
    
    #plot the data
    require(ggplot2)
    ggplot(df, aes(x=visits)) +
      geom_histogram(breaks=c(seq(0, 200, by=10), max(visits)), position = "identity") +
      coord_cartesian(xlim=c(0,210))
    

    This would look like this (with the caveats that the fake data looks pretty bad here and the axis need to be adjusted as well to match the breaks):

    manual breaks on histogram

    Edit:

    Maybe someone else can weigh in here:

    # create breaks and labels
    brks <- c(seq(0, 200, by=10), max(visits))
    lbls <- c(as.character(seq(0, 190, by=10)), "200+", "")
    # true
    length(brks)==length(lbls)
    
    # hmmm
    ggplot(df, aes(x=visits)) +
      geom_histogram(breaks=brks, position = "identity") +
      coord_cartesian(xlim=c(0,220)) +
      scale_x_continuous(labels=lbls)
    

    The plot errors with:

    Error in scale_labels.continuous(scale) : 
      Breaks and labels are different lengths
    

    Which looks like this but that was fixed 8 months ago.

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

Sidebar

Related Questions

I am trying to produce some example graphics using ggplot2, and one of the
I want to create a scatter plot with regression line, while using size aesthetics
I have created a graph using ggplot2. Now I want to have the legend
I am trying to create a barplot using ggplot2, with the y axis starting
I am trying to produce a heat map using ggplot2. I found this example
I want to create intervals (discretize/bin) of continuous variables to plot a choropleth map
I want to position a plotmath symbol (x bar) in a plot using ggplot2.
Possible Duplicate: Create a bar graph with pre-summarized data using ggplot2 On ggplot2 reference
I'm using ggplot2 to make some bullseye charts in R. They look delightful, and
I am using ggplot2 to plot a figure that contains nine facets. Each facet

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.