使用 ggplot2 模拟 Google Sheets 平滑折线图
Emulating Google Sheets smoothed line chart with ggplot2
我正在尝试使用 ggplot2 模拟 Google Sheets 平滑折线图。它通过所有数据点绘制一条平滑线。
绘制在 Google 表格中:
上下两行是我硬编码的置信区间。
数据:
library(tidyverse)
data <- tibble(
date = seq.Date(as.Date("2018-12-01"), as.Date("2018-12-20"), by = "days"),
var = c(0.329,0.348,0.349,0.355,0.382,0.363,0.340,0.359,0.336,0.358,0.398,0.389,0.389,0.390,0.383,0.343,0.352,0.415,0.397,0.430),
lower = c(0.311,0.330,0.330,0.336,0.364,0.345,0.321,0.342,0.319,0.341,0.384,0.375,0.375,0.374,0.369,0.329,0.337,0.400,0.382,0.417),
upper = c(0.347,0.366,0.368,0.374,0.400,0.381,0.358,0.377,0.354,0.375,0.413,0.404,0.403,0.405,0.397,0.357,0.368,0.430,0.412,0.444))
我几乎得到了与黄土相同的曲线。但它在某些时候给了我一个警告和一个 奇怪的锐利区域 。
代码:
ggplot(data, aes(x = seq_along(date), y = var)) +
geom_point(size = 2, color = "blue", alpha = 0.2) +
# geom_smooth(method = "lm", formula = y ~ splines::bs(x, 20), se = F) +
# stat_smooth(method = "gam", formula = y ~ s(x, k = 19), se = F) +
stat_smooth(method = "loess", span = 0.2, se = F) +
theme_classic() + theme(axis.line = element_line(size = 0.5, colour = "grey80"))
警告:
Warning messages:
1: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
Chernobyl! trL>n 20
2: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
Chernobyl! trL>n 20
3: In sqrt(sum.squares/one.delta) : NaNs produced
R图:
只是为了可视化,你可以试试 awesome ggalt package by @hrbrmstr。
ggalt::geom_xspline
绘制 x 样条曲线。
library(ggalt)
library(tidyverse)
data <- tibble(
date = seq.Date(as.Date("2018-12-01"), as.Date("2018-12-20"), by = "days"),
var = c(0.329,0.348,0.349,0.355,0.382,0.363,0.340,0.359,0.336,0.358,0.398,0.389,0.389,0.390,0.383,0.343,0.352,0.415,0.397,0.430),
lower = c(0.311,0.330,0.330,0.336,0.364,0.345,0.321,0.342,0.319,0.341,0.384,0.375,0.375,0.374,0.369,0.329,0.337,0.400,0.382,0.417),
upper = c(0.347,0.366,0.368,0.374,0.400,0.381,0.358,0.377,0.354,0.375,0.413,0.404,0.403,0.405,0.397,0.357,0.368,0.430,0.412,0.444))
ggplot(data, aes(seq_along(date), var)) +
geom_point(size = 2, color = "blue", alpha = 0.2) +
geom_xspline() +
theme_classic() +
theme(axis.line = element_line(size = 0.5, colour = "grey80"))
我正在尝试使用 ggplot2 模拟 Google Sheets 平滑折线图。它通过所有数据点绘制一条平滑线。
绘制在 Google 表格中:
数据:
library(tidyverse)
data <- tibble(
date = seq.Date(as.Date("2018-12-01"), as.Date("2018-12-20"), by = "days"),
var = c(0.329,0.348,0.349,0.355,0.382,0.363,0.340,0.359,0.336,0.358,0.398,0.389,0.389,0.390,0.383,0.343,0.352,0.415,0.397,0.430),
lower = c(0.311,0.330,0.330,0.336,0.364,0.345,0.321,0.342,0.319,0.341,0.384,0.375,0.375,0.374,0.369,0.329,0.337,0.400,0.382,0.417),
upper = c(0.347,0.366,0.368,0.374,0.400,0.381,0.358,0.377,0.354,0.375,0.413,0.404,0.403,0.405,0.397,0.357,0.368,0.430,0.412,0.444))
我几乎得到了与黄土相同的曲线。但它在某些时候给了我一个警告和一个 奇怪的锐利区域 。
代码:
ggplot(data, aes(x = seq_along(date), y = var)) +
geom_point(size = 2, color = "blue", alpha = 0.2) +
# geom_smooth(method = "lm", formula = y ~ splines::bs(x, 20), se = F) +
# stat_smooth(method = "gam", formula = y ~ s(x, k = 19), se = F) +
stat_smooth(method = "loess", span = 0.2, se = F) +
theme_classic() + theme(axis.line = element_line(size = 0.5, colour = "grey80"))
警告:
Warning messages:
1: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
Chernobyl! trL>n 20
2: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
Chernobyl! trL>n 20
3: In sqrt(sum.squares/one.delta) : NaNs produced
R图:
只是为了可视化,你可以试试 awesome ggalt package by @hrbrmstr。
ggalt::geom_xspline
绘制 x 样条曲线。
library(ggalt)
library(tidyverse)
data <- tibble(
date = seq.Date(as.Date("2018-12-01"), as.Date("2018-12-20"), by = "days"),
var = c(0.329,0.348,0.349,0.355,0.382,0.363,0.340,0.359,0.336,0.358,0.398,0.389,0.389,0.390,0.383,0.343,0.352,0.415,0.397,0.430),
lower = c(0.311,0.330,0.330,0.336,0.364,0.345,0.321,0.342,0.319,0.341,0.384,0.375,0.375,0.374,0.369,0.329,0.337,0.400,0.382,0.417),
upper = c(0.347,0.366,0.368,0.374,0.400,0.381,0.358,0.377,0.354,0.375,0.413,0.404,0.403,0.405,0.397,0.357,0.368,0.430,0.412,0.444))
ggplot(data, aes(seq_along(date), var)) +
geom_point(size = 2, color = "blue", alpha = 0.2) +
geom_xspline() +
theme_classic() +
theme(axis.line = element_line(size = 0.5, colour = "grey80"))