如何使用 GGPlot 绘制两条回归虚线
How to plot two dashed regression lines using GGPlot
我目前正在尝试使用 ggplot 函数形成两条虚线。该图显示了属于两个不同因素组的两条回归线。我已经能够使其中一条线变成虚线,但我无法让另一条线变成虚线。任何帮助将不胜感激。
coli_means %>%
ggplot(aes(time, mean_heartrate, group = treatment)) +
geom_point( aes(group = treatment, color = treatment)) +
geom_smooth(aes(method = "loess", linetype = treatment, se = FALSE,
group = treatment, color = treatment, show.legend = TRUE))
我觉得我缺少一个简单的输入。谢谢
你需要做的是使用 scale_linetype_manual()
然后告诉它两个治疗组都需要一条虚线。
让我们从一个可重现的例子开始:
# reproducible example:
set.seed(0)
time <- rep(1:100,2)
treatment <- c(rep("A",100), rep("B",100))
mean_heartrate <- c(rnorm(100,60,2), rnorm(100,80,2))
coli_means <- data.frame(time, treatment, mean_heartrate)
# ggplot
coli_means %>%
ggplot(aes(x = time, y = mean_heartrate)) +
geom_point(aes(color = treatment)) +
geom_smooth(aes(linetype = treatment, color = treatment))+
scale_linetype_manual(values = c('dashed','dashed'))
我目前正在尝试使用 ggplot 函数形成两条虚线。该图显示了属于两个不同因素组的两条回归线。我已经能够使其中一条线变成虚线,但我无法让另一条线变成虚线。任何帮助将不胜感激。
coli_means %>%
ggplot(aes(time, mean_heartrate, group = treatment)) +
geom_point( aes(group = treatment, color = treatment)) +
geom_smooth(aes(method = "loess", linetype = treatment, se = FALSE,
group = treatment, color = treatment, show.legend = TRUE))
我觉得我缺少一个简单的输入。谢谢
你需要做的是使用 scale_linetype_manual()
然后告诉它两个治疗组都需要一条虚线。
让我们从一个可重现的例子开始:
# reproducible example:
set.seed(0)
time <- rep(1:100,2)
treatment <- c(rep("A",100), rep("B",100))
mean_heartrate <- c(rnorm(100,60,2), rnorm(100,80,2))
coli_means <- data.frame(time, treatment, mean_heartrate)
# ggplot
coli_means %>%
ggplot(aes(x = time, y = mean_heartrate)) +
geom_point(aes(color = treatment)) +
geom_smooth(aes(linetype = treatment, color = treatment))+
scale_linetype_manual(values = c('dashed','dashed'))