By the end of this exercise, you will be familiar with basic data manipulation and visualization functions provided by the tidyverse package in R. This includes functions from dplyr and ggplot2 for data wrangling and visualization.
Use ggplot2 to create a scatter plot of mpg vs hp.
ggplot(data = mtcars, aes(x = hp, y = mpg)) +geom_point() +labs(title ="Scatter plot of MPG vs Horsepower",x ="Horsepower",y ="Miles per Gallon")
9. Create a Bar Plot
Use ggplot2 to create a bar plot showing the average mpg for each cyl.
ggplot(data = mtcars_grouped, aes(x =factor(cyl), y = avg_mpg)) +geom_bar(stat ="identity") +labs(title ="Average MPG for Each Cylinder",x ="Number of Cylinders",y ="Average MPG")
10. Create a Histogram
Use ggplot2 to create a histogram of the mpg values.
ggplot(data = mtcars, aes(x = mpg)) +geom_histogram(binwidth =2, fill ="blue", color ="black") +labs(title ="Histogram of MPG",x ="Miles per Gallon",y ="Frequency")
Step 4: Combining Everything
Combine all steps into a single script to practice the workflow from data manipulation to visualization.
library(tidyverse)# Load datasetdata(mtcars)# Data manipulationmtcars_selected <- mtcars %>%select(mpg, cyl, hp)mtcars_filtered <- mtcars_selected %>%filter(cyl >6)mtcars_mutated <- mtcars_filtered %>%mutate(hp_per_cyl = hp / cyl)mtcars_summary <- mtcars_mutated %>%summarize(avg_mpg =mean(mpg), avg_hp_per_cyl =mean(hp_per_cyl))mtcars_grouped <- mtcars_selected %>%group_by(cyl) %>%summarize(avg_mpg =mean(mpg))# Data visualization# Scatter plotggplot(data = mtcars, aes(x = hp, y = mpg)) +geom_point() +labs(title ="Scatter plot of MPG vs Horsepower",x ="Horsepower",y ="Miles per Gallon")# Bar plotggplot(data = mtcars_grouped, aes(x =factor(cyl), y = avg_mpg)) +geom_bar(stat ="identity") +labs(title ="Average MPG for Each Cylinder",x ="Number of Cylinders",y ="Average MPG")# Histogramggplot(data = mtcars, aes(x = mpg)) +geom_histogram(binwidth =2, fill ="blue", color ="black") +labs(title ="Histogram of MPG",x ="Miles per Gallon",y ="Frequency")
Bonus: “but I use GraphPad, this plot looks bad…”
library(ggprism)
Warning: package 'ggprism' was built under R version 4.3.3
ggplot(data = mtcars, aes(x = mpg)) +geom_histogram(binwidth =2, fill ="blue", color ="black") +labs(title ="Histogram of MPG",x ="Miles per Gallon",y ="Frequency") +theme_prism(base_size =16)
Exercise for you:
Apply the GraphPad theme to the scatter plot of MPG vs Horsepower