在ts.plot中添加一条黄土线
Adding a loess line to a ts.plot
假设以下代码:
library(ggplot2)
library(RCurl)
x <- getURL("https://gist.githubusercontent.com/aronlindberg/dfa0115f1d80b84ebd48b3ed52f9c5ac/raw/3abf0f280a948d6273a61a75415796cc103f20e7/growth_data.csv")
data <- read.csv(text = x)
data <- data[,2:20]
data[cbind(FALSE, t(apply(data[,-1], 1, function(z) duplicated(z)
& z >= max(z))))] <- NA
ts.plot(t(data)+2, gpars = list(col = ggplot2::alpha("black", 0.5),
ylim = c(0.5, 500), xlim = c(2, 20), xlab = "Years",
ylab = "Cumulative Numbers"), log = 'y')
如何向该图添加红色 loess
线?
关于修改后的问题,我们进一步修改了那里显示的代码,直接在 read.csv
中读取 URL 并使用 R 的基数中的 adjustcolor
然后添加红色黄土线如图。没有使用包。
u <- "https://gist.githubusercontent.com/aronlindberg/dfa0115f1d80b84ebd48b3ed52f9c5ac/raw/3abf0f280a948d6273a61a75415796cc103f20e7/growth_data.csv"
data <- read.csv(u)[, 2:20]
data[cbind(FALSE, t(apply(data[,-1], 1, function(z) duplicated(z)
& z >= max(z))))] <- NA
data2 <- data + 2
ts.plot(t(data2), gpars = list(col = adjustcolor("black", 0.5),
ylim = c(0.5, 500), xlim = c(2, 20), xlab = "Years",
ylab = "Cumulative Numbers"), log = 'y')
s <- na.omit(stack(data2))
s$years <- as.integer(s$ind)
lo <- loess(values ~ years, s)
lines(fitted(lo) ~ years, s, col = "red", lwd = 2)
给予:
旧
你将无法使用这么少的点来使用黄土,但这里有一个使用内置 mdeaths
和 ldeaths
的示例。请注意,作为基础包的 grDevices 具有 adjustcolor,因此您无需使用 ggplot2 来获取 alpha。
lo.m <- fitted(loess(mdeaths ~ time(mdeaths)))
lo.l <- fitted(loess(ldeaths ~ time(ldeaths)))
ts.plot(mdeaths, ldeaths, lo.m, lo.l,
gpars = list(col = adjustcolor(1:2, 0.5), lty = c(1, 1, 2, 2)))
假设以下代码:
library(ggplot2)
library(RCurl)
x <- getURL("https://gist.githubusercontent.com/aronlindberg/dfa0115f1d80b84ebd48b3ed52f9c5ac/raw/3abf0f280a948d6273a61a75415796cc103f20e7/growth_data.csv")
data <- read.csv(text = x)
data <- data[,2:20]
data[cbind(FALSE, t(apply(data[,-1], 1, function(z) duplicated(z)
& z >= max(z))))] <- NA
ts.plot(t(data)+2, gpars = list(col = ggplot2::alpha("black", 0.5),
ylim = c(0.5, 500), xlim = c(2, 20), xlab = "Years",
ylab = "Cumulative Numbers"), log = 'y')
如何向该图添加红色 loess
线?
关于修改后的问题,我们进一步修改了那里显示的代码,直接在 read.csv
中读取 URL 并使用 R 的基数中的 adjustcolor
然后添加红色黄土线如图。没有使用包。
u <- "https://gist.githubusercontent.com/aronlindberg/dfa0115f1d80b84ebd48b3ed52f9c5ac/raw/3abf0f280a948d6273a61a75415796cc103f20e7/growth_data.csv"
data <- read.csv(u)[, 2:20]
data[cbind(FALSE, t(apply(data[,-1], 1, function(z) duplicated(z)
& z >= max(z))))] <- NA
data2 <- data + 2
ts.plot(t(data2), gpars = list(col = adjustcolor("black", 0.5),
ylim = c(0.5, 500), xlim = c(2, 20), xlab = "Years",
ylab = "Cumulative Numbers"), log = 'y')
s <- na.omit(stack(data2))
s$years <- as.integer(s$ind)
lo <- loess(values ~ years, s)
lines(fitted(lo) ~ years, s, col = "red", lwd = 2)
给予:
旧
你将无法使用这么少的点来使用黄土,但这里有一个使用内置 mdeaths
和 ldeaths
的示例。请注意,作为基础包的 grDevices 具有 adjustcolor,因此您无需使用 ggplot2 来获取 alpha。
lo.m <- fitted(loess(mdeaths ~ time(mdeaths)))
lo.l <- fitted(loess(ldeaths ~ time(ldeaths)))
ts.plot(mdeaths, ldeaths, lo.m, lo.l,
gpars = list(col = adjustcolor(1:2, 0.5), lty = c(1, 1, 2, 2)))