Use ggplot2 to plot two lines with ribbons
I have a data frame with metric v and a categorical variable t. I can plot them as lines like this
set.seed(42)
v <- runif(20, min=0, max=100)
t <- sample( LETTERS[1:2], 20, replace=TRUE )
df <- data.frame(v,t)
qplot(1:length(v),v,data=df,geom="line",group=t,color=t)
I would like to add ribbons around each line. Possibly different widths (which I will set with the formula) and opacity. I tried to replace geom with "feed", but I got the following error:
qplot(1:length(v),v,data=df,geom="ribbon",group=t,color=t)
Error: geom_ribbon requires the following missing aesthetics: ymin, ymax
How can I plot both rows and their ribbons in one chart?
+3
ADJ
source
to share
1 answer
You can add a ribbon here. You can, of course, change the formulas for ymin
and ymax
to suit your needs:
ggplot(df, aes(x=1:length(v), y=v, group=t, colour=t)) +
geom_ribbon(aes(ymin=v-0.1*v, ymax=v+0.1*v, fill=t), alpha=0.2) +
geom_line()
+5
eipi10
source
to share