It is a graphic package implemented in R, it allows to make graphs in a very flexible way because it contains many different commands to perform specific functions when creating and manipulating graphs. In addition, this graphic package has an underlying grammar in layers, so that it is composed of independent components. These independent components can be combined in many different ways to make or modify the graphs. Each graph is made up of a few equal components: data, visual marks that represent data points (geoms), and a coordinate system. The necessary variables for the ggplot command are the data to use in data frame format and know which variables we will use as x and y:
ggplot (df, aes (x, y, ))
You can add more layers to the previous ggplot using the
+
operator, so the code is explicit as to which layers were added and in the order they were added.
here we will use the dataset
iris
:head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
Some examples:
- Plot using dots:
require('ggplot2')
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.4.1
ggplot(iris, aes(iris$Petal.Length, iris$Petal.Width)) +
geom_point(aes(color=Species, shape = Species)) +
xlab("Petal Length") +
ylab("Petal Width") +
ggtitle("IRIS1")
ggplot(iris, aes(iris$Petal.Length, iris$Petal.Width)) +
geom_point(aes(color = Petal.Length, shape = Species), size = 2) +
geom_vline(aes(xintercept = mean(Petal.Length)), color = "red", linetype = "longdash")+
geom_hline(aes(yintercept = mean(Petal.Width)), color = "red", linetype = "longdash") +
scale_color_gradient(low = "pink", high = "blue") +
xlab("Petal Length") +
ylab("Petal Width") +
ggtitle("IRIS2")
- Dots with a grid:
ggplot(iris, aes(iris$Petal.Length, iris$Petal.Width)) +
geom_point(aes(color=Species, shape = Species)) +
facet_grid(~Species) +
xlab("Petal Length") +
ylab("Petal Width") +
ggtitle("IRIS3")
- Density plots:
ggplot(iris, aes(Petal.Width, fill= Species, color = Species)) +
geom_density(alpha = 0.5) +
xlab("Petal Width") +
ggtitle("IRIS4")
ggplot(iris, aes(Petal.Width, fill= Species, color = Species)) +
geom_density(alpha = 0.5, position = 'stack') +
xlab("Petal Width") +
ggtitle("IRIS5")
- Boxplots:
ggplot(iris, aes(iris$Petal.Length, iris$Petal.Width)) +
geom_boxplot(aes(fill=Species)) +
ylab("Sepal Length") +
ggtitle("IRIS6")
ggplot(iris, aes(iris$Species, iris$Petal.Length)) +
geom_boxplot(aes(fill=Species)) +
stat_summary(fun.y= max, geom="point", shape=2, size=3) +
stat_summary(fun.y= min, geom="point", shape=1, size=3) +
stat_summary(fun.y= mean, geom="point", shape=0, size=3) +
xlab("Species") +
ylab("Sepal Length") +
ggtitle("IRIS7")