ggplot2 is a package in RStudio that focuses on aesthetics, geometry, and data. The plots produced using this package are much more pleasing to look at, compared to the basics you can make in RStudio. However, the commands are quite different, as you can remember from using the package in the plot styles module to create stacked and grouped bar plots.


Scatter plots (with simple linear regression)


Any time you use the ggplot2 package, do not forget to load the ggplot2 package.


library(ggplot2)


Load your data into R and then use the skeleton of this code.


gplot(dataset, aes(x=xvalues, y=yvalues, color=levelsnames)) + 
  geom_point(shape=number) + scale_color_manual(values=vector) + geom_smooth(method=lm, se = FALSE, fullrange = TRUE) +
  labs(x = "xlabel", y = "ylabel", title = "maintitle", color ="levelsnames")


To include a specific color or range of colors, combine HTML color codes into a vector and set this equal to the values in scale_color_manual() code. method=lm puts your data into a linear model and includes the regression line of your data. Labels are simply labels.


vector <- c("#numbers")


Here is an example using vital capacity data!

library(ggplot2)

vc <- read.csv(url("https://raw.githubusercontent.com/nmccurtin/CSVfilesbiostats/master/vc%20(1).csv")) ##update this

## Create a color palette! We can call this one "pal"

pal <- c("#e600ac", "#e600e6", "#e60073", "#e60039")

## Which will correspond to this part of your code `scale_color_manual(values=pal)`

ggplot(vc, aes(height, vc, color=sex)) + geom_point() + scale_color_manual(values=pal) + geom_smooth(method = lm, se = FALSE) + labs(x = "Height (in)", y = "Vital Capacity (mL)", title = "Vital Capacity Data of BIO-204", color ="Sex") +  theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 20))