add mpg data plot example
This commit is contained in:
29
mpg_plots.r
Normal file
29
mpg_plots.r
Normal file
@@ -0,0 +1,29 @@
|
||||
library(tidyverse)
|
||||
|
||||
# take the mpg data set and pipe it to the mutate function
|
||||
# then add a new column to the data set and save to mpg_metric
|
||||
mpg_metric <- mpg %>%
|
||||
mutate(city_metric = 0.425144 * cty)
|
||||
|
||||
# View the metric data set
|
||||
View(mpg_metric)
|
||||
|
||||
# group the mpg dataset by classes
|
||||
# then summarize the data by mean and median cty
|
||||
mpg %>%
|
||||
group_by(class) %>%
|
||||
summarise(mean(cty),
|
||||
median(cty))
|
||||
|
||||
# Data viz with ggplot2
|
||||
ggplot(mpg, aes(x = cty)) +
|
||||
geom_histogram() +
|
||||
labs(x = "City mileage")
|
||||
|
||||
# Scatter plot between city and highway mileage
|
||||
# this shows a linear relationship
|
||||
# then add a regression line on top
|
||||
ggplot(mpg, aes(x = cty,
|
||||
y = hwy)) +
|
||||
geom_point() +
|
||||
geom_smooth(method = "lm")
|
||||
Reference in New Issue
Block a user