The goal of this tutorial is to learn different ways to introduce a third variable in a scatter plot in ggplot using shape, colour and size in geom_point.
# First we load the libraries
library(ggplot2)
# In this example we will use the open repository of plants classification Iris.
data("iris")
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
# Let's plot the petal length vs petal width
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length))
# We can add a third variable to the plot using colour
# Notice that the color parameter should be set inside of the aes function, otherwise the effect will be different
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length, color = Species))
# See what happens if we define de colour outside
# ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length), color = iris$Species)
# Error in grDevices::col2rgb(colour, TRUE) : invalid color name 'setosa'
# ggplot is expecting a name of a colour and insted it gets the character "setosa", then it doesn't know what to do
# However we could use a defined colour and it will work
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length), color = "blue")
# ggplot comes with predefined shapes for the data points
# We can ask different shapes for different values of the third variable
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length, shape = Species))
# Notice that we could combine both parameters in order to separate better points that overlap
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length, shape = Species, color = Species))
# We can define the size of the data points as a variable
# Using size for a discrete variable is not advised.
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length, size = Species))
# Again it can be combined with the other parameters for better separation of the third variable points
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length, size = Species, color = Species, shape = Species))
In this tutorial we have learnt how to use different parameters as shape, colour and size to introduce a third variable in our scatter plots using geom_point.