Sunday 11 April 2021

BUILDING DOT PLOTS IN R SIMILAR TO THOSE WITH GraphPad

 

Have you ever had the necessity to build a dot plot in R that looks like the nice ones obtained with GraphPad?


Here, an example of what I am talking about:



To show you the solution I found, I'll use the chickwts preloaded dataset and ggplot2.

First, we'll load the packages and data needed:

library(ggplot2)
library(dplyr)
data(chickwts)  #the dataset shows chicken weights depending on food type 


In our example, we'll shorten the dataset to show only the weights in the sunflower and soybean group:

Dataset <- chickwts %>%
   filter(feed == "sunflower" | feed == "soybean")


Using the geom_dotplot() function, we can build the following graph:




As you can see, it shows the individuals as dots, coloured by category, with a black bar indicating the mean of the group.
Here, you have the code:

Dataset %>%
ggplot(aes(feed, weight, fill = feed)) +
geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 1, #stackratio moves the dots                                                                                       #towards the left or the right
  dotsize = 0.7, binpositions = "all") +
stat_summary(fun = mean, geom = "crossbar", width = 0.7) +  #width change the length of the bar
theme_bw()



If you want to change the colour of the bar, you can do it by adding the colour argument in stat_summary().
In the following example I gave it the same colour of the dots:

Dataset %>%
ggplot(aes(feed, weight, fill = feed)) +
geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 1, 
  dotsize = 0.7, binpositions = "all") +
stat_summary(fun = mean, geom = "crossbar", width = 0.7, 
col = c("#9E0142","#3288BD")) +
scale_fill_manual(values = c("#9E0142","#3288BD")) +
theme_bw()







Arrived at this point, you could be still disappointed because the graph is not that similar to the original one.
So, since in geom_dotplot() is not possible to change the shape of dots, what to do?

The geom_jitter() function can help us:

Dataset %>%
ggplot(aes(feed, weight, fill = feed)) +
geom_jitter(aes(shape = feed, col = feed), size = 2.5, width = 0.1)+
stat_summary(fun = mean, geom = "crossbar", width = 0.7,
col = c("#9E0142","#3288BD")) +
scale_fill_manual(values = c("#9E0142","#3288BD")) +
scale_colour_manual(values = c("#9E0142","#3288BD")) +
theme_bw()


Changing the width argument in geom_jitter() you can move a bit the points, so they do not overlap. 
The use of both scale_fill_manual() and scale_colour_manual() allows giving the same colour to the legend and dots.


Although it is not exactly the same, it looks pretty similar to the first one, doesn't it?


And now the final customization.
Perhaps you would like to choose the symbols displayed. In this case, you can add a layer with scale_shape_manual():

Dataset %>%
ggplot(aes(feed, weight, fill = feed)) +
geom_jitter(aes(shape = feed, col = feed), size = 2.5, width = 0.1)+
stat_summary(fun = mean, geom = "crossbar", width = 0.7,
 col = c("#9E0142","#3288BD")) +
scale_fill_manual(values = c("#9E0142","#3288BD")) +
scale_colour_manual(values = c("#9E0142","#3288BD")) +
        scale_shape_manual(values = c(18, 25)) +
theme_bw()



You can find the symbols used in R here.

I hope you found this post useful.
As usual, if you have a better solution, please let me know!

No comments:

Post a Comment