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

為{幸葍}努か 提交于 2019-12-08 11:33:49

问题


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:

  • Question 1
  • Question 2
  • Question 3

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?


回答1:


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



来源:https://stackoverflow.com/questions/41594211/how-to-place-a-border-around-points-for-the-scatter-plot-with-gradient-color-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!