setting transparent points/dots with countour color

815 views
Skip to first unread message

Bogdan Tanasa

unread,
Feb 9, 2016, 3:32:30 AM2/9/16
to ggplot2
Dear all,

would appreciate please your advise on the following : I am displaying a scatter plot with points/circles in 2 colors;

please could you advise on how I can set up the 1st layer of circles as transparent circles with contour color ?

the code I have is :

ggplot(data = data, aes(y = y, x = x) )) +
  geom_point(aes(colour=z, size=z), alpha=0.4) +
  scale_color_manual(values = c("orange", "purple")) +
  scale_size_discrete(range = c(0.2, 2))

many thanks,

bogdan

Ben Bond-Lamberty

unread,
Feb 9, 2016, 3:42:21 AM2/9/16
to ggplot2
It's a little hard to know, as you didn't provide a reproducible
example. But as transparency is defined by the `alpha` aesthetic,
perhaps you want

... + geom_point(aes(colour=z, size=z, alpha=z)) +
scale_alpha_manual(values=c(0.3, 1.0))

or something like that?
Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Dennis Murphy

unread,
Feb 9, 2016, 11:30:09 AM2/9/16
to Bogdan Tanasa, ggplot2
Do you mean something like

ggplot(data = data, aes(y = y, x = x) )) +
geom_point(aes(colour=z, size=z, shape = z), alpha=0.4) +
scale_color_manual(values = c("orange", "purple")) +
scale_size_discrete(range = c(0.2, 2)) +
scale_shape_manual(values = c(1, 16))

As Ben suggested, your description of "transparent" is not clear - it
could mean alpha = 0 or an unfilled shape, such as shape = 1, or you
could also use color = "transparent" if you want to make the points
blend into the plot background.

Dennis

Bogdan Tanasa

unread,
Feb 9, 2016, 1:38:38 PM2/9/16
to ggplot2, tan...@gmail.com

Thanks Ben and Dennis , still working on a code, and will get back to you later.

Bogdan Tanasa

unread,
Feb 9, 2016, 4:33:14 PM2/9/16
to ggplot2, tan...@gmail.com

Dear Ben, and Dennis,

considering a simple example below, please  could you let me know how could I set up the FILL and TRANSPARENCY for the plot below in order to bring the RED and GREEN dots in the foreground and have the GREY dots in the background (in the real example, I have approx 5000 dots).

many thanks, appreciate the help !

mycolours <- c("U" = "green", "D" = "red", "U"="grey")

x <- c(1,2,3,4,5,6)
y <- c(1,2,3,4,5,6)
z <- c("U","U","D","D","N", "N")
t <- data.frame(x,y,z)

ggplot(data = t, aes(y = y, x = x)) +
                 geom_point(size=2, aes(colour=z)) +
                 scale_color_manual("Status", values = mycolours)

Dennis Murphy

unread,
Feb 10, 2016, 11:31:12 AM2/10/16
to Bogdan Tanasa, ggplot2
If you use a grey scale for the points you want to deemphasize, there
should be no problem:

mycolours1 <- c("U" = "darkgreen", "D" = "red", "N"="grey50")
mycolours2 <- c("U" = "darkgreen", "D" = "red", "N"="grey90")
mycolours3 <- c("U" = "darkgreen", "D" = "red", "N"="blue")

library(ggplot2)

# Notice that the levels of z are set in the data frame
# (Aside: Why did I define it this way instead of copying yours?)
DF <- data.frame(x = seq(6), y = seq(6),
z = factor(rep(c("U", "D", "N"), each = 2),
levels = c("U", "D", "N")))


# Modify grey scale
ggplot(data = DF, aes(y = y, x = x)) +
theme_bw() +
geom_point(size=3, aes(colour=z)) +
scale_colour_manual("Status", values = mycolours1)

# Higher values of grey produce lighter shades
ggplot(data = DF, aes(y = y, x = x)) +
theme_bw() +
geom_point(size=3, aes(colour=z)) +
scale_colour_manual("Status", values = mycolours2)

Just tweak the grey value to produce the effect you want.

Another approach is to map the alpha transparency level (aka
'opacity') of each mapped color value. In your example, the levels of
z are "D", "N" and "U", so you can define the values of the opacity
vector accordingly. (This was why I set the levels in the data frame,
BTW.)

# Modify opacity (alpha)
ggplot(data = DF, aes(y = y, x = x)) +
theme_bw() +
geom_point(size=3, aes(colour=z)) +
scale_colour_manual("Status", values = mycolours3)

opacity <- c(1, 1, 0.2) # values correspond to the level ordering of z
ggplot(data = DF, aes(y = y, x = x)) +
theme_bw() +
geom_point(size=3, aes(colour=z)) +
scale_colour_manual("Status",
values = alpha(mycolours3, opacity))

Again, you can adjust the values of opacity to taste.

Dennis

Bogdan Tanasa

unread,
Feb 10, 2016, 1:09:05 PM2/10/16
to Dennis Murphy, ggplot2
thanks a lot Dennis, highly appreciate it !
Reply all
Reply to author
Forward
0 new messages