0

im trying to plot Historam using R

Data is like that:

myData <- c(1,1,1,1,1,1,1,5,5,15000)

lets say I divided it into 3 buckets:

1-3 4-6 7-20000

so as a result the plot whould look like this:

-     
-     
-     
-     
-     
-     -
-     -     - 
1-3   3-6   6-20000   

So I did sth like this:

hist(myData, breaks=c(1,3,6,20000), right=TRUE)

But the x axis in the result is so long that it is imposible to make histogram plot readeable.

Is it possible to make x scale non linear in this example just to show that there are 3 buckets and not 1-20000 long x axis scale ?

thank You very much for help

smci
  • 32,567
  • 20
  • 113
  • 146
gruber
  • 28,739
  • 35
  • 124
  • 216
  • The issue is not that the x-axis is long, but that your data is very right-skewed, i.e. it has a long tail extending to the right. – smci May 30 '17 at 05:34

2 Answers2

3

You can use cut, rather than trying to manipulate hist directly :

myData <- c(1,1,1,1,1,1,1,5,5,15000)

data <- data.frame(myData)

data <- transform(data, groupdata = cut(myData,
                                        breaks=c(1,3,6,20000),
                                        right=TRUE,include.lowest = TRUE))
library(ggplot2)
qplot(x = groupdata, data = data, stat = "bin")

homemade histogram

AndrewMacDonald
  • 2,870
  • 1
  • 18
  • 31
  • Oh great, but Ive got 2 questions: can I add labels to the x values ? for example: <=3, <=6, > 6 ? And second question: can I get numbers of each class obejcts ? so in this case it would be: 7, 2, 1. And hot to change count label w y axis ? – gruber Jun 28 '14 at 17:07
  • Oh for y axis it will be parameter: ylab :D – gruber Jun 28 '14 at 17:10
  • Oh I know that ading labels may be done by labels parameter in cut, so the only thing left is hot to determine number ob objects in each class – gruber Jun 28 '14 at 17:13
  • 1
    all you had to do was `barplot(table(data$groupdata))`, why would you change the solution to ggplot when the question was in base R? – rawr Jun 28 '14 at 17:16
  • You're quite right, @rawr, that would also work -- and it seems like @gruber is more comfortable in base anyway. I admit it -- I've been thinking in `ggplot` for so long I've forgotten some base-basics :P – AndrewMacDonald Jun 28 '14 at 17:20
  • Ok but How can I then get exact number of objects in each class ? – gruber Jun 28 '14 at 17:23
  • int @rawr soution it would be table(data$groupdata) but in AndrewMacDonald 's ? – gruber Jun 28 '14 at 17:26
  • yeah just do that after the `transform` step and @AndrewMacDonald, I can empathize. for me there was obviously a learning curve for ggplot, and I only thought in that for the longest time. recently, I have gone back to using minimal add-on packages, and very much prefer the ease of tweaking small details in base graphics – rawr Jun 28 '14 at 17:28
  • @rawr, ok but then I get sth like in the screenshot, I dont get sum of all elements in given interval: http://pl.tinypic.com/view.php?pic=f1hbx1&s=8#.U68Gbvl_scM – gruber Jun 28 '14 at 18:17
1
yourData <- c(1,1,1,1,1,1,1,5,5,15000)

adapting from here:

h = hist(yourData,breaks = c(1,3,6,20000))
plot(h$counts,log="x",type='h',lwd=3,lend=2)
Community
  • 1
  • 1
ftabaro
  • 136
  • 11