How to build a spline with ggplot2?

I have some data that I want to make a histogram. However, I want to present this histogram with a string. I tried to use freq_polyfor ggplot2. However, the line produced is rather uneven. I want to know if it is possible to use splineswith ggplot2so that the line created in freq_polyis 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 this using ggplot2.

+3
source share
1 answer

I assume that you are trying to use a function spline(). If not, do not take this answer into account.

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 ...

data.frame . , , :

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")
+4

All Articles