How do I do spline-smoothing on a multi-line plot in the code segment below? The attached figure shows the two plots generated by this code.
Thanks!
library(tidyverse)
library(ggplot2)
# group by cyl
by_cyl <- mtcars %>% group_by(cyl) %>% summarize(mpg = mean(mpg))
# group by cyl & gear
by_cyl_gear <- mtcars %>% group_by(cyl, gear) %>% summarize(mpg = mean(mpg))
# spline smoothing for a single line plot
spline_d <- as.data.frame(spline(by_cyl$cyl, by_cyl$mpg))
ggplot(data = by_cyl, aes(x = cyl, y = mpg)) +
geom_line(data = spline_d, aes(x = x, y = y)) +
geom_point() +
ggtitle("Unpartitioned (smooth)")
# multi-line plot - how to do spline-smoothing here?
by_cyl_gear$gear <- as.character(by_cyl_gear$gear)
ggplot(data = by_cyl_gear, aes(x = cyl, y = mpg)) +
geom_line(aes(color = gear)) +
geom_point(aes(color = gear)) +
ggtitle("Partitioned by gear (non-smooth)") +
theme(legend.position = c(0.9, 0.6))

