This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

plot(cars)

#install.packages("tidyverse")
library(tidyverse) 
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.0     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2) 

data_mtcars <- mtcars

head(data_mtcars)
data_mtcars$am <- as.factor(data_mtcars$am)
data_mtcars$cyl <- as.factor(data_mtcars$cyl)
ggplot(data_mtcars, aes(x = wt, y = mpg, color = cyl)) +
geom_point() + # Add points to the plot
labs(title = "Weight vs. Miles Per Gallon", x = "Weight (1000 lbs)", y = "Miles Per Gallon") # Add plot labels

data_mtcars_line <- data_mtcars %>% mutate(index = row_number()) #add index column so we can plot it

ggplot(data_mtcars_line, aes(x = index, y = mpg)) +
geom_line() + # add a line to the plot
labs(title = "Miles Per Gallon by Index", x = "Index", y = "Miles Per Gallon") # add plot labels

hp_by_cyl <- data_mtcars %>% group_by(cyl) %>% summarize(avg_hp = mean(hp))

ggplot(hp_by_cyl, aes(y = cyl, x = avg_hp)) +
geom_bar(stat = 'identity') + # Create bars based on the calculated averages
labs(title = "Average HP by Cylinder Count", y = "Cylinder Count", x = "Average Horsepower") # Add plot labels

bar_data_mtcars <- data_mtcars %>% group_by(cyl) %>% summarize(mpg = mean(mpg), disp = mean(disp), hp = mean(hp), wt = mean(wt)) %>% pivot_longer(cols = c("mpg", "disp", "hp", "wt"), names_to = "Measurement", values_to = "Average")

ggplot(bar_data_mtcars, aes(x = cyl, fill = Measurement, y = Average)) +
geom_bar(stat = "identity") + 
labs(title = "Average Measurements by Cylinder Count", x = "Cylinder Count", y = "Average Measurement")

Based on the data 4 cylinder cars on average make much less power than 6 and 8 cylinder cars but get much more MPGs than the other 2. 6 cylinders seem to be in the middle making more power than 4 and having more MPGs than 8. Cars with more displacement seem to make more power and less MPGs.