如何使用GGPlot

时间:2019-11-12 05:19:52

标签: r ggplot2 rstudio regression

我目前正在尝试使用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))

我觉得我缺少一种简单的输入。谢谢。

1 个答案:

答案 0 :(得分:0)

您需要做的是使用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'))

[Dashed lines for both treatments