# List any packages you need to use here
packages <- c("ggplot2", "readr", "tidyverse", "dplyr", "ggpubr")
#Check to see if any of your listed packages need installed
check_install_packages <- function(pkg){
if (!require(pkg, character.only = TRUE)) {
install.packages(pkg, dependencies = TRUE)
library(pkg, character.only = TRUE)
}
}
# Download the packages and read in the libraries if necessary
sapply(packages, check_install_packages)
## $ggplot2
## NULL
##
## $readr
## NULL
##
## $tidyverse
## NULL
##
## $dplyr
## NULL
##
## $ggpubr
## NULL
data("USArrests")
view(USArrests)
head(USArrests)
## Murder Assault UrbanPop Rape
## Alabama 13.2 236 58 21.2
## Alaska 10.0 263 48 44.5
## Arizona 8.1 294 80 31.0
## Arkansas 8.8 190 50 19.5
## California 9.0 276 91 40.6
## Colorado 7.9 204 78 38.7
?USArrests
USArrests data questions: 1: What are the variables available? murder, assault, urbanpop, and rape
2: How is each variable defined or calculated? muder, assault, and rape are numeric values calculated for one per 100,000 arrests urbanpop is defined as the percent urban population
3: Is each one numerical or categorical? each is numerical
#General format is going to be calling a ggplot, followed by the dataframe name (mtcars), followed by defining the X and Y variables of the graphic.
data("mtcars")
ggplot(mtcars, aes(x = mpg, y=hp)) +
#You then indicate the type of graph to make (in this case, a dotplot using points).
geom_point() +
#changing dots to size 2.4 and star shaped
geom_point(size = 2.4, shape = 8) +
#change theme to minimal
theme_minimal() +
#create cyl column into a color gradient
aes(colour = cyl) +
#change the legend position
theme(legend.position = "bottom") +
#labeling the graph
labs(title = "Effect of Horsepower on Fuel Efficiency", subtitle = "Categorized by Number of Cylinders", x = "Horsepower", y = "Fuel Efficiency (MPG)")
library(ggplot2)
#using a different demo data set to create another graph
library(ggplot2)
data(Irisi)
#create plot
ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) + geom_boxplot() +
#change theme
theme_classic() +
#labels
labs(title = "Patel Length of Iris by Species") +
#custom legend position
theme(legend.position = c(0.2,0.8))