I'm trying to plot two groups by their data and trendline over time. Ideally, I'd like the legend to show the groups as title with the rate and trendline below. Right now the legend is only showing for the group and not the trendline. This is my dummy code:
week <- c(1,2,3,4,5,1,2,3,4,5)
hits <- c(262,698,239,195,122,160,746,283,557,239)
rate <- c(116, 134, 154, 185, 167, 216, 178, 242, 267, 234)
lower <- c(94, 111, 123, 145, 138, 196, 147, 204, 216, 176)
upper <- c(126, 167, 176, 225, 191, 245, 199, 287, 292, 267)
group <- c("A","A","A","A","A","B","B","B","B","B")
df <- data.frame(week = week, hits=hits, rate=rate, lower=lower, upper=upper,group=group)
ggplot(df, aes(x = week))
geom_line(aes(y = rate, color = group))
geom_ribbon(aes(ymin = lower, ymax = upper, fill = group),
alpha = 0.2)
geom_smooth(aes(y = rate, color = group), method = 'lm',
size = 0.5, se = FALSE, linetype = "dashed",
show.legend = TRUE)
scale_color_manual(values = c("red", "black"))
scale_fill_manual(values = c("red", "black"))
Edit So ideally the legend would look like this:
CodePudding user response: