你可以画出两个geom_smooth()
--一个代表4条‘好’线,一个代表1条‘最差’线,例如
library(tidyverse)
set.seed(142222)
num_lots <- 5
# Create an empty data frame to store the simulated data
data <- data.frame(Lot = rep(1:num_lots, each = 9),
Time = rep(3 * 0:8, times = num_lots),
Measurement = numeric(num_lots * 9))
# Simulate purity data for each lot and time point
for (lot in 1:num_lots) {
# Generate random intercept and slope for each lot
intercept <- rnorm(1, mean = 95, sd = 2)
slope <- runif(1, min = -.7, max = 0)
for (month in 0:8) {
# Simulate purity data with noise
data[data$Lot == lot & data$Time == month * 3, "Purity"] <- intercept + slope * month * 3 + rnorm(1, mean = 0, sd = .35)
}
}
ggplot(data = data,
aes(x = Time, y = Purity,
color = as.factor(Lot),
shape = as.factor(Lot))) +
geom_point(key_glyph = "point") +
geom_smooth(data = data %>% filter(Lot == 2),
method = "lm", se=TRUE, type = 1,
key_glyph = "point") +
geom_smooth(data = data %>% filter(Lot != 2),
method = "lm", se=FALSE, type = 1,
key_glyph = "point") +
labs(
title = "Test",
x = "month",
y = "Purity",
color = "Lot", # Set legend title for color
shape = "Lot" # Set legend title for shape
) +
theme_minimal() +
scale_x_continuous(breaks = c(0, 3, 6, 9, 12, 15, 18, 21, 24))
#> Warning in geom_smooth(data = data %>% filter(Lot == 2), method = "lm", :
#> Ignoring unknown parameters: `type`
#> Warning in geom_smooth(data = data %>% filter(Lot != 2), method = "lm", :
#> Ignoring unknown parameters: `type`
#> `geom_smooth()` using formula = 'y ~ x'
#> `geom_smooth()` using formula = 'y ~ x'
创建于2023-10-12年第reprex v2.0.2页
编辑
与其使用key_glyph = "point"
,不如根据@stefan的 comments 使用show_legend = FALSE
:
library(tidyverse)
set.seed(142222)
num_lots <- 5
# Create an empty data frame to store the simulated data
data <- data.frame(Lot = rep(1:num_lots, each = 9),
Time = rep(3 * 0:8, times = num_lots),
Measurement = numeric(num_lots * 9))
# Simulate purity data for each lot and time point
for (lot in 1:num_lots) {
# Generate random intercept and slope for each lot
intercept <- rnorm(1, mean = 95, sd = 2)
slope <- runif(1, min = -.7, max = 0)
for (month in 0:8) {
# Simulate purity data with noise
data[data$Lot == lot & data$Time == month * 3, "Purity"] <- intercept + slope * month * 3 + rnorm(1, mean = 0, sd = .35)
}
}
ggplot(data = data,
aes(x = Time, y = Purity,
color = as.factor(Lot),
shape = as.factor(Lot))) +
geom_point() +
geom_smooth(data = data %>% filter(Lot == 2),
method = "lm", formula = "y ~ x",
se=TRUE,
show.legend = FALSE) +
geom_smooth(data = data %>% filter(Lot != 2),
method = "lm", formula = "y ~ x",
se=FALSE,
show.legend = FALSE) +
labs(
title = "Test",
x = "month",
y = "Purity",
color = "Lot", # Set legend title for color
shape = "Lot" # Set legend title for shape
) +
theme_minimal() +
scale_x_continuous(breaks = c(0, 3, 6, 9, 12, 15, 18, 21, 24))
创建于2023-10-12年第reprex v2.0.2页
编辑 2
你可以用不同的方法自动 Select 最差的线;最简单的方法是在时间=0时 Select 纯度最低的批次,但这可能会根据你的数据而变化(即,你可能想要在时间=24?时 Select 纯度最低的批次?).你只能画出上界,但你必须自己计算坐标.
library(tidyverse)
set.seed(142222)
num_lots <- 5
# Create an empty data frame to store the simulated data
data <- data.frame(Lot = rep(1:num_lots, each = 9),
Time = rep(3 * 0:8, times = num_lots),
Measurement = numeric(num_lots * 9))
# Simulate purity data for each lot and time point
for (lot in 1:num_lots) {
# Generate random intercept and slope for each lot
intercept <- rnorm(1, mean = 95, sd = 2)
slope <- runif(1, min = -.7, max = 0)
for (month in 0:8) {
# Simulate purity data with noise
data[data$Lot == lot & data$Time == month * 3, "Purity"] <- intercept + slope * month * 3 + rnorm(1, mean = 0, sd = .35)
}
}
# Select the worst regression line
worst <- data %>% filter(Purity == min(Purity)) %>% pull(Lot)
# Build the 5 linear models
output <- data %>%
nest_by(Lot) %>%
reframe(model = list(lm(data = data, formula = Purity ~ Time)))
# Apply the models and extract the coordinates
preds <- predict(output$model[[worst]], newdata = data, se.fit = TRUE)
input_df <- data.frame(fit = preds$fit, se.fit = preds$se.fit) %>%
bind_cols(data)
# Plot data and input_df
ggplot(data = data,
aes(x = Time, y = Purity,
color = as.factor(Lot),
shape = as.factor(Lot))) +
geom_point() +
geom_smooth(method = "lm", formula = "y ~ x",
se=FALSE,
show.legend = FALSE) +
geom_ribbon(data = input_df, aes(x = Time, y = Purity,
ymin = fit, ymax = fit + se.fit * 2),
inherit.aes = FALSE, lty = 2, fill = "blue", alpha = 0.25) +
labs(
title = "Test",
x = "month",
y = "Purity",
color = "Lot", # Set legend title for color
shape = "Lot" # Set legend title for shape
) +
theme_minimal() +
scale_x_continuous(breaks = c(0, 3, 6, 9, 12, 15, 18, 21, 24))
创建于2023-10-12年第reprex v2.0.2页