简体   繁体   中英

Add borders to points with different sizes and shapes in GGplot2

I want to plot a scatterplot where the points are different shapes and sizes. Importantly, I would like black borders around each shape. The dataframe is below:

> dput(human.correlations[1:5, c(2:5)])
structure(list(variable = c("Caudate.Astrocytes", "Caudate.dSPNs_eccentric", 
"Caudate.dSPNs_matrix", "Caudate.dSPNs_patch", "Caudate.Endothelia1"
), correlation = c(0.746433126, 0.80268901, 0.783305333, 0.790514121, 
0.706648893), Number.Of.Wins = c(0L, 0L, 0L, 0L, 0L), Region = c("Caudate", 
"Caudate", "Caudate", "Caudate", "Caudate")), row.names = c(NA, 
5L), class = "data.frame")


mid <- 0.6 #set the midpoint


#scatterplot of figure
ggplot(human.correlations, aes(y=Number.Of.Wins, x=Region, color = correlation, shape = Cell.Class, size=Number.Of.Wins))+
  geom_quasirandom(groupOnX=TRUE)+
  scale_color_gradient2(midpoint=mid, low="white", mid="yellow", high="red")+
  scale_size(range = c(2,9))+
  theme_bw()+
  ylab("Number of Wins")+
  xlab("Brain region") 

在此处输入图像描述

How can i do this?

You need to change the color aesthetic to a fill aesthetic, and use shapes 21, 22, 23, which are outlined shapes taking both a fill and a color. Set their color to black to get a black outline.

Note that your example data frame was missing the Cell.Class column that actually maps to your shapes, so I added a random one:

# Make example reproducible
human.correlations$Cell.Class <- c("Excitatory", "Glia", "Inhibitory Neuron", 
                                   "Glia", "Excitatory")

The plotting code is then

library(ggplot2)
library(ggbeeswarm)

ggplot(human.correlations, aes(y = Number.Of.Wins, x = Region, fill = correlation, 
                               shape = Cell.Class, size = Number.Of.Wins)) +
  geom_quasirandom(groupOnX = TRUE, color = 'black') +
  scale_fill_gradient2(midpoint = mid, low = "white", mid = "yellow", high = "red") +
  scale_size(range = c(2, 9)) +
  scale_shape_manual(values = c(21, 22, 23)) +
  theme_bw() +
  ylab("Number of Wins") +
  xlab("Brain region") 

在此处输入图像描述

I have not seen geom_quasirandom before but it likely needs you to utilize the colour argument, so try

geom_quasirandom(groupOnX=TRUE, colour = "black")

and then rerun. If this does not work, then the shapes that are being selected by that function do not have borders that can be changed. If so, you will need to select these manually.

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