I am trying to plot multiple line charts with a ribbon in ggplot2
. The following code gives me a black ribbon around the colored line, whereas I actually I want the ribbon to have the same color as the line.
ggplot(newdf,aes(x=date,y=value,group=variable,color=variable)) +
geom_line() +
facet_grid(variable ~.) +
scale_x_date() +
geom_ribbon(aes(ymin=0, ymax=value)) +
theme(legend.position='none') +
geom_line(aes(y=0),linetype="dotted")
This gives me a black ribbon as in the image below
I was thinking of setting fill = variable
as per below:
ggplot(newdf,aes(x=date,y=value,group=variable,color=variable)) +
geom_line() +
facet_grid(variable ~.) +
scale_x_date() +
geom_ribbon(aes(ymin=0, ymax=value), fill = variable) +
theme(legend.position='none') +
geom_line(aes(y=0),linetype="dotted")
However, this gives me the following error:
Error in do.call("layer", list(mapping = mapping, data = data, stat = stat, : object 'variable' not found
The newdf
is a data.frame
and looks like this
>head(newdf)
date variable value
1 2014-01-02 CHINA -0.3163197
2 2014-01-03 CHINA -0.2820751
3 2014-01-06 CHINA -0.3256087
4 2014-01-07 CHINA -0.3741991
5 2014-01-08 CHINA -0.4064004
6 2014-01-09 CHINA -0.3630387
How do I color the ribbon correctly?