library(graphics)

1. Generate data

y <- rnorm(20)

# Reset perangkat grafik (untuk mengatasi error 'margins too large')
if(!is.null(dev.list())) dev.off()
## null device 
##           1
# Plot data awal
plot(y, type = "p", main = "Plot rnorm", col = "blue", pch = 16)

Poin 2: Visualisasi Statistik Dasar (1-Dimensi)

# Menyiapkan data
data_silinder <- table(mtcars$cyl)
data_mpg <- mtcars$mpg

# Plotting
plot(data_silinder, main="Barplot Silinder (1-D Table)", col="tomato", ylab="Frekuensi")

pie(data_silinder, main="Pie Chart Silinder", col=rainbow(3))

dotchart(data_mpg, labels=row.names(mtcars), 
         main="Dotplot Konsumsi Bensin", pch=21, bg="green")

stripchart(data_mpg, method = "jitter", pch = 19, col = "blue",
           main = "1-D Scatterplot MPG", xlab = "Miles Per Gallon")

sunflowerplot(data_mpg)

Poin 3: Analisis Data Kategorik (2-Dimensi)

Visualisasi khusus untuk membandingkan kategori (contoh: Gender vs Hobi).

data_hobi <- matrix(c(20,10,5,25), nrow= 2, byrow = TRUE)
colnames(data_hobi) <- c("Olahraga", "Membaca")
rownames(data_hobi) <- c("Pria", "Wanita")

# Ubah jadi format table
tabel_2d <- as.table(data_hobi)

# Macam-macam plot kategorik
fourfoldplot(tabel_2d, color = c("#99CCFF", "#6699FF"),
             main = "Fourfold Display: Gender vs Hobi")

assocplot(tabel_2d, main = "Association Plot: Gender vs Hobi", 
          col = c("green", "red"))

mosaicplot(tabel_2d, main = "Mosaic Plot: Gender vs Hobi", 
           color = TRUE, shade = TRUE)

Poin 4: Boxplot dan Garis Kustom

Bagian ini menunjukkan cara menggabungkan beberapa elemen grafis (seperti garis dan legenda) dalam satu plot.

# Boxplot dengan skala logaritmik
boxplot(decrease ~ treatment, data=OrchardSprays, log="y", col="light gray", boxwex=0.5)

# Barplot Horizontal
barplot(VADeaths[1:2,], angle=c(45, 135), density=20, col="gray", names=c("RM","RF","UM","UF"),
        horiz=TRUE)

# Plot x-y dengan garis tambahan dan legenda
x <- 1:10
y1 <- x*x
y2 <- 2*y1

plot(x, y1, type="b", pch=19, col="red", xlab="x", ylab="y") 
lines(x, y2, pch=18, col="blue", type="b", lty=2) 
legend("topleft", legend=c("Line 1", "Line 2"),
       col=c("red","blue"), lty=1:2, lwd = 2, cex=0.9)

Poin 5: Visualisasi Lanjutan (Lattice & GGPlot2)

Bagian ini menggunakan library pihak ketiga untuk grafik yang lebih kompleks dan estetis.

library(lattice)
library(ggplot2)

# Contoh Lattice dengan kustomisasi panel
xyplot(mpg ~ disp | factor(gear), data = mtcars, layout = c(3, 1), aspect = 1,
       panel = function(x, y, ...) {
         panel.xyplot(x, y, ...)
         panel.abline(h = 29, lty = "dashed", col = "black")
         panel.text(x = 470, y = 29.5, labels = "efficiency criterion", adj = c(1, 0), cex = 0.7)
       })

# Contoh GGPlot2 - Scatter plot dengan warna berdasarkan kelas
ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy, colour = class)) +
  labs(title = "Hubungan Displacement dan MPG (GGPlot2)",
       x = "Engine Displacement (Liter)",
       y = "Highway MPG") +
  theme_minimal()

# Contoh GGPlot2 - Bar Chart Berdampingan (Dodge)
ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")