I was originally going to use a ribbon for this function, but ribbon was giving me an "Aesthetics can not vary within a ribbon" error. I tried a geom_smooth, geom_ribbon, and geom_line, and none of them worked for what I needed. The picture posted below is what I need, except that I would like those square joints between where the line changes colors to be either vertical and flush or round.
I looked at these posts already:
Shade Fill or Cover Area Density Under Curve
R ggplot Issue When Using Group Interaction
I've also tried using groups in a couple different ways. I should mention that I'm extremely new to ggplot2 and R in general, so any help or possible avenues would be appreciated. I've been using these resources, which you might find useful for this problem or for your own:
GGPlot2: Elegant Graphics For Data Analysis
Code:
library(ggplot2)
pdf("ggplot_learning.pdf")
data <- data.frame(
xvals = c(0:5),
yvals = c(4, 5, 4.5, 5.5, 5, 6),
lower = c(3.9, 4.9, 4.4, 5.4, 4.9, 5.9),
upper = c(4.1, 5.1, 4.6, 5.6, 5.1, 6.1)
)
plot <- ggplot(data, aes(xvals, yvals, ymax = 8, xmax = 8)) +
geom_line(aes(color = yvals, alpha = .3), lwd = 10) +
geom_line(aes(color = yvals)) +
theme(legend.position = "none")
print(plot)
What I have so far: