在ggplot中创建具有给定的最大和最小x值的平滑图

问题描述 投票:0回答:2

我想在ggplot中创建一个图表,显示潮汐系统在几天内的水位。我有每个高潮和低潮的水位,但是当我在ggplot中绘制它们时,线条看起来前卫并且我希望它看起来弯曲。我试图使用geom_smooth,但是那行不通。有人知道吗?

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9nMFhUeC5wbmcifQ==” alt =“看起来像”>“ >>

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9xdnREVy5wbmcifQ==” alt =“在此处输入图像描述”>“ >>

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

我想在ggplot中创建一个图表,显示潮汐系统在几天内的水位。我有每个高潮和低潮的水位,但是当我在ggplot中绘制它们时,...

ggplot2 curve
2个回答
0
投票

您可以使用spline()内插原始数据点:

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')

0
投票

使线条平滑的一种可能性是在每对点之间建立一个sigmoid curve。由于您没有以易于复制的格式提供数据,因此让我们按如下方式构成一些模拟数据:

© www.soinside.com 2019 - 2024. All rights reserved.