This vignette shows how to create a scatter plot based on the coefficients from (Standardised) Major Axis Estimation fit using the “smatr” package and “ggplot” in R.
Load packages
library(ggsmatr)
library(smatr)
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.3
Fit a SMA using smatr package:
data(iris)
<- sma(Petal.Width ~ Petal.Length+ Species, data=iris) fit
We want to plot the results using ggplot package:
# plot
ggsmatr(data = iris, groups = "Species", xvar = "Petal.Length", yvar =
"Petal.Width", sma.fit = fit)
#> group r2 pval
#> 1 setosa 0.110 0.019
#> 2 versicolor 0.619 0.000
#> 3 virginica 0.104 0.023
If we want to change the axis labels, label position and colors we can use the default arguments implemented in ggplot:
#Plot is saved as ggp object, and can be edited using ggplot parameters.
library(ggplot2)
+ xlab("x axis name") + ylab("y axis name") +theme(legend.position = "right",
ggp legend.title=element_blank()) + scale_colour_manual(values = c("black", "firebrick", "cornflowerblue"))