简体   繁体   中英

How to Place a border around points for the scatter plot with gradient color and shape in R?

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 在此处输入图片说明

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!

在此处输入图片说明

Any help is highly appreciated?

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM