2

My goal is to place the border around points in scatter plot WHICH has the gradient color and gradient shape based on the value (as you can see in following script).

ggplot(filname, aes(Va1, Var2, col= PR, size=PR)) +
  geom_point() +
  labs(list(title = "Title", y = "Var2", x = "Var1")) +
  xlim(0, 150) +
  scale_color_gradientn(colours = rainbow(7)) +
  scale_x_continuous(breaks=seq(0, 150, 12))

** PR is the third Var in my data. 

The generated plot enter image description here

I found the following questions here:

but really they do not work in my case due to the two reasons I bolded above, I want to keep the gradient color and gradient shape but at the same time add the border around the points to make them more appreciable.

Basically, to make outline colour in ggplot I found we would have :

  1. fill
  2. colour for border

Considering this when I act accordingly as below

ggplot(PR_Grt100_REL_80, aes(Age, SC, col= PR, size=PR)) + 
  geom_point(aes (fill= PR), colour = "black") +
  labs(list(title = "Title", y = "Var2", x = "Var1")) +
  xlim(0, 150) +
  scale_color_gradientn(colours = rainbow(7)) +
  scale_x_continuous(breaks=seq(0, 150, 12))

I would get the following graph!

enter image description here

Any help is highly appreciated?

Community
  • 1
  • 1
Daniel
  • 1,202
  • 2
  • 16
  • 25
  • Difficult to test without the data but you use `scale_color_gradientn` which apply to the colour, and I think it would make sense to use `scale_fill_gradientn` instead. Also drop `col=PR` in the first aes (as you use fill in `geom_point` – timat Jan 11 '17 at 15:15
  • @timat Thanks but it did not change anything. Regarding the data, it is huge and unfortunately impossible to put them here! – Daniel Jan 11 '17 at 15:24
  • 1
    you could add just the first 10 rows of it with `dput(head(df, 10))` or alternatively use a built in set like `mtcars` or `iris` – Nate Jan 11 '17 at 15:33

1 Answers1

3

I think all you need to do is to use a shape that respects fill and color. Shapes 21:25 have this property, http://sape.inf.usi.ch/quick-reference/ggplot2/shape.

Using mtcars:

library(ggplot2)
ggplot(mtcars, aes(mpg, hp, fill = cyl, size = cyl)) +
    geom_point(shape = 21, stroke = 2) + # change the thickness of the boarder with stroke
    scale_fill_gradientn(colours = rainbow(7)) +
    scale_size(range = c(2,6)) # only for example visibility

enter image description here

Nate
  • 10,361
  • 3
  • 33
  • 40