我是 R 新手,我正在練習嘗試創建一些基本圖表。在下面的代碼中,我從 2 個變數中得到一條平均線。如何將±標準偏差添加到平均線?任何幫助將非常感激。
dat <- structure(list(distance = c(0, 0.2174741, 0.4349482, 0.6520882, 0.8695623, 1.0870364, 1.3045105, 1.5216505, 1.7391246, 1.9565987, 2.1740728, 2.3912128), grey1 = c(-300.364, -296.963, -292.887, -290.31, -285.777, -279.921, -274.418, -272.005, -273.666, -270.381, -270.273, -270.705), distance1 = c(0, 0.2114969, 0.4229937, 0.6341657, 0.8456625, 1.0571594, 1.2686562, 1.4798282, 1.6913251, 1.9028219, 2.1143188, 2.3254907), grey2 = c(-135.219, -132.601, -131.959, -133.514, -127.111, -116.404, -116.85, -115.464, -102.823, -101.497, -98.245, -98.474)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))
library(data.table)
newdat <- data.table::melt(as.data.table(dat),
measure = patterns("^distance","^grey"),
value.name = c("distance", "grey"))
newdat
ggplot(newdat, aes(distance, grey))
geom_line(aes(color = variable, group = variable))
ggplot(newdat, aes(distance, grey, color = variable))
geom_line()
geom_point()
newdist <- seq(0, min(max(dat$distance), max(dat$distance1)), by = 0.1)
newdat2 <- newdat[, setNames(approx(distance, grey, xout = newdist), c("distance", "grey")), by = variable
][, .(variable = "Avg", grey = mean(grey)), by = distance]
newdat2 <- rbindlist(list(newdat, newdat2), use.names = TRUE)
ggplot(newdat2, aes(distance, grey, color = variable))
geom_line()
geom_point()
uj5u.com熱心網友回復:
對于兩個不同的組,您的 x 值略有不同,因此標準偏差并未真正定義為 的每個唯一值distance。一種折衷方案是使用黃土回歸,其中包括圍繞兩組條件均值的置信區間。這很簡單,使用geom_smooth:
ggplot(newdat, aes(distance, grey))
geom_line(aes(color = variable))
geom_smooth(aes(color = "Avg"), method = loess, size = 0.7, alpha = 0.15)
geom_point()
theme_light(base_size = 16)

如果您對使用均值和 sds 的逐行值感到滿意,您可以這樣做:
library(data.table)
library(ggplot2)
newdat <- data.table::melt(as.data.table(dat),
measure = patterns("^distance","^grey"),
value.name = c("distance", "grey"))
dat$mean <- (dat$grey1 dat$grey2) / 2
dat$d <- (dat$distance1 dat$distance) / 2
dat$sd <- unlist(Map(function(a, b) sd(c(a, b)), dat$grey1, dat$grey2))
ggplot(newdat)
geom_line(aes(distance, grey, color = variable))
geom_ribbon(data = dat, aes(d, ymin = mean - sd, ymax = mean sd),
alpha = 0.15)
geom_line(data = dat, aes(d, mean, color = "Avg"))
geom_point(data = dat, aes(d, mean))
geom_point(aes(distance, grey))
theme_light(base_size = 16)

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/487266.html
