Adding a line to ggplot

I am trying to add a line to the point graph, and I cannot figure it out. My y values ​​are numbers from 0 to Inf, while my x values ​​refer to an ordered coefficient.

Here is a build code that only displays dots:

g = ggplot() + layer(data = ratesdf, mapping = aes(x = age, y = rates), geom = "point", stat="identity") + layer(data = ratesdf, mapping = aes(x = age, y = rates), geom = "smooth", stat = "smooth", method = loess) print(g) 

Here is the data frame:

  rates age [0,5) 0.00000000 [0,5) [5,10) 0.00000000 [5,10) [10,15) 0.00000000 [10,15) [15,20) 0.02017059 [15,20) [20,25) 0.32707402 [20,25) [25,30) 0.54013169 [25,30) [30,35) 0.71698958 [30,35) [35,40) 0.81120944 [35,40) [40,45) 0.87283637 [40,45) [45,50) 0.91411649 [45,50) [50,55) 0.91273334 [50,55) [55,60) 0.95627322 [55,60) [60,65) 0.92879819 [60,65) [65,70) 0.98088779 [65,70) [70,75) 0.90406674 [70,75) [75,80) 1.00000000 [75,80) [80,85) 1.00000000 [80,85) [85,Inf] 1.00000000 [85,Inf] 

Thanks to everyone in advance!

(Hadley, I promise to buy your book as soon as I receive my birthday gift card :))

+4
source share
2 answers

You need to manually specify group = 1, because by default ggplot2 groups are combined with all categorical variables on the chart.

+3
source

I'm not sure I missed what you are trying to do, but basically wants a step function. For instance:

 rates = c(0.00000000 ,0.00000000 ,0.00000000 ,0.02017059 ,0.32707402, 0.54013169 ,0.71698958 ,0.81120944 ,0.87283637 ,0.91411649 ,0.91273334 ,0.95627322 ,0.92879819 ,0.98088779 ,0.90406674 ,1.00000000 ,1.00000000, 1.00000000 ) age = seq(0, 85, 5) #ReJig the variables r2 = sort(rep(rates,2));r2 = r2[1:(length(r2)-1)] a = sort(rep(age,2));a = a[2:(length(a))] library(ggplot2) ggplot() + geom_line(aes(x=a, y=r2)) 

NTN

0
source

All Articles