2

I have this data

set.seed(28100) 
my_data <- data.frame(cat = factor(sample(c('cat1','cat2'), 50, replace = T)),
                      x = sample(1:100, 50, replace = T),
                      y = sample(1:100, 50, replace = T),
                      value = sample(1:100), 50, replace = T)

And I want to color my observations based on two dimensions. First, I want to distinguish between the categories by colouring based on the factor variable cat. Second, I want to scale the colour based on the continuous variable value.

This doesn't give the expected result:

require(ggplot2)
ggplot(my_data, aes(x=x, y=y, colour=cat, fill=value)) +
  geom_point(size=5) +
  theme_bw()

Is it possible by tweaking the arguments of ggplot() or geom_point()?

CptNemo
  • 6,455
  • 16
  • 58
  • 107
  • 3
    You can use `alpha` : `ggplot(my_data, aes(x=x, y=y, colour=cat, alpha=value))` – HubertL Sep 02 '16 at 01:59
  • or use `shape` for discrete and and `color` for continuous :`ggplot(my_data, aes(x=x, y=y, shape=cat, col=value))` – HubertL Sep 02 '16 at 02:03
  • 1
    The `alpha` idea is good @HubertL – Pierre L Sep 02 '16 at 02:10
  • What about colouring by your factor? and sizing by value `aes(colour = cat, size = value)` and `scale_size_continuous(range = c(2,4))`. Just another idea – Nate Sep 02 '16 at 02:20
  • 3
    if you want to continue with the two colour strategy you need to assign `geom_point(shape = 21)` shapes 21:25 have a fill aes slot in them, see this answer : http://stackoverflow.com/questions/15965870/fill-and-border-colour-in-geom-point-scale-colour-manual-in-ggplot – Nate Sep 02 '16 at 02:28

0 Answers0