Exercise 1 (Comparison - mpg).

Calculate the average cty for each manufacturer, select the top 10, create a comparison plot, and summarize the main finding.

Answer.

library(tidyverse)
## ── 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
library(dplyr)
library(ggplot2)

data.mpg <- mpg %>%
  select(manufacturer,cty) %>%
  group_by(manufacturer) %>%
  summarise(average.cty = mean(cty))

top.10.mpg <- sort_by(data.mpg,data.mpg$average.cty,decreasing = TRUE)
top.10.mpg <- top.10.mpg[1:10,]
top.10.mpg
## # A tibble: 10 × 2
##    manufacturer average.cty
##    <chr>              <dbl>
##  1 honda               24.4
##  2 volkswagen          20.9
##  3 subaru              19.3
##  4 hyundai             18.6
##  5 toyota              18.5
##  6 nissan              18.1
##  7 audi                17.6
##  8 pontiac             17  
##  9 chevrolet           15  
## 10 ford                14
ggplot(data = top.10.mpg,
       aes(x = reorder(manufacturer,average.cty),
           y = average.cty)) + 
  geom_col(fill = "green") + 
  coord_flip() + 
  labs(
    title = 
    "     Average of Fuel Efficiency 
     According to Vehicle Manufacturer",
    x = NULL,
    y = 
    "Average of Fuel Efficiency When Driving Inside A City"
  ) + 
  theme_minimal()

Based on the data visualization, we can conclude that there are three vehicle manufactures which have highest average of fuel efficiency (when driving inside a city), those are Honda (1st), Volkswagen (2nd), and Subaru (3rd). Honda’s fuel efficiency is almost 25 miles/gallon, Volkswagen’s fuel efficiency is between 20 until 22,5 miles/gallon, meanwhile Subaru’s fuel efficiency is almost 20 miles/gallon. What does it mean? It means that when we choose to drive with Honda’s vehicle in city’s streets, then we can do journey as far almost 25 miles (≈ 40,2 km) for 1 gallon of fuel, as far 20 - 22,5 miles ( ≈ 32,2 km - 36,2 km) for 1 gallon of fuel if we Choose Volkswagen’s vehicle, and as far almost 20 miles (≈ 32,2 km) for 1 gallon of fuel if we Choose Subaru’s vehicle.

Exercise 2 (Distribution - diamonds).

Choose one numerical variable, compare its distribution across one categorical variable, improve the plot appearance, and interpret the pattern.

class(diamonds$carat)
## [1] "numeric"
class(diamonds$cut)
## [1] "ordered" "factor"
unique(diamonds$cut)
## [1] Ideal     Premium   Good      Very Good Fair     
## Levels: Fair < Good < Very Good < Premium < Ideal
fair.cut <- diamonds %>%
  select(cut,carat) %>%
  filter(cut == "Fair") %>%
  mutate(Carat = sort(carat))

fair.cut <- fair.cut %>%
  select(Carat) %>%
  group_by(Carat) %>%
  summarise(Frequency = n())

good.cut <- diamonds %>%
  select(cut,carat) %>%
  filter(cut == "Good") %>%
  mutate(Carat = sort(carat))

good.cut <- good.cut %>%
  select(Carat) %>%
  group_by(Carat) %>%
  summarise(Frequency = n())

ver.good.cut <- diamonds %>%
  select(cut,carat) %>%
  filter(cut == "Very Good") %>%
  mutate(Carat = sort(carat))

ver.good.cut <- ver.good.cut %>%
  select(Carat) %>%
  group_by(Carat) %>%
  summarise(Frequency = n())

premium.cut <- diamonds %>%
  select(cut,carat) %>%
  filter(cut == "Premium") %>%
  mutate(Carat = sort(carat))

premium.cut <- premium.cut %>%
  select(Carat) %>%
  group_by(Carat) %>%
  summarise(Frequency = n())

ideal.cut <- diamonds %>%
  select(cut,carat) %>%
  filter(cut == "Ideal") %>%
  mutate(Carat = sort(carat))

ideal.cut <- ideal.cut %>%
  select(Carat) %>%
  group_by(Carat) %>%
  summarise(Frequency = n())
# Fair Cut
ggplot(data = fair.cut, aes(x = Carat)) +
geom_histogram(
  fill = "green",
  color = "black"
) + 
labs(
  title = 
"Distribution of Carat From 
Fair Cut Diamond",
x = "Carat",
y = "Frequency of Carat Variation"
) 
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# Good Cut
ggplot(data = good.cut, aes(x = Carat)) +
  geom_histogram(
    fill = "deepskyblue",
    color ="black"
) + 
labs(
  title = 
"Distribution of Carat From 
Good Cut Diamond",
x = "Carat",
y = "Frequency of Carat Variation"
) 
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# Very Good Cut
ggplot(data = ver.good.cut, aes(x = Carat)) +
  geom_histogram(
    fill = "mediumpurple1",
    color = "black"
) + 
labs(
  title = 
"Distribution of Carat From
Very Good Cut Diamond",
x = "Carat",
y = "Frequency of Carat Variation"
) 
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# Premium Cut
ggplot(data = premium.cut, aes(x = Carat)) + 
  geom_histogram(
    fill = "hotpink",
    color = "black"
) + 
labs(
  title = 
"Distribution of Carat From 
Premium Cut Diamond",
x = "Carat",
y = "Frequency of Carat Variation"
) 
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# Ideal Cut
ggplot(data = ideal.cut, aes(x = Carat)) +
  geom_histogram(
    fill = "red",
    color = "black"
) + 
labs(
  title = 
"Distribution of Carat From
Ideal Cut Diamond",
x = "Carat",
y = "Frequency of Carat Variation"
) 
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

Based on data visualization, we can conclude these some findings. Statistically,

Therefore, we can conclude that diamonds which are produced tend to posses weight about 0,25 until 2,5 carats (≈ 0,05 - 0,5 grams).

Exercise 3 (Relationship-diamonds_sample).

Visualize the relationship between carat and price, add at least one relevant aesthetic, apply suitable customization, and explain the relationship shown.

Answer.

diamonds_sample <- diamonds %>%
  select(carat,price) 

ggplot(diamonds_sample,
       aes(x = carat, y = price)) + 
  geom_point(colour  = "chocolate") +
  labs(
    title = "Relationship of Carat and Diamonds Price",
  x = "Carat",
  y = "Diamonds Price")

Based on the data visualization above, we can conclude that generally, when carat of diamonds increase then also the diamonds price tend to increase. From that visualization, we also can conclude that :

Exercise 4 (Time Series-economics).

Visualize psavert over time, use clear labels and a suitable theme, highlight or annotate a noticeable change, and provide ia short interpretation.

Answer.

ggplot(data = economics,
       aes(x = date,
           y = psavert)) + 
  geom_line(colour = "blue") +
  labs(title = "Trend of Personal Savings Rate",
       x = "Date",
       y = "Personal Savings Rate")

From the visualization above we can know that maximum personal savings rate was reached in year between 1975 and 1980, and the minimum personal savings rate was reached in year between 2005 and 2010. Overall, from the visualization we can know that personal savings rate tend to decrease although with a bit of fluctuation (from 1970s until early 2000s,) but has increased since year almost 2010.

Exercise 5 (Improve a Visualization).

Create one visualization with at least three presentation problems, then redesign it using improvement such as color, theme, scale, labels, legend or annotation, and briefly explain the change.

Answer.

# Example of a visualization that needs to be improved.

food <- c("Matcha","Burger","Seblak","Dimsum","Chocolate")
lovers <- c(80,70,60,80,90)
fav.food <- data.frame(food,lovers)

bp <- barplot(height = fav.food$lovers,
        names.arg = fav.food$food,
        ylim = c(0,max(lovers)*1.5),
        main = "Favorite Food of Student",
        xlab = "Food",
        ylab = "Frequency of Lovers",
        col = rainbow(length(fav.food$food)))

x.4 <- c(seq(0.2,6,length = length(fav.food$food))) 


y.4 <- c(fav.food$lovers) 

data.approx <- approxfun(x.4,y.4)
curve(data.approx, add = TRUE, lty = 1, lwd = 3)

From this visualization, there are some things that we can pay attention:

Based on the explanation above, then here is the visualizations that has been adjusted with suggested changes.

fav.food <- fav.food %>%
  arrange(desc(lovers))

bp <- barplot(height = fav.food$lovers,
              names.arg = fav.food$food,
              ylim = c(0,max(lovers)*2),
              main = "     Favorite Food of Student",
              xlab = "Food",
              ylab = "Frequency of Lovers",
              col = "yellow")
text(x = bp, y = fav.food$lovers,labels = fav.food$lovers,
     pos = 1, font = 3, col = "black")