## 2nd Graph# Get the 6 largest manufacturers by counttop_makers <- mpg %>%count(manufacturer) %>%top_n(6, n) %>%pull(manufacturer)# Filter the data to include only the top 6 manufacturersmpg_filtered <- mpg %>%filter(manufacturer %in% top_makers)# Create the faceted histograms for city mpgggplot(mpg_filtered, aes(x = cty)) +geom_histogram(binwidth =2, fill ="violetred2", color ="white") +facet_grid(class ~ manufacturer) +# Facet by class and cylinderslabs(x ="City MPG", y ="Count", title ="City MPG Distribution by Manufacturer, Class, and Cylinder", family=font) +theme_minimal() +theme(strip.text =element_text(size =10,family=font), # Adjust facet label sizepanel.spacing =unit(1, "lines"), # Add spacing between facetsaxis.text.x =element_text(size =8,family=font), # Adjust size of x-axis textaxis.text.y =element_text(size =8, family=font), # Adjust size of y-axis textaxis.title.x =element_text(size =12, family = font), # Set x-axis label font and sizeaxis.title.y =element_text(size =12, , family = font), # Set y-axis label font and sizeplot.title =element_text(size =14, family = font, hjust =0.5),panel.grid =element_blank(),panel.border =element_rect(color ="gray40", fill =NA, size =0.6) )
Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.
## 3rd Graph mh <-aggregate(hwy ~ class, data = data, mean)mc <-aggregate(cty ~ class, data = data, mean)par(mfrow =c(1, 2), mar =c(5,6,2,2), mgp =c(4, 1, 0.8), family = font, cex.main =1.2, cex.lab =1, font.main =1, font.lab =1, font.axis =1, cex.axis =0.7)# mean highway MPGbarplot(mh$hwy, names.arg = mh$class,horiz =TRUE, col ="violetred2", border ="black",xlab ="Mean MPG", ylab ="Type of Vehicle",xlim =c(0, 35),main ="Highway", las =1)abline(v =0, lwd =4, col ="black") # mean city MPGbarplot(mc$cty, names.arg = mc$class, horiz =TRUE, col ="white", border ="black", xlab ="Mean MPG", ylab ="", xlim =c(0, 35),main ="City", las =1)abline(v =0, lwd =4, col ="black")
## 4th Graphdata_sum <- mpg %>%group_by(class) %>%summarise(mh =mean(hwy), mc =mean(cty))ggplot(data_sum, aes(x = class)) +geom_col(aes(y = mh), fill ="violetred2", width =0.4, position =position_nudge(x =0.1)) +geom_col(aes(y = mc), fill ='white', color ="black", width =0.4, position =position_nudge(x =-0.1)) +labs(x ="Type of Vehicle", y ="Mean Miles per Gallon", title ="Mean MPG by Class and Vehicle Type") +theme_minimal() +theme(panel.grid =element_blank(),axis.line =element_line(color ="black"), axis.text.x =element_text(angle =45, hjust =1, family = font), axis.text.y =element_text(family = font),axis.title.x =element_text(family = font, size =12), axis.title.y =element_text(family = font, size =12), plot.title =element_text(family = font, size =14, hjust =0.5))