Visualizing Distributions in R

 The goal for this week was to make a distribution visualization in R and think about how well it worked. I used the usual mtcars dataset that comes with R for this purpose.


This multidimensional figure makes it evident that there is a negative correlation: as the weight of the automobile goes up, the MPG goes down. The grid also reveals that automobiles with 4, 6, and 8 cylinders are grouped together. This design follows the advice of Stephen Few and Nathan Yau, who say that tiny multiples with aligned axes are better for straightforward comparison than a single, overloaded chart.

I completely agree with Few's criticism that conventional ways of visualizing data, including layering graphs, might hide the underlying structure of a dataset. This way of breaking the facts into a grid makes the tale more clearer and more honest.












R-code

# Load the ggplot2 library library(ggplot2) # Create a density plot ggplot(mtcars, aes(x = mpg)) + geom_density(fill = "skyblue", color = "blue", alpha = 0.7) + labs( title = "Distribution of Miles Per Gallon (MPG)", x = "Miles Per Gallon", y = "Density" ) + theme_minimal() # Create Bar Plot cylinder_counts <- table(mtcars$cyl) # # Create a dbar plot using base R barplot(cylinder_counts, main = "Car Distribution by Number of Cylinders", xlab = "Number of Cylinders", ylab = "Count of Cars", col = "steelblue", border = "white") # Load the ggplot2 library library(ggplot2) # Create Grid of Scatter Plots ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(color = "tomato", size = 2) + # Create a separate plot for each cylinder category facet_wrap(~ cyl, labeller = labeller(cyl = c(`4` = "4 Cylinders", `6` = "6 Cylinders", `8` = "8 Cylinders") )) + labs( title = "MPG vs. Weight, by Number of Cylinders", x = "Weight (1000 lbs)", y = "Miles Per Gallon (MPG)" ) + theme_light()



Comments

Popular posts from this blog

LIS 4317 Final Project – Diamonds Analysis

Module 5. Part-to-Whole and Ranking Analysis