How to fit a line in geom_point(), to estimate the overall trend for each plot.
Each line represents one patient with baseline measurement (red dot) and a second measurement (blue dot).
p <- ggplot(data = df, aes(x=days, y=temp_1))
geom_point(aes(x=days, y=temp_1), color="red", show.legend = TRUE)
geom_point(aes(x=day, y=temp_2), color="blue", show.legend = TRUE)
scale_color_manual(name="Measurements", breaks=c("temp_1", "temp_2"), values = c("red", "blue"))
xlim(0, 20) ylim(70, 100)
geom_segment(aes(x=days, y=temp_1, xend=day, yend=temp_2))
facet_grid(~ treatment)
theme(legend.position = "bottom")
CodePudding user response:
One option would be to first reshape you data to long format which allows to simplify your plotting code a bit and makes it easy to add a tend line via geom_smooth
.
Using some fake random example data:
set.seed(123)
n <- 100
df <- data.frame(
days = 0,
day = sample(1:20, n, replace = TRUE),
temp_1 = sample(70:100, n, replace = TRUE),
temp_2 = sample(70:100, n, replace = TRUE),
treatment = sample(c("yes", "no"), n, replace = TRUE)
)
library(ggplot2)
library(dplyr)
library(tidyr)
df <- df %>%
rename(day_1 = days, day_2 = day) %>%
mutate(id = seq(nrow(.))) %>%
pivot_longer(-c(id, treatment), names_to = c(".value", "time"), names_sep = "_")
ggplot(data = df, aes(x = day, y = temp))
geom_point(aes(color = time), show.legend = TRUE)
geom_line(aes(group = id))
geom_smooth(method = "lm", se = FALSE)
scale_color_manual(name = "Measurements", values = c("red", "blue"))
xlim(0, 20)
ylim(70, 100)
facet_grid(~treatment)
theme(legend.position = "bottom")
#> `geom_smooth()` using formula 'y ~ x'