3

I have some data that i want to make a histogram of. However, I want to represent this histogram with line. I have tried using the freq_poly of the ggplot2. However, the line produced is pretty jagged. I want to know if it is possible to use the splines with ggplot2, so that the line produced in freq_poly will be smoother.

d <- rnorm( 1000 )
h <- hist( d, breaks="FD", plot=F )
plot( spline( h$mids, h$counts ), type="l" )

This is what i want to accomplish. But I want to do it using ggplot2.

Sam
  • 7,922
  • 16
  • 47
  • 62
  • 3
    Some combination of `geom_smooth` and `geom_density` are probably what you want. Can you please provide a reproducible example? – Ben Bolker May 23 '11 at 20:36

1 Answers1

4

I'm assuming that you are trying to use the spline() function. If not, disregard this answer.

spline() returns a list object of two components, x and y:

List of 2
 $ x: num [1:93] -3.3 -3.23 -3.17 -3.1 -3.04 ...
 $ y: num [1:93] 1 0.1421 -0.1642 -0.0228 0.4294 ...

We can simply turn these into a data.frame and plot them There may be a fancier ways to do this, but this will work:

h <- hist( d, breaks="FD", plot=F )
zz <- spline( h$mids, h$counts )
qplot(x, y, data = data.frame(x = zz$x, y = zz$y), geom = "line")
Chase
  • 67,710
  • 18
  • 144
  • 161
  • This is a good answer, I should have though about this. The only problem is this does not necessarily produce a smooth line the way `spline` produces. – Sam May 24 '11 at 10:23