Latihan Visualisasi Data - Sains Data

Author

Rosita Ria Rusesta

Latihan Visualisasi Data GGPLOT2

library(ggplot2)

ggplot(mtcars, aes(x = mpg, fill = factor(cyl))) +
  geom_histogram(binwidth = 2, color = "white", alpha = 0.8) +
  facet_wrap(~cyl, scales = "free_y") +
  labs(
    title = "DISTIRBUSI MPG PER JUMLAH SILINDER",
    subtitle = "Distribusi MPG per Jumlah Silinder (mtcars)",
    x = "MPG",
    y = "Count",
    fill = "Cyl"
  ) +
  theme(
    plot.title = element_text(hjust = 0.5, size = 20, face = "bold"),     # judul tengah, besar, bold
    plot.subtitle = element_text(hjust = 0, size = 8, face = "plain"), # subtitle tengah
    axis.title.x = element_text(size = 14, face = "bold"),
    axis.title.y = element_text(size = 14, face = "bold"),
    legend.title = element_text(size = 12, face = "bold"),
    legend.text = element_text(size = 11)
  )

library(ggplot2)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
  geom_point(size = 1) +
  labs(
    title = "SEPAL LENGTH VS SEPAL WIDTH",
    subtitle = "Sepal Length vs Sepal Width (iris)",
    x = "Sepal Length",
    y = "Sepal Width",
    caption = "Sumber data: iris"
  ) +
  theme(
    plot.title = element_text(hjust = 0.5, size = 20, face = "bold"),      # judul tengah, besar, bold
    plot.subtitle = element_text(hjust = 0.5, size = 14, face = "plain"),  # subtitle tengah
    plot.caption = element_text(hjust = 1, size = 10, face = "italic"),    # caption kanan bawah
    axis.title.x = element_text(size = 14, face = "bold"),                 # label X tebal
    axis.title.y = element_text(size = 14, face = "bold"),                 # label Y tebal
    legend.title = element_text(size = 12, face = "bold"),                 # judul legenda
    legend.text = element_text(size = 11)                                  # teks legenda
  )

library(ggplot2)

ggplot(mtcars, aes(x = hp, y = mpg, color = factor(cyl), shape = factor(cyl))) +
  geom_point(size = 3, alpha = 0.8) +   # alpha < 1 bikin warna lebih lembut
  labs(
    title = "Perbandingan Efisiensi Bahan Bakar\nHubungan antara Horsepower dan MPG berdasarkan jumlah silinder",
    x = "Horsepower (hp)",
    y = "Miles per Gallon (mpg)",
    color = "Cylinders",
    shape = "Cylinders"
  ) +
  scale_color_brewer(palette = "Pastel1") +   # palet pastel lembut
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 10, face = "bold"),
    plot.subtitle = element_text(hjust = 0.5, size = 12, face = "italic"),
    axis.title.x = element_text(size = 14, face = "bold"),
    axis.title.y = element_text(size = 14, face = "bold"),
    legend.position = "top",
    legend.title = element_text(size = 12, face = "bold"),
    legend.text = element_text(size = 11)
  )

library(ggplot2)

ggplot(mpg, aes(x = displ, y = cty, size = cyl, color = class)) +
  geom_point(alpha = 0.7) +   # alpha buat warna lebih soft
  facet_wrap(~drv) +
  labs(
    title = "City MPG vs Displacement, difaset per Drive (mpg)",
    x = "Engine Displacement (L)",
    y = "City MPG",
    size = "Cyl",
    color = "Class"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
    axis.title.x = element_text(size = 13, face = "bold"),
    axis.title.y = element_text(size = 13, face = "bold"),
    legend.title = element_text(size = 12, face = "bold"),
    legend.text = element_text(size = 10)
  )

# install.packages("ggridges") # kalau belum terpasang
library(ggplot2)
library(ggridges)

ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
  geom_density_ridges(alpha = 0.7) +
  labs(
    title = "Ridgeline Plot Distribusi Harga per Cut (diamonds)",
    x = "Price",
    y = "Cut"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
    axis.title.x = element_text(size = 13, face = "bold"),
    axis.title.y = element_text(size = 13, face = "bold"),
    legend.title = element_text(size = 12, face = "bold"),
    legend.text = element_text(size = 10)
  )
Picking joint bandwidth of 458

library(ggplot2)

ggplot(diamonds, aes(x = price, fill = cut)) +
  geom_density(alpha = 0.4) +  # alpha membuat warna lebih soft/transparan
  labs(
    title = "Distribusi Harga Berlian berdasarkan Cut",
    subtitle = "Data diamonds dari ggplot2",
    x = "Price",
    y = "Density"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
    plot.subtitle = element_text(hjust = 0.5, size = 12, face = "italic"),
    axis.title.x = element_text(size = 13, face = "bold"),
    axis.title.y = element_text(size = 13, face = "bold"),
    legend.position = "top",
    legend.title = element_text(size = 12, face = "bold"),
    legend.text = element_text(size = 10)
  ) +
  scale_fill_brewer(palette = "Pastel1")  # palet warna pastel

library(ggplot2)

ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
  geom_boxplot(alpha = 0.6, color = "black") +  # boxplot dengan warna soft
  geom_jitter(width = 0.2, alpha = 0.7, color = "black", size = 1.5) +  # titik sebaran
  labs(
    title = "Sebaran Sepal Length per Species (iris)",
    x = "",
    y = "Sepal Length"
  ) +
  scale_fill_brewer(palette = "Pastel1") +  # warna pastel lembut
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
    axis.title.y = element_text(size = 12, face = "bold"),
    axis.text.x = element_text(size = 11, face = "bold")
  )

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(treemapify)

# Data agregasi jumlah berlian per cut × color
diamonds_count <- diamonds %>%
  count(cut, color)

# Plot treemap
ggplot(diamonds_count, aes(area = n, fill = cut, label = color)) +
  geom_treemap() +
  geom_treemap_text(color = "white", place = "centre", grow = TRUE) +
  labs(
    title = "Treemap Distribusi Cut × Color (diamonds)"
  ) +
  scale_fill_brewer(palette = "Spectral") + # palet warna soft
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
    legend.title = element_blank()
  )

library(ggplot2)

# Data bawaan ggplot2
data("economics")

# Hitung rata-rata pengangguran
mean_unemp <- mean(economics$unemploy)

# Plot
ggplot(economics, aes(x = date, y = unemploy)) +
  geom_line(color = "steelblue", size = 1) +
  geom_hline(yintercept = mean_unemp, linetype = "dashed", color = "red") +
  annotate("text", x = as.Date("2010-01-01"), y = mean_unemp + 300, 
           label = "Rata-rata pengangguran", color = "red", hjust = 0.6,size=3) +
  labs(
    title = "Perkembangan Pengangguran di AS",
    subtitle = "Data time series dari dataset economics (ggplot2)",
    x = "",
    y = "Unemployed (thousands)"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
    plot.subtitle = element_text(hjust = 0.5, size = 11)
  )
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

library(ggplot2)
library(dplyr)
library(networkD3)

# Data agregasi
diamonds_count <- diamonds %>%
  count(cut, color)

# Buat nodes
nodes <- data.frame(name = c(as.character(unique(diamonds_count$cut)),
                             as.character(unique(diamonds_count$color))))

# Buat links
links <- diamonds_count %>%
  mutate(
    IDsource = match(cut, nodes$name) - 1,
    IDtarget = match(color, nodes$name) - 1
  ) %>%
  select(IDsource, IDtarget, n)

# Plot Sankey Diagram
sankeyNetwork(Links = links, Nodes = nodes,
              Source = "IDsource", Target = "IDtarget",
              Value = "n", NodeID = "name",
              fontSize = 12, nodeWidth = 30)
Links is a tbl_df. Converting to a plain data frame.