ilFonta
ilFonta

Reputation: 301

setting trasparency in ggplot2 using aes_color_fill

I wrote this few lines in order to plot some boxplots, and everything works:

plotSerie <- ggplot(fileIn) +
geom_boxplot(aes(x=DOY_S1, y=S1_VV, alpha=0.5, fill="S1", group=paste(fileIn$DOY_S1, sep=""))) +
geom_boxplot(aes(x=DOY_RS2, y=RS2_VV, alpha=0.5, fill="RS2", group=paste(fileIn$DOY_RS2, sep=""))) +
scale_fill_manual(name="Sensor", values=c("S1"="brown", "RS2"="grey"))

but I would like to define the trasparency in scale_fill_manual. In google I found this solution, but doesn't work

plotSerie <- ggplot(fileIn)+
geom_boxplot(aes(x=DOY_S1, y=S1_VV, fill="S1",group=paste(fileIn$DOY_S1, sep=""))) +
geom_boxplot(aes(x=DOY_RS2, y=RS2_VV, fill="RS2", group=paste(fileIn$DOY_RS2, sep=""))) +
scale_fill_manual(name="Sensor", values=alpha(c("S1"="brown", "RS2"="grey"), 0.5))+

can you help me?

Thank you very much

Upvotes: 0

Views: 78

Answers (1)

Roland
Roland

Reputation: 132969

Specify alpha in the geom:

ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
  geom_boxplot(alpha = 0.5) +
  scale_fill_manual(values = c(setosa = "blue",
                               versicolor = "green",
                               virginica = "red"))

The transparency is adopted for the legend automatically.

Of course, you can also map a variable to a transparency scale with aes.

Upvotes: 2

Related Questions