0

So I have a vector of integers, quotes, which I wish to see whether it observes a power law distribution by plotting the frequency of data points and making both the x and y axes logarithmic. However, I am not quite sure how to accomplish this in R. I can currently create a histogram using

hist(quotes, breaks = max(quotes))

But the axes are all linear.

wrongusername
  • 18,564
  • 40
  • 130
  • 214

1 Answers1

3

There's probably a better way to do this, but this (basically) works:

data = rnorm(1000,0,1)
r <- hist(log(data))
plot(r$breaks[-1],log(r$counts))

EDIT: Better solution:

r <- hist(data)
plot(r$breaks[-1], r$counts, log='xy', type='h')
# or alternatively:
barplot(r$counts, log="y", col="white", names.arg=r$breaks[-1])

The barplot version doesn't have a transformed x axis for reasons that will become clear if you try it with the x axis transformed.

David Marx
  • 8,172
  • 3
  • 45
  • 66
  • a histogram is just a scatterplot. You can use `type='h'` to make this into a thin bar chart. Also, I left out the $breaks in the second version, but I think you still want them. Modified my response. – David Marx Feb 21 '13 at 23:16
  • Aha, thanks, that works well! By the way, what is the reason behind removing the first element from breaks? – wrongusername Feb 22 '13 at 00:03
  • The breaks vector is one element longer than the counts vector. Just lopping off one end or other from the vector works but is sort of lazy: what you really want is the median value between each pair of "breaks." I'll leave that as an exercise to the reader ;) – David Marx Feb 22 '13 at 05:35
  • What does the r$breaks[-1] do? – Phil Goetz Dec 02 '16 at 21:50
  • @PhilGoetz I already answered this in the comments. Look at the comment directly above yours. – David Marx Dec 05 '16 at 16:52