Create a smooth plot with given maximum and minumun x values in ggplot

218 views Asked by At

I want to create a plot in ggplot showing the water level of a tidal system over a time span of several days. I have the water levels for each High and Low Tide, but when I plot them in ggplot the line looks edgy and I would like it to look curvy. I tried to use geom_smooth but that will not work. Has someone any idea?

This is how it looks like

enter image description here

+---------------+-------------+ | Water.level_m | Time_h | +---------------+-------------+ | 3.9 | 0 | | 1.4 | 0.270833333 | | 3.8 | 0.516666667 | | 1.5 | 0.7875 | | 3.7 | 1.041666667 | | 1.7 | 1.308333333 | | 3.9 | 1.5625 | | 1.7 | 1.829166667 | | 3.6 | 2.091666667 | | 1.8 | 2.35 | | 3.8 | 2.608333333 | | 1.7 | 2.875 | | 3.6 | 3.141666667 | | 1.8 | 3.4 | | 3.9 | 3.654166667 | | 1.6 | 3.9375 | | 3.8 | 4.191666667 | | 1.6 | 4.454166667 | | 4.1 | 4.704166667 | | 1.3 | 4.9875 | | 4.2 | 5.245833333 | | 1.2 | 5.504166667 | | 4.4 | 5.75 | | 0.9 | 6.025 | | 4.5 | 6.275 | | 0.8 | 6.5375 | | 4.8 | 6.7875 | | 0.6 | 7.058333333 | | 4.8 | 7.308333333 | | 0.5 | 7.566666667 | | 5 | 7.816666667 | | 0.4 | 8.083333333 | | 5 | 8.3375 | | 0.2 | 8.6 | | 5.2 | 8.85 | | 0.2 | 9.108333333 | | 5.1 | 9.3625 | | 0.1 | 9.625 | | 5.2 | 9.879166667 | | 0.2 | 10.14166667 | | 5.1 | 10.39166667 | +---------------+-------------+

2

There are 2 answers

0
jdobres On BEST ANSWER

You could use spline() to interpolate your original data points:

df <- data.frame(
  x = 1:20,
  y = rep(c(-1, 1), 10) * 1:20
)

ggplot(df, aes(x, y)) +
  geom_line() +
  geom_line(data = as.data.frame(spline(df, n = 300, method = 'natural')), aes(x = x, y = y), color = 'red')

enter image description here

1
teunbrand On

One possibility of smoothing the line is to make a sigmoid curve between each pair of points. Since you haven't provided the data in a format that is easy to copy, let's make up some mock data as follows:

df <- data.frame(
  x = 1:20,
  y = rep(c(-1, 1), 10) * 1:20
)

ggplot(df, aes(x, y)) +
  geom_line()

enter image description here

You could make a sigmoid curve out of this as follows:

sigmoid <- cbind(head(df, -1), tail(df, -1))
names(sigmoid)[3:4] <- c("xend", "yend")

sigmoid <- apply(sigmoid, 1, function(dat) {
  x <- seq(dat[["x"]], dat[["xend"]], length.out = 10)
  A <- dat[["y"]]
  K <- dat[["yend"]]
  B <- 10
  C <- 1
  mid <- mean(c(dat[["x"]], dat[["xend"]]))
  y <- A + (K - A) / (C + exp(-B * (x - mid)))
  data.frame(x = x, y = y)
})

sigmoid <- do.call(rbind, sigmoid)

Feel free to vary the B and C parameters to fit your data. Have a look at the linked wikipedia page above for an explanation of the parameters.

Now the plot would look like the following combined with the input data:

ggplot(df, aes(x, y)) +
  geom_line() +
  geom_line(data = sigmoid, colour = "red")

enter image description here

In order to not misdirect people with your graph, I recommend to include the actual measurements as points on the graph.