6

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()
zx8754
  • 52,746
  • 12
  • 114
  • 209
user3709306
  • 93
  • 1
  • 6
  • 1
    How about adding some sample input data to make this problem [reproducble](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and tell us which of the lines you want to adjust the opacity for. You can use `alpha=` to adjust the transparency of certain elements. – MrFlick Feb 26 '15 at 01:23

1 Answers1

12

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.

Hugh
  • 15,521
  • 12
  • 57
  • 100