Add geom_abline to time series plot

huangapple go评论54阅读模式
英文:

Add geom_abline to time series plot

问题

I have some values through time, I want to plot them and manually add a trend line using geom_abline(). When I add the line nothing shows up. E.g.

library(ggplot2)

# example data
df1 <- structure(list(Date = structure(c(18603, 16965, 17294, 16111, 18274, 18820,
                                   17658, 17056, 17448, 18757, 16902, 16594,
                                   16447, 16027, 18330, 16685, 17210, 18694,
                                   15778, 16755), class = "Date"), 
               Value = c(5.37, 1.95, 3.72, 7.55, 5.05, 3.1, 1.43, 3.45, 
                         7.1, 1.44, 4, 4.3, 6.9, 8.01, 1.61, 3.3, 1.4, 8.46,
                         8.78, 8.3)), class = "data.frame",
          row.names = c(NA, -20L))



# plot just the line - works fine
intc <- 3.57
slope <- 0.1

ggplot() +
  scale_y_continuous(limits = c(0, 10)) +
  geom_abline(intercept = intc, slope = slope)

Add geom_abline to time series plot

adding the point data makes the line disappear

ggplot() +
scale_y_continuous(limits = c(0, 10)) +
geom_abline(intercept = intc, slope = slope) +
geom_point(data = df1, aes(x = Date, y = Value))

Add geom_abline to time series plot

Any adice on how I can get the trend line and the points on the same plot would be much appreciated.

英文:

I have some values through time, I want to plot them and manually add a trend line using geom_abline(). When I add the line nothing shows up. E.g.

library(ggplot2)

# example data
df1 &lt;- structure(list(Date = structure(c(18603, 16965, 17294, 16111, 18274, 18820,
                                   17658, 17056, 17448, 18757, 16902, 16594,
                                   16447, 16027, 18330, 16685, 17210, 18694,
                                   15778, 16755), class = &quot;Date&quot;), 
               Value = c(5.37, 1.95, 3.72, 7.55, 5.05, 3.1, 1.43, 3.45, 
                         7.1, 1.44, 4, 4.3, 6.9, 8.01, 1.61, 3.3, 1.4, 8.46,
                         8.78, 8.3)), class = &quot;data.frame&quot;,
          row.names = c(NA, -20L))



# plot just the line - works fine
intc &lt;- 3.57
slope &lt;- 0.1

ggplot() +
  scale_y_continuous(limits = c(0, 10)) +
  geom_abline(intercept = intc, slope = slope)

Add geom_abline to time series plot

# adding the point data makes the line disappear
 ggplot() +
  scale_y_continuous(limits = c(0, 10)) +
  geom_abline(intercept = intc, slope = slope) +
  geom_point(data = df1, aes(x = Date, y = Value))

Add geom_abline to time series plot

Any adice on how I can get the trend line and the points on the same plot would be much appreciated.

答案1

得分: 2

如 @542goweast 提到的,这有点复杂,因为你的 x 轴具有 Date 类。日期表示为整数,其中 0 == as.numeric(as.Date("1970-01-01"))。因此,轴不从 0 开始,而是从 as.numeric(min(df1$Date)) -> 15778 开始。

另外,由于 x 轴单位是按天计算的,你需要相应地调整斜率和截距。

以下是一个选项。

num_of_days = as.numeric(difftime(max(df1$Date), min(df1$Date), unit = "days"))
slope = .1 / num_of_days
intc = 3.57 - slope*min(as.numeric(df1$Date))

ggplot(data = df1, aes(x = Date, y = Value)) +
  geom_point() +
  scale_y_continuous(limits = c(0, 10)) +
  geom_abline(aes(intercept = intc, slope = slope))

Add geom_abline to time series plot


请注意,这是你提供的代码的翻译部分。

<details>
<summary>英文:</summary>

As @542goweast mentions, it&#39;s a bit convoluted since your x-axis has the class of Date. And dates are represented as integers where `0 == as.numeric(as.Date(&quot;1970-01-01&quot;))`. So the axis does not start at 0 and instead starts at `as.numeric(min(df1$Date)) -&gt; 15778`. 

Additionally since the x-axis units are by day you need to scale your slope and intercept accordingly.

Below is an option.

num_of_days = as.numeric(difftime(max(df1$Date), min(df1$Date)), unit = "days")
slope = .1 / num_of_days
intc = 3.57 - slope*min(as.numeric(df1$Date))

ggplot(data = df1, aes(x = Date, y = Value)) +
geom_point() +
scale_y_continuous(limits = c(0, 10)) +
geom_abline(aes(intercept = intc, slope = slope))


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/O8iSf.png

</details>



# 答案2
**得分**: 1

这与你的 `slope` 对象有关。一旦你添加了数据点,你的 x 轴就是在 2000 年代的刻度上。在这个刻度上,这样一条线会看起来大致是垂直的。在第一个刻度线 - 2014 年 - 我们的 y 值大约是 205。尝试 `slope=0.0001`。

<details>
<summary>英文:</summary>

It has to do with your `slope` object. Once you add the data points, your x-axis is on the scale of 2000&#39;s. At that scale, such a line would appear approximately vertical. On the first hash mark - 2014 - we would have a y value of about 205. Try `slope=0.0001`. 

</details>



huangapple
  • 本文由 发表于 2023年5月11日 07:57:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定