add mpg data plot example

This commit is contained in:
aaron
2024-01-27 12:18:32 +01:00
parent f9273f7c06
commit 3945172290

29
mpg_plots.r Normal file
View 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")