── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── 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
data(mpg)ggplot(mpg, aes(x = displ, y = hwy, color = class)) +geom_point(size =2, alpha =0.7) +theme_minimal() +labs(title ="Engine Displacement vs. Highway MPG by Vehicle Class",x ="Engine Displacement (displ)",y ="Highway MPG (hwy)",color ="Vehicle Class" )
For Part 1, I used ggplot to create the graph, separating the cars into different color coded dots to see the engine displacement versus how fuel efficient the engine is. However, a few problems arise when you do it this way. The first one is that the dots stack on each other because many cars share the exact same engine and fuel efficiency, especially when they have the same parent company. The second one is that even though the dots are color coded, cramming them all together makes it hard to differentiate the similar looking colors. The last problem is that mixing all the cars together makes it hard to compare classes or find trends in one specific car class.
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +geom_point(size =2, alpha =0.7) +facet_wrap(~ class) +theme_minimal() +labs(title ="Engine Displacement vs. Highway MPG Faceted by Vehicle Class",x ="Engine Displacement (displ)",y ="Highway MPG (hwy)" ) +theme(legend.position ="none")
In Part 2, I used facet wrap in my code because, as we saw in Part 1, we ran into quite a few problems where all the car classes were mixed into one graph. That made it unbelievably hard to read between them, spot any trends or differences within the same class, or compare any classes to each other. Because I used facet wrap here, I am able to separate each class into its own graph so it is nice and clean. Now we can actually compare the classes to each other and see how cars within the same class have different engine sizes and fuel efficiency. Overall, this makes it much easier to study car classes, their engine displacement, and their highway MPG fuel efficiency.
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +geom_point(size =2, alpha =0.7) +facet_grid(drv ~ cyl) +theme_minimal() +labs(title ="Engine Displacement vs. Highway MPG by Drivetrain and Cylinders",x ="Engine Displacement (displ)",y ="Highway MPG (hwy)",color ="Vehicle Class" )
For Part 3, I swapped out face wrap for face grid, which honestly allows us to take things to a whole another level, I would say. Because now we can compare two variables or more at the same time on the graph. Like for example, now we’re comparing drivetrain VS cylinders VS MPG VS engine displacement. I mean sure, there’s like three different graphs technically because we need to lay it out on the MPG scale and the cylinder size, highway MPG, fuel economy, drivetrain, and engine displacement at the bottom. Specifically, the drivetrain is mapped to the rows and the cylinders are mapped to the columns. When you run the code, some boxes appear empty because in this data set that was provided to me, there just isn’t any manufacturers or classes or any car models that have been built with those exact specifications of drivetrain, cylinders, displacement, or MPG. So in the end, I think face grid gives us a much more clear and deeper understanding of this data that we were provided with. For example, we can clearly see that for all-wheel drive cars, ones with eight-cylinder engines and much bigger displacement have absolutely horrible fuel efficiency, while smaller four-cylinder engines with front-wheel drive are totally different.
part 4
1.
The reason why the original part one scatter plot was quite difficult to read, especially if people don’t know cars, is because everything was just crowded in one area, the dots were overlapping each other, and the colors were quite similar and hard to distinguish. That was pretty difficult, at least for me. Because of that, I couldn’t really see individual trends or spot specific data points that I wanted to take a look at, or any correlation between classes or engine displacement because everything was just all over the place.
2.
Facet wrap actually takes care of the problem that I said on the top, that because all the information is all over the place, I can’t really analyze it. But now, because each class is separated into its own graph, I can analyze each class individually, see how the engine displacement and fuel efficiency affect each other in each class, which makes it a lot more easy to analyze the data because there’s less crowding, and you do not need to be constantly trying to analyze what color the dot is.
3.
Facet grid actually takes the previous facet wrap and takes it to a whole another level because now you can actually combine two variables to see how drivetrain or the different vehicle classes interact. So, because of that, you can actually now on one chart compare all the cars’ MPG, all the cars’ drivetrain, and its class in one place.
I would say facet wrap, for me at least, is the easiest one to read because it separates each class into its own plot and it shows that overall for each class that for some classes the difference might be smaller, but overall the relationship is that as engine size gets bigger, the fuel economy gets worse. And also, unlike facet grid and regular ggplot part one where everything is just scattered and it’s colored in the same place, this actually separates it and we can actually take a look at each class without feeling overwhelmed or getting confused about the colors or the classes.
5
I would say the only disadvantage I can think of as of right now is having too many variables because now each panel becomes smaller and smaller to fit in more information and it can make it very hard to read.