Files
r-toolbox/ggplot_examples/mpg_plots.r
2024-01-27 15:25:00 +01:00

30 lines
732 B
R

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")