The following is a basic introduction to creating scatterplots. See https://rpubs.com/pcjapan/696485 for the main document. This uses datasets provided with the R installation, but once you are familiar with the code it should be easy to adapt this to your own needs.
Install the necessary packages:
install.packages(ggplot2, hrbrthemes, ggExtra)
Load the packages into R:
library(ggplot2)
library(hrbrthemes)
Use the iris dataset. This is provided natively by R and does not have to be imported:
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
Basic scatterplot:
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point()
Modify the above. First colour by group:
sp <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_point(size=6) +
theme_ipsum() #can change theme easily with use of added packages
#print to screen
sp
Differentiate the data by groups. Use the sp object from above and add ellipses:
sp +
stat_ellipse()
Examples from R Graph Gallery: https://www.r-graph-gallery.com/
Load the ggExtra library:
library(ggExtra)
Use the mtcars dataset, again part of the base R installation:
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Create the basic plot:
p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, size=cyl)) +
geom_point() +
theme(legend.position="none")
p
Add marginal histogram:
p1 <- ggMarginal(p, type="histogram", size=10)
p1
Add marginal density plot:
p2 <- ggMarginal(p, type="density")
p2
p2 <- ggMarginal(p, type="histogram", fill = "slateblue", xparams = list(bins=10))
p2
Add marginal boxplot:
p3 <- ggMarginal(p, type="boxplot")
p3
Show the marginal plot for only the x axis:
p4 <- ggMarginal(p, margins = 'x', color="purple", size=4)
p4