0

I have a question regarding plotting a line through some datapoints I have.

The data shows that the efficiency of my expt levels off at about 0.8-0.9. However, when I plot geom_smooth() it appears as if it makes a dip again. When I plot with geom_line() the transition from 1 point to the next is not as smooth as I'd like it to have.

the values i used are:

 x <-  c(0.20, 0.44, 0.72, 0.86, 0.88, 0.89)
 time <-  c(0, 5, 15, 40, 80, 120)
 dfs2 <- data.frame(x, time)

I made a dataframe out of these to vectors called dfs2. I have used either geom_line() or geom_smooth() (both are shown in the code below but only one is used)

plot <- ggplot(dfs2, aes(x = `time`, y = `x`)) + 
  geom_point() +
  geom_line() +
  geom_smooth(span = 2, se = F) +
  xlim(0, 120) + ylim(0, 1)

How do i get this data to show as a line that levels off as if you would plot with geom_line() but without the sharp corners from one point to the next

Thanks in advance!

  • 4
    Depends on the goal here. `geom_smooth` typically will need more points to create a better model. If you just want a pretty picture (and statistical accuracy is less important) you can use `ggforce::geom_bspline0()` or `ggalt::geom_xspline()`. – Axeman Jun 26 '18 at 08:53
  • Thanks, that worked for me – Daan Verhagen Jun 26 '18 at 09:15

1 Answers1

-1

Have you checked this question? Maybe spline() is what you're looking for.

spline_int = as.data.frame(spline(dfs2$time, dfs2$x))

ggplot(dfs2, aes(x = `time`, y = `x`)) + 
  geom_point() + 
  geom_line(data=spline_int, aes(x = x, y = y))
Sangbok Lee
  • 2,132
  • 3
  • 15
  • 33
  • This is more or less a copy of the answer in the question you linked. In that case it is preferred to tag the question as a duplicate. – Axeman Jun 26 '18 at 09:33