Colour Scale Only For Overplotted Points

24 views
Skip to first unread message

Dario Strbenac

unread,
Apr 6, 2015, 11:00:09 PM4/6/15
to ggp...@googlegroups.com
I have a data frame of two categories of points. One category has many fewer observations, so I want to plot the points after the larger category's points have already been plotted. Here's a small example :

plotData <- data.frame(category = rep(LETTERS[1:2], c(900, 100)),
                    group = sample(LETTERS[24:26], 1000, replace = TRUE),
                    x = rnorm(1000),
                    y = rnorm(1000))

plotA <- plotData[plotData[, 1] == 'A', ]
plotB <- plotData[plotData[, 1] == 'B', ]

library(scales)
groupColours <- hue_pal()(3)

library(ggplot2)
ggplot(aes(x = x, y = y), data = plotA) + geom_point() + geom_point(data = plotB, colour = group, alpha = 0.65)  # Error about 'group' not found. Also need to use scale_colour_manual, but only for the smaller category.

How can I make the smaller category's points be coloured according to group using the colour scale which I picked ?

Doug Mitarotonda

unread,
Apr 7, 2015, 10:30:49 AM4/7/15
to Dario Strbenac, ggp...@googlegroups.com
Thanks for the reproducible example! Does this work for you?

ggplot(aes(x = x, y = y), data = plotA) + geom_point() + geom_point(aes(colour = group), data = plotB, alpha = 0.65) + scale_colour_manual(values = groupColours)

As an aside, when I use two different data sources, I would typically write this as:

ggplot() + geom_point(aes(x = x, y = y), data = plotA) + geom_point(aes(colour = group), data = plotB, alpha = 0.65) + scale_colour_manual(values = groupColours)


--
--
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.

Dario Strbenac

unread,
Apr 7, 2015, 8:00:13 PM4/7/15
to ggp...@googlegroups.com, dario....@gmail.com
This answers my question. One point to note is that the second example is missing x and y parameters for the second aes usage. Once these are added, it also works.

ggplot() + geom_point(aes(x = x, y = y), data = plotA) + geom_point(aes(x = x, y = y, colour = group), data = plotB, alpha = 0.65) + scale_colour_manual(values = groupColours)

Doug Mitarotonda

unread,
Apr 7, 2015, 8:19:40 PM4/7/15
to Dario Strbenac, ggp...@googlegroups.com
Yes, my mistake. I forgot to put the x and y in, sorry!

On Apr 7, 2015, at 5:00 PM, Dario Strbenac <dario....@gmail.com> wrote:

This answers my question. One point to note is that the second example is missing x and y parameters for the second aes usage. Once these are added, it also works.

ggplot() + geom_point(aes(x = x, y = y), data = plotA) + geom_point(aes(x = x, y = y, colour = group), data = plotB, alpha = 0.65) + scale_colour_manual(values = groupColours)

Reply all
Reply to author
Forward
0 new messages