Tugas Praktikum Visualization Data

Author

Wira Adiguna Sutawa M0501261029

Soal 1 : Comparison mpg

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

Penyelesaian

1. Panggil data mpg

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
glimpse(mpg)
Rows: 234
Columns: 11
$ manufacturer <chr> "audi", "audi", "audi", "audi", "audi", "audi", "audi", "…
$ model        <chr> "a4", "a4", "a4", "a4", "a4", "a4", "a4", "a4 quattro", "…
$ displ        <dbl> 1.8, 1.8, 2.0, 2.0, 2.8, 2.8, 3.1, 1.8, 1.8, 2.0, 2.0, 2.…
$ year         <int> 1999, 1999, 2008, 2008, 1999, 1999, 2008, 1999, 1999, 200…
$ cyl          <int> 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8, …
$ trans        <chr> "auto(l5)", "manual(m5)", "manual(m6)", "auto(av)", "auto…
$ drv          <chr> "f", "f", "f", "f", "f", "f", "f", "4", "4", "4", "4", "4…
$ cty          <int> 18, 21, 20, 21, 16, 18, 18, 18, 16, 20, 19, 15, 17, 17, 1…
$ hwy          <int> 29, 29, 31, 30, 26, 26, 27, 26, 25, 28, 27, 25, 25, 25, 2…
$ fl           <chr> "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p…
$ class        <chr> "compact", "compact", "compact", "compact", "compact", "c…

You can add options to executable code like this

top10_mpg <- mpg %>%
  group_by(manufacturer) %>% summarise(rata_cty = mean(cty, na.rm = TRUE) ) %>% arrange(desc(rata_cty)) %>% slice_head(n = 10)
top10_mpg

The echo: false option disables the printing of code (only output is displayed).

ggplot(top10_mpg, aes(x = rata_cty,y = manufacturer)) +
  geom_point()

Soal 2 : Distribution - Diamonds

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

Penyelesaian

ggplot(mpg, aes(x = class, y = cty)) +
  geom_point() +
  labs(
    title = "Distribution of City MPG Across Vehicle Classes",
    x = "Vehicle Class",
    y = "City MPG"
  ) +
  theme_minimal()

Soal 3 : Relationhip - Diamond_sample

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

Penyelesaian

diamonds
ggplot(diamonds,
       aes(x = carat, y = price, color = cut)) +
  geom_point(alpha = 0.6, size = 2) +
  labs(
    title = "Relationship Between Carat and Price",
    subtitle = "Colored by Diamond Cut",
    x = "Carat",
    y = "Price",
    color = "Cut"
  )

Soal 4 : Time series economics

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

Penyelesaian

ggplot(data = economics,       
       aes(x = date, y = psavert)) +  
  geom_line() +  
  labs( title = "Distribusi Harga Berlian",
        x = "Year",    
        y = "Personal Saving Rate"  )+  
  theme_minimal()

Soal 5 : Improve a Visualization

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

ggplot(mpg, aes(
    x = reorder(class, cty, FUN = median),
    y = cty,
    fill = class)) +
  geom_boxplot(alpha = 0.7, outlier.shape = 12) +
  geom_jitter(width = 0.15,alpha = 0.35,
    size = 1.5
  ) + labs(
    title = "Distribution of City MPG Across Vehicle Classes",
    subtitle = "Vehicle classes are ordered by median city fuel efficiency",
    x = "Vehicle Class",
    y = "City MPG") +
  theme_minimal(base_size = 12) +
  theme(
    legend.position = "none",
    plot.title = element_text(
      face = "bold",
      size = 14),
    axis.text.x = element_text(
      angle = 30,
      hjust = 1),
    panel.grid.minor = element_blank())