首页 > 解决方案 > 我可以将曲线趋势线添加到每列只有一个数据点的 ggplot 吗?

问题描述

我使用 ggplot2 为土壤水分释放曲线创建了折线图。但是,因为我在每个压力值(x 轴)上只有一个数据点,所以这些线直接从点到点连接。我想保留这些点,但有一条曲线可以显示点的趋势。这是土壤水分释放曲线的典型样式。

数据:

> dput(head(sub2018))
structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = 
c("2018", 
"2019"), class = "factor"), Pressure = structure(1:6, .Label = c("-1", 
"-0.5", "-0.25", "-0.2", "-0.1", "-0.05", "-0.02", "-0.01", "0"
), class = "factor"), meanVWC = c(0.291819594, 0.308328767666667, 
0.318496127666667, 0.323671866333333, 0.349356212666667, 
0.374201803666667
)), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = "Year", drop = TRUE, indices = list(
0:5), group_sizes = 6L, biggest_group_size = 6L, labels = structure(list(
Year = structure(1L, .Label = c("2018", "2019"), class = "factor")),             
row.names = c(NA, 
-1L), class = "data.frame", vars = "Year", drop = TRUE))

格图:

GGplot2018 <- ggplot(sub2018, aes(x=Pressure, y=meanVWC, group=1)) +
  geom_line() +
  geom_point() + labs(y= "Volumetric Water Content") 
GGplot2018

有谁知道我是否/如何添加这条曲线?

非常感谢您的帮助!

标签: rggplot2

解决方案


我相信这就是您正在寻找的:

GGplot2018 <- ggplot(sub2018, aes(x=Pressure, y=meanVWC, group=1)) +
  geom_line()+
  geom_point() + labs(y= "Volumetric Water Content")+
  geom_smooth(method = "lm",se = FALSE)
GGplot2018

推荐阅读