简体   繁体   中英

Remove color option from second element in ggplot2

I have this plot where I first fit a melted table with a geom_line using the color:variable to colour the diffrent lines.

When I then try to fit a second element, namely a geom_ribbon . I then get the error message:

Error in eval(expr, envir, enclos) : object 'variable' not found

Why is that? I'm not using it in the second element. A sub-optimal workaround is to overwrite with color=NA in the ribbon geom but this produces a black line around my ribbon which I don't want..

ggplot(data, aes(x=as.Date(date), y=value, group=variable, color=variable)) + 
geom_line() +
geom_ribbon(data=fitted, aes(x=as.Date(date), y=forecast, ymin=lower, ymax=upper, fill="black", group=1), alpha=.2,  fill="red")

As you have set color=variable in aes() of ggplot() call then geom_ribbon() also tries to use variable for the color. Add inherit.aes=FALSE inside geom_ribbon() to ensure that previously set aesthetics are not used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM