install.packages("ggplot2") #install ggplot
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
library(ggplot2) #ensure ggplot installed
data(mpg) #load in data
ggplot(mpg, aes(x=hwy))+ #specify data and x axis
geom_histogram(binwidth= 2)+ #create histogram and change bandwidth
labs( #change axes and title names
x= "Fuel Usage", #name x axis
title = "Highway Fuel Effiency" #create title
) +
theme_minimal() #minimal theme
What range of highway fuel efficiency values is most common? 15-35 is the range with the most values.
Does the distribution appear symmetric, skewed, or multimodal? The distribution is skewed right.
ggplot(mpg, aes(x= displ))+ #create plot with mpg dataset
geom_histogram(binwidth=0.5, fill= "purple1", col="purple4")+ #shorten binwidth, color purple & darker purple outline
labs(
x= "Displacement", #name x axis
title = "Overall Engine Displacement" #create title
)+
theme_minimal() #minimal theme
Question
ggplot(mpg, aes(x= displ))+ #create plot with mpg data
geom_density(fill= "skyblue", col= "blue4", alpha=0.5)+ #fill blue, dark blue outline, lower opacity
labs(
x = "Displacement", #name x axis
y = "Density", #name y axis
title = "Engine Displacement" #add title
)+
theme_minimal() #minimal theme
Question
ggplot(mpg, aes(x= displ, fill = class))+ #create plot using mpg, fill by vehicle class
geom_density(alpha= 0.5)+ #lower opacity
guides(fill= guide_legend(title="Vehicle Class")) #change legend title
Question
ggplot(mpg, aes(x=hwy, color=class))+ #plot mpg data & color by vehicle class
geom_freqpoly(binwidth=2) #change binwidth
Question
ggplot(mpg, aes(x= displ, y=hwy))+ #create mpg plot with displ & hwy
geom_density_2d(aes(fill= after_stat(level)), geom= "polygon")+ #map fill to density + geom polygon
theme_bw()+ #change theme
labs(
title = "Displacement & Fuel Efficiency", #add title
x = "Displacement", #title x axis
y = "Fuel Efficiency", #title y axis
fill = "Density Level"
)
## Warning in geom_density_2d(aes(fill = after_stat(level)), geom = "polygon"):
## Ignoring unknown parameters: `geom`
## Warning in geom_density_2d(aes(fill = after_stat(level)), geom = "polygon"):
## Ignoring unknown aesthetics: fill
Question
data(diamonds) #load in diamonds data
install.packages("ggExtra") #install ggExtra
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
install.packages("ggthemes") #install ggthemes
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
library(ggExtra) #ensure ggExtra installed
library(ggthemes) #ensure ggthemes installed
diamond_plot <- ggplot(diamonds, aes(x= carat, y= price, color = color))+ #create diamond_plot
geom_point(size=2, alpha= 0.2)+ #scatterplot w lowered opacity
theme_minimal()+ #minimal theme
labs(
x = "Carat", #name x axis
y = "Price", #name y axis
title = "Diamond Carats versus Price" #add title
)
ggMarginal(diamond_plot, type = "density", groupColour= TRUE, groupFill= TRUE)
Question