4

I have produced a chart using ggplot2 and the geom_freqpoly function. I am not able to post an image but hopefully I can describe my issue.

My chart displays two lines for the number of observations at given points along an x-axis which is in increments of 0.50 and values in my data only exists along these 0.50 intervals.

I have created this example data set to hopefully illustrate:

AvgMargin <- c(0.00, 0.50, 2, 1, 1, 0.5, 0.5)
Median <- as.factor(c("High", "Low", "Low", "High", "High", "Low", "Low"))
Matches <- data.frame(AvgMargin, Median)

the code I have used is as follows:

ggplot(Matches, aes(AvgMargin, colour=Median)) + geom_freqpoly(binwidth=0.5) + scale_x_continuous(breaks=-5:5)

The problem I have is that the peaks of my lines do not correspond with the values I would expect on the x axis. The values on my x axis are only in increments of 0.50 yet I seem to have peaks inbetween these points (for at 0.25 and 0.75 but I have no values of 0.25 and 0.75 in my data).

What I would like to know is how do I get my lines to correspond with my x axis please?

user3198404
  • 127
  • 11
  • 3
    A reproducible example (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is much better than the description. Post the dataset you use and providing help will be much easier. – tonytonov Jan 15 '14 at 14:19
  • Hi, I cannot post my dataset but have edited my question with an example that hopefully can be reproduced. thanks – user3198404 Jan 15 '14 at 14:41
  • That is even better, by the way! – tonytonov Jan 15 '14 at 14:45
  • So what values are you expecting on your x axis? Increments of 0.25? – Wilmer E. Henao Jan 15 '14 at 14:46
  • Hi, the values on my x-axis are correct (0.00, 0.50, 1.00 etc) but I expect the peaks of my lines to correspond to these 0.50 increments. So in the example above my red line ("High") currently peaks at 0.25 and 1.25 whereas I would expect it to be at 0.00 and 1.00. Similarly, the blue line ("Low") peaks at 0.75 and 2.25 when I would expect it to be at 0.50 and 2.00 – user3198404 Jan 15 '14 at 14:51

2 Answers2

4

From the answers I have received I now have code of:

ggplot(Matches, aes(AvgMargin, colour=Median)) + geom_freqpoly(binwidth=0.5, origin=-0.25)

This gives me the following output:

enter image description here

The argument "origin" tells the bins to start at -0.25 therefore the midpoints and peaks now fall on the increments I want

user3198404
  • 127
  • 11
0

It's because of the way you're setting binwidth. Try this:

ggplot(Matches, aes(AvgMargin, colour=Median)) + 
  geom_freqpoly() + 
  xlim(-5,5)

ggplot counts AvgMargin into bins and plots the peaks at the center of each bin. Since your data ranges from [0,2], if you set binwidth=0.5, you get bins at [0,0.5), [0.5,1), [1,1.5), [1.5,2), and [2,2.5). The peaks are plotted at 0.25, 0.75, 1.25, and 1.75, and 2.25. If you leave out the binwidth= parameter, it defaults to range/30, or about 0.066; the first bin will be centered at 0.033 and the last will be centered at 2.033.

So, the smaller you make binwidth, the closer the peaks will be to the positions you want (but also, the narrower the polygons will be):

ggplot(Matches, aes(AvgMargin, colour=Median)) + 
  geom_freqpoly(binwidth=0.0001) 

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Hi thank you for replying. I still can't get the lines to look as I expect though. I would expect the peaks (and dips) of each line to correspond with the points on my x-axis (and the corresponding gridlines). These are in 0.50 increments – user3198404 Jan 15 '14 at 16:24
  • thanks for your help. Your answer prompted me to look up how to get the bins to start at different points (not just at zero). I still want them 0.50 width but with the peaks on the 0.50 intervals. I thing the "origin" argument achieves this: ggplot(Matches, aes(AvgMargin, colour=Median)) + geom_freqpoly(binwidth=0.5, origin=-0.25) – user3198404 Jan 15 '14 at 17:26
  • You should post this as an answer to your own question. This type of thing is encouraged on SO. – jlhoward Jan 15 '14 at 17:31
  • I will do. Thank you for your help, much appreciated – user3198404 Jan 15 '14 at 17:36