Home > front end >  Add borders to points with different sizes and shapes in GGplot2
Add borders to points with different sizes and shapes in GGplot2

Time:03-21

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") 

enter image description here

How can i do this?

CodePudding user response:

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") 

enter image description here

CodePudding user response:

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.

  • Related