Colored tape in ggplot2 per group

I am trying to plot some line charts with a ribbon in ggplot2

. The following code gives me a black ribbon around the colored line, when in fact I want the ribbon to be 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 like the image below enter image description here

I was thinking about setting up fill = variable

like 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

newdf

is 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 to color the ribbon correctly?

+3


source to share





All Articles