0

I'm trying to make a histogram using basic R. The histogram is doing a good job representing the data. However, I want to combine the few bars on the right-side(from 300,000+)( here is an image enter image description here) into one bar to represent their values.

hist(TotalIncomeRural$D21, breaks ="sturges", labels = TRUE,
       xaxt='n', ylim=c(0,1350), col = "red")
axis(side=1, at=c(seq(0,1500000,100000)))

any help is very much appreciated.

Edit: I'll demonstrate what I want to do with the histogram: suppose I have this data and hist function:

x<-c(1,3,5,6,8,9,10,11,13,15,1,3,8,10,1,6,8,2,0, 2,5,3,8,5,5,1,0,0,0,0,3,6,5,8,10,12,16,18, 9, 8,1,1,1,0,5,5,5,6)
hist(x)

when I run this new histogram I'd like to combine all the values above 10 into one bin to represent them (I'm attaching an image here).

enter image description here

r2evans
  • 141,215
  • 6
  • 77
  • 149
j_sands
  • 3
  • 2
  • Hi j_sands. It's always better to give a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) that way you can help others to help you! Also I am not sure if I understand your question: Do you want to have bins that have different sizes? – dario Feb 08 '20 at 17:11
  • Possibly useful? https://stackoverflow.com/q/37766893 – r2evans Feb 08 '20 at 18:58
  • Though the other answer demonstrated some new features for you, did you try just `hist(x, breaks = c(0, 2, 4, 6, 8, 10, 20))`? It gives (I think) precisely what you show in your drawing. – r2evans Feb 08 '20 at 21:52

1 Answers1

0

You can specify the breaks and axis labels:

hist( TotalIncomeRural$D21, 
      breaks = c(0:3, 16) * 100000,
      ylim = c(0, 1350),
      xlim = c(0, 400000),
      freq = T,
      xaxt = "n",
      col = "red")

axis( side = 1, 
      at = (0:3 +.5) * 100000, 
      tick = F,
      labels = c("\n< $100,000\n ", 
                 "\n$100,000 -\n$200,000", 
                 "\n$200,000 -\n$300,000", 
                 "\n> $300,000\n "))

enter image description here

I tried to mimic your original data like this:

set.seed(69)
TotalIncomeRural <- data.frame(D21 = rexp(2000)^1.3 * 100000)

If you wanted to, you could have one big wide bar at the right by changing the xlim of the axis, but then the labels wouldn't fit as well (and the area of the histogram would be wrong)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • could you briefly explain how (breaks = c(0:3, 16) * 100000,) works? – j_sands Feb 08 '20 at 20:12
  • especially, why you used 16? so I can use it when i create other histograms. thank you – j_sands Feb 08 '20 at 20:14
  • @j_sands That was just so all my random data fitted in. The expression c(0:3, 16) * 100000 expands to (0, 100000, 200000, 300000, 1600000), which means that's where the breaks were. The highest number in my random data was about 1530000, so I just needed to make sure the highest break was higher than that. – Allan Cameron Feb 08 '20 at 20:51