3

I want my lines and points to be slightly lighter for the last 7 data points. I tried to use alpha, but no matter how small the increment I use, the points are way too light. Can I use alpha (and if yes, how), or do I have to mess with the colors manually?

I'm using tidyverse 1.3.0.

Example follows.

foo <- data.frame(x=seq(1:10))
foo$y <- foo$x
foo$alpha <- c(rep(1, 7), rep(0.5, 3))
ggplot(foo, aes(x, y)) + geom_point(aes(alpha=alpha))

with alpha=0.5:

alpha=0.5, too light

with alpha=0.999, same lightness (too light):

alpha=0.999, too light

tjebo
  • 21,977
  • 7
  • 58
  • 94
dfrankow
  • 20,191
  • 41
  • 152
  • 214

2 Answers2

3

If you want to set alpha to a specific value you have to set it as an argument outside of aes, e.g. geom_point(alpha = foo$alpha) or make use of scale_alpha_identity. Try this:

foo <- data.frame(x=seq(1:10))
foo$y <- foo$x
foo$alpha <- c(rep(1, 7), rep(0.5, 3))

library(ggplot2)

ggplot(foo, aes(x, y)) + 
  geom_point(aes(alpha=alpha)) +
  scale_alpha_identity()

foo$alpha <- c(rep(1, 7), rep(0.1, 3))

ggplot(foo, aes(x, y)) + 
  geom_point(aes(alpha=alpha)) +
  scale_alpha_identity()

stefan
  • 90,330
  • 6
  • 25
  • 51
2

Regarding obtaining very specific values with your alpha, see this related thread.

If you don't need "transparency", but just want to lighten up the colors a bit, then you can do that with the shades or the colorspace package.

Both have advantages and disadvantages. The cool thing about the shades package is that you can modify entires palettes, like the brewer palettes.

If you have only one color to modify, the colorspace package is a bit easier. Here using the colorspace package:

library(ggplot2)
library(colorspace)

foo <- data.frame(x = seq(1:10))
foo$y <- foo$x
foo$lighter <- c(rep("black", 7), rep(lighten("black", 0.5), 3))

ggplot(foo, aes(x, y)) +
  geom_point(aes(color = lighter)) +
  scale_color_identity()

tjebo
  • 21,977
  • 7
  • 58
  • 94