library(GGally)
## Loading required package: ggplot2
library(ggplot2)

data(iris)

ggparcoord(
  iris,
  columns = 1:4,
  groupColumn = 5,
  scale = "uniminmax",
  alphaLines = 0.5
) +
  labs(
    title = "Parallel Coordinates of Iris Species",
    x = "measurements",
    y = "values",
    color = "Species"
  ) +
  theme_minimal()

This plot is comparing the flower measurments across different iris species. Each line is one flow observation, so having multiple of them in one graph makes it easier to compare. The plot is easy because it helps visualize multivariable relations.

library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(maps)

crime <- USArrests
crime$region <- tolower(rownames(crime))

states <- map_data("state")

data <- left_join(states, crime, by = "region")

ggplot(data, aes(long, lat, group = group, fill = Murder)) +
  geom_polygon(color = "white") +
  labs(
    title = "Murder Rates by State",
    fill = "Murder Rate"
  ) +
  theme_void()

The map displays the murder rate across the U.S. If a state has a darker color it shows higher murder rates. This is a good visual because it allows you to quickly gather and understand the different rates of murder.

library(ggplot2)
library(ggalluvial)

df <- as.data.frame(Titanic)

ggplot(df,
       aes(axis1 = Class,
           axis2 = Sex,
           axis3 = Survived,
           y = Freq)) +

  geom_alluvium(aes(fill = Survived)) +
  geom_stratum() +

  labs(
    title = "Titanic",
    fill = "Survived"
  )

This diagram shows how titanic passengers are connected across different categories. The wider the flow the more passengers are in each category. This is a good visual because it helps you clearly view an estimate for different groups.

library(ggplot2)
library(ggdist)

ggplot(ToothGrowth,
       aes(supp, len, fill = supp)) +

  stat_halfeye() +
  geom_boxplot(width = 0.1) +
  geom_jitter(width = 0.1) +

  labs(
    title = "Tooth Length by Supplement",
    x = "Supplement",
    y = "Tooth Length"
  ) +

  theme_minimal()

The raincloud plot compares tooth length and supplement group in the dataset to show the distribution of the data. It makes it easier to compare the difference between supplement types by showing you different plots like density, boxplot and jitter.

Portfolio

Central Theme: How do cars differ in fuel efficiency? Question

What is the spread of miles per gallon (MPG) look like?

Plot Type

Histogram

library(ggplot2)

ggplot(mtcars, aes(x = mpg)) +
  geom_histogram(binwidth = 2, fill = "steelblue", color = "white") +
  labs(
    title = "Distribution of Fuel Efficiency",
    x = "Miles Per Gallon",
    y = "Number of Cars"
  ) +
  theme_minimal()

Most cars have a full efficiency that lies around the 15-20 miles per gallon.

Do cars with more cylinders have worse fuel efficiency?

Boxplot

ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
  geom_boxplot(fill = "orange") +
  labs(
    title = "Fuel Efficiency by Cylinder Count",
    x = "Number of Cylinders",
    y = "Miles Per Gallon"
  ) +
  theme_minimal()

Cars with 4 cylinders have a much higher value of Miles per Gallon.

Does heavier weight reduce fuel efficiency?

Scatter plot

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(color = "darkblue", size = 3) +
  labs(
    title = "Weight vs Fuel Efficiency",
    x = "Car Weight (1000 lbs)",
    y = "Miles Per Gallon"
  ) +
  theme_minimal()

As the car weight increases the value of miles per gallons decreases as well

Do manual or automatic cars have better fuel efficiency?

Bar plot

ggplot(mtcars, aes(x = factor(am), y = mpg)) +
  stat_summary(fun = mean, geom = "bar", fill = "green") +
  labs(
    title = "Average MPG by Transmission",
    x = "Transmission (0 : auto, 1: manual",
    y = "Average Miles Per Gallon"
  ) +
  theme_minimal()

The manual cars seem to have a much higher average miles per gallon value

How does fuel efficiency change across different numbers of cylinders?

Density plot

ggplot(mtcars, aes(x = mpg, fill = factor(cyl))) +
  geom_density(alpha = 0.5) +
  labs(
    title = "Fuel Efficiency by Cylinder Count",
    x = "Miles Per Gallon",
    fill = "Cylinders"
  ) +
  theme_minimal()

4-cyl has higher mpg value compared to the 8-cyl however the 8-cyl had a much greater density.