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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:45:52+00:00 2026-06-17T11:45:52+00:00

Suppose I need to plot a dataset like below: set.seed(1) dataset <- sample(1:7, 1000,

  • 0

Suppose I need to plot a dataset like below:

set.seed(1)
dataset <- sample(1:7, 1000, replace=T)
hist(dataset)

As you can see in the plot below, the two leftmost bins do not have any space between them unlike the rest of the bins.

enter image description here

I tried changing xlim, but it didn’t work. Basically I would like to have each number (1 to 7) represented as a bin, and additionally, I would like any two adjacent bins to have space beween them…Thanks!

  • 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-17T11:45:53+00:00Added an answer on June 17, 2026 at 11:45 am

    The best way is to set the breaks argument manually. Using the data from your code,

    hist(dataset,breaks=rep(1:7,each=2)+c(-.4,.4))
    

    gives the following plot:

    enter image description here

    The first part, rep(1:7,each=2), is what numbers you want the bars centered around. The second part controls how wide the bars are; if you change it to c(-.49,.49) they’ll almost touch, if you change it to c(-.3,.3) you get narrower bars. If you set it to c(-.5,.5) then R yells at you because you aren’t allowed to have the same number in your breaks vector twice.

    Why does this work?

    If you split up the breaks vector, you get one part that looks like this:

    > rep(1:7,each=2)
     [1] 1 1 2 2 3 3 4 4 5 5 6 6 7 7
    

    and a second part that looks like this:

    > c(-.4,.4)
     [1] -0.4  0.4
    

    When you add them together, R loops through the second vector as many times as needed to make it as long as the first vector. So you end up with

      1-0.4  1+0.4  2-0.4  2+0.4  3-0.4  3+0.4 [etc.]
    =   0.6    1.4    1.6    2.4    2.6    3.4 [etc.]
    

    Thus, you have one bar from 0.6 to 1.4–centered around 1, with width 2*.4–another bar from 1.6 to 2.4 centered around 2 with with 2*.4, and so on. If you had data in between (e.g. 2.5) then the histogram would look kind of silly, because it would create a bar from 2.4 to 2.6, and the bar widths would not be even (since that bar would only be .2 wide, while all the others are .8). But with only integer values that’s not a problem.

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

Sidebar

Related Questions

Suppose you need to create a 'top' of some sort and have code like
Suppose I need to update myTab from luTab as follows update myTab set LookupVale
Suppose I need to set an opacity mask on a WPF control that highlights
Suppose I need to inherit from two classes in C# . This is not
Suppose I need several CCSprite s using the same image. I can think of
Suppose I need to write an onclick handler in an attribute , like this:
Suppose I need to make a CDROM with a Tomcat server. How can I
Suppose I need to store 1000 objects in Hashset, is it better that I
I need to create a movie. Suppose, I create an axis and plot on
Suppose i need to create an object as follows and set some values FileMetaData

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.