1. Learn to plot dotplots using ggplot. The examples show how to generate the dotplots.
  2. Change shapes, colours and transparency of the dots.
  3. How to create labels for the dots using geom_text.
  4. How to create labels using the ggrepel library to avoid overlapping of dots.

youtube video link with explanations for these examples https://youtu.be/VavBoQuV4aM

We will be using thw following packages

#install.packages("ggplot2")
library(ggplot2)
library(ggrepel)

Using the built in dataset cars use head(cars) command to see the data in the cars dataset

head(cars)

we will create a dotplot using speed and dist columns in the cars dataset

p <- ggplot(cars, aes(x= speed, y = dist)) 
p <- p +  geom_point(color = "red", shape = 2 ) 
p <- p + theme_classic()
p <- p + labs(title = "Dot Plot") 
p <- p + labs(subtitle ="Speed vs Distance")
p <- p + labs(caption = paste0('n=', nrow(cars)))
p <- p + labs(x ="Speed", y = "Distance")
p

Changing the transparency level of the dots using the alpha = 0.5. You can change the alpha levels as needed. lower number (eg. alpha = 0.2 means more transparency)

p <- ggplot(cars, aes(x= speed, y = dist)) 
p <- p +  geom_point(alpha = 0.5) 
p <- p + theme_classic()
p <- p + ggtitle("Speed Vs Dist")
p <- p + labs(subtitle = "Transparency of the dots can be controlled by alpha") 
p

We can colour each dot using the speed variable in our cars dataset by using the color = speed

p <- ggplot(cars, aes(x= speed, y = dist, color = speed)) 
p <- p +  geom_point() 
p <- p + theme_classic()
p <- p + ggtitle("Speed Vs Dist coloured by speed")
p

We can also colour the dots in a gradient using the command scale_color_gradient(low =“green”,high =“red”)

p <- ggplot(cars, aes(x= speed, y = dist, color = speed)) 
p <- p +  geom_point() 
p <- p + theme_classic()
p <- p + ggtitle("Speed Vs Dist coloured by speed, controlling the gradient colour")
p <- p + labs(subtitle = "Using our custom defined low and high colours for the gradient") 
p <- p + scale_color_gradient(low ="green",high ="red")
p <- p + theme(legend.position = "right")
p

Adjusting the legend position, background colour, fill, key size etc.

p <- ggplot(cars, aes(x= speed, y = dist, color = factor(speed))) 
p <- p +  geom_point()
p <- p + theme_classic()
p <- p +  ggtitle("Speed Vs Dist coloured by factor(speed)")
p <- p + labs(subtitle = "Changing the placement and other aspects of the legend") 
p <- p + theme(legend.position = "top"
               , legend.background = element_rect(color ="red", fill = "green")
               , legend.key = element_rect(fill = "yellow")
               , legend.key.size = unit(0.5, "cm")
               ,  legend.key.width = unit(1,"cm")
               
               )
p <- p + labs(color ="Speed")
p

Changing the shape of the dots using shape numbers.

p <- ggplot(cars, aes(x= speed, y = dist)) 
p <- p +  geom_point(shape = 2) 
p <- p + theme_classic()
p <- p + ggtitle("Speed Vs Dist Shape = 2")
p

shp = 1
p <- ggplot(cars, aes(x= speed, y = dist)) 
p <- p +  geom_point( shape = shp) 
p <- p + theme_classic()
p <- p + ggtitle("Speed Vs Dist")
p <- p + labs(subtitle = paste0('Using Shape =' , shp)) 
p

For the purpose of demonstrating various shapes, I have created a function to create the same chart with different shape number values.

Following charts demonstrate the available shape for the dots.

# Let us create a simple function to 
# create the dot chart using specified shape type from the function

createallshapes <- function(shp){
  p <- ggplot(cars, aes(x= speed, y = dist)) 
  p <- p +  geom_point( shape = shp) 
  p <- p + theme_classic()
  p <- p + ggtitle("Speed Vs Dist")
  p <- p + labs(subtitle = paste0('Using Shape =' , shp)) 
  p
}

You can change the color and the size as well of the dots.

p <- ggplot(cars, aes(x= speed, y = dist)) 
p <- p +  geom_point(shape = 2, color = "red", size = 3) 
p <- p + theme_classic()
p <- p + ggtitle("Speed Vs Dist")
p <- p + labs(subtitle = 'Changing the shape, colour and size of the dots') 
p

Using the label for the dots using the geom_text command and using the dist column to create the label values.

p <- ggplot(cars, aes(x= speed, y = dist)) 
p <- p +  geom_point(shape = 2, color = "red", size = 3) 
p <- p + geom_text(aes(label = dist))
p <- p + theme_classic()
p <- p + ggtitle("Speed Vs Dist Shape = 2")
p <- p + labs(subtitle = 'Placing the text on the points') 
p

Adjusting the placement location of the text using vjust

p <- ggplot(cars, aes(x= speed, y = dist)) 
p <- p +  geom_point(shape = 2, color = "red", size = 3) 
p <- p + geom_text(aes(label = dist), vjust = -0.1)
p <- p + theme_classic()
p <- p + ggtitle("Speed Vs Dist")
p <- p + labs(subtitle = 'Adjusting the placement location of the text using vjust') 
p

Adjusting the size of the text using size command in geom_text

p <- ggplot(cars, aes(x= speed, y = dist)) 
p <- p +  geom_point(shape = 2, color = "red", size = 3) 
p <- p + geom_text(aes(label = dist), vjust = -0.5, size = 3)
p <- p + theme_classic()
p <- p + ggtitle("Speed Vs Dist")
p <- p + labs(subtitle = 'Reducing the size of the text') 
p

Using the geom_text_repel which avoids overlapping of text

Using ggrepel we can ensure that the overlapping of the dots is avoided. This is a better way as it avoids the overlapping and makes your charts elegant.

library(ggrepel)
p <- ggplot(cars, aes(x= speed, y = dist)) 
p <- p +  geom_point(shape = 1, color = "red", size = 3) 
p <- p + geom_text_repel(aes(label = dist), vjust = -0.5, size = 3)
p <- p + theme_classic()
p <- p + ggtitle("Speed Vs Dist using ggrepel for text placement")
p <- p + labs(subtitle = 'Using the geom_text_repel which avoids overlapping of text') 
p

youtube video link with explanations for these examples https://youtu.be/VavBoQuV4aM