Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set lines to different transparency

Tags:

r

ggplot2

I have a ggplot that plots 4 different series as lines. I would like to set each line to a different transparency. How do I do this? More specifically, I want two of the lines to be transparent and two lines to be opaque. I am aware of how to set all lines to the same transparency with alpha, but now how to set the transparency individually.

Here is example data and code:

mydata = data.frame(rep(1:4,4),runif(16),c(rep("A",4),rep("B",4),rep("C",4),rep("D",4)))
colnames(mydata) = c("month","price","series")
library(ggplot2)
ggplot(mydata,aes(month,price,color=series))+geom_line()
like image 561
user3709306 Avatar asked Feb 26 '15 01:02

user3709306


1 Answers

Direct alpha to an aesthetic variable and use scale_alpha_manual

ggplot(mydata,aes(month,price,color=series, alpha=series)) + 
geom_line() + 
scale_alpha_manual(values = c(0.1, 0.1, 1, 1))

The order of c(0.1, 0.1, 1, 1) will of course depend on which lines you want opaque.

like image 172
Hugh Avatar answered Oct 05 '22 14:10

Hugh