ggscatter to display important statistics on point data
ggscatter in RThis work is developed using part of the code from RUFORUM, specifically from 1 hr 24 minutes 55 seconds time-stamp. While watching the training on YouTube, I realized there could be more possibilities in using ggscatter. Good job by Makerere University staff and collaborators.
library(ggpubr)
ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris')
## `geom_smooth()` using formula 'y ~ x'
The plot can also be faceted by Species
ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species')
## `geom_smooth()` using formula 'y ~ x'
It is also possible to easily add ellipses around points:
ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species', ellipse = TRUE)
## `geom_smooth()` using formula 'y ~ x'
It is possible to change the transparency of the allipse in revealing points encapsulated by the ellipses.
ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species', ellipse = TRUE, ellipse.alpha = 0.5)
## `geom_smooth()` using formula 'y ~ x'
Further, it is possible to add mean points on the plots. One can thus easily tell the mean point of the data.
ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species', ellipse = TRUE, ellipse.alpha = 0.5, mean.point = TRUE)
## `geom_smooth()` using formula 'y ~ x'
Can also generate spokes from the mean to the data points, that is, star plot.
ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species', ellipse = TRUE, ellipse.alpha = 0.5, mean.point = TRUE, star.plot = TRUE)
## `geom_smooth()` using formula 'y ~ x'
Can use any of the many themes from ggplot2 package.
library(ggplot2)
ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species', ellipse = TRUE, ellipse.alpha = 0.3, mean.point = TRUE, star.plot = TRUE, ggtheme = theme_dark())
## `geom_smooth()` using formula 'y ~ x'
There are many more possibilities in ggscatter that one can use to design plot to their liking.
Best of luck coding in R!