Package grafik dasar di R memiliki kelemahan yaitu tidak bisa disimpan dalam object.

Fungsi plot()

x <- 1:40
y <- rnorm(40, 5, 1)

# data baru
x1 <- 41:50
y1 <- rnorm(10, 5, 1)

# membuat scatter plot dengan data awal
plot(x, y, type="p",
     xlab="Sumbu x",
     ylab="Sumbu y",
     main="Grafik Bilangan Acak Normal",
     col=topo.colors(50),
     pch=16, cex=2, xlim=c(0,50), ylim=c(2.5, 7.5))

# plot data baru
points(x1, y1, cex=2)

# menambah garis
x2 <- rep(40.5, 20)
y2 <- seq(min(c(y,y1)), max(c(y,y1)), length=20)

# membuat garis (v vertikal, h horizontal)
abline(v=40.2, col="blue")
abline(h=mean(y), col="red", lwd=2.5)

abline(a=2, b=1/10, col="purple", lwd=2, lty=2)

#menambahkan tanda panah, dengan argumen x dan y menyatakan koordinat
arrows(x0=30, y0=3.5, x1=40, y1=mean(y)-.1, lwd=2)

#menambahkan tulisan, dengan argumen x dan y menyatakan koordinat
text(x=29,y=3.3, labels="Titik potong", cex=0.7)
text(x=3,y=7.3, labels="Data awal", cex=0.7)
text(x=46,y=7.3, labels="Data baru", cex=0.7)

Keterangan: * type = mengatur tipe plot yang meliputi: 1. “p” for points, 2. “l” for lines, 3. “b” for both, 4. “c” for the lines part alone of “b”,
5. “o” for both ‘overplotted’, 6. “h” for ‘histogram’ like (or ‘high-density’) vertical lines, 7. “s” for stair steps, 8. “S” for other steps, see ‘Details’ below, 9. “n” for no plotting * main = untuk judul plot * col = untuk colors dari plot * pch = memodifikasi simbol dariplot (1-25 jenis simbol) * cex = mengatur ukuran objek plot * lwd = mengatur ketebalan garis * xlab, ylab = memberi label pada sumbu x dan y * xlim, ylim = mengatur batas sumbu x dan y * lty = line type untuk plot “l”, “o”, “b”, “s”, dengan nilai 0 sampai 6 (1 untuk garus tanpa putus-putus).

Plot Data mtcars

Menyiapkan data:

data("mtcars")
head(mtcars, 10)
##                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

Membuat Histogram

Histogram dari mpg

hist(mtcars$mpg, main="Histogram of Miles per Gallon of Mtcars Data", xlab = "mpg")
with(mtcars, hist(mpg, main="Histogram of Miles per Gallon of Mtcars Data", xlab = "mpg"))

Bar Plot

counts <- table(mtcars$cyl)
counts 
## 
##  4  6  8 
## 11  7 14
barplot(counts, col = c("#ffd166","#06d6a0"), #custom colors
        legend = rownames(counts), # menambahkan legenda
        cex.names=.8) # mengatur ukuran nama

Scatter Plot

x = seq(from = -2*pi, to = 2*pi, length.out = 100)
y = 2*sin(x)
plot(x,y)

plot(mtcars$hp, mtcars$mpg)

Line Plot

plot(x,y, type = "l")

plot(x,y, type = "h")

x <- 1:40
y <- rnorm(40,5,1)
plot(x,y,type="o")

Pie Chart

pie(table(iris$Species))

mytable <- table(iris$Species)
lbls <- paste(names(mytable), "\n", mytable, sep="")
pie(mytable, labels = lbls,
   main="Pie Chart of Species\n (with sample sizes)")

Box Plot

boxplot(iris$Sepal.Length)

boxplot(Sepal.Length ~ Species, data = iris, cex.axis=.8)

boxplot(Sepal.Length ~ Species, data = iris,
        col = RColorBrewer::brewer.pal(length(unique(iris$Species)), 'Set2'),
        cex.axis=.8)

Layout

mtcars[,c(1:5)]
##                      mpg cyl  disp  hp drat
## Mazda RX4           21.0   6 160.0 110 3.90
## Mazda RX4 Wag       21.0   6 160.0 110 3.90
## Datsun 710          22.8   4 108.0  93 3.85
## Hornet 4 Drive      21.4   6 258.0 110 3.08
## Hornet Sportabout   18.7   8 360.0 175 3.15
## Valiant             18.1   6 225.0 105 2.76
## Duster 360          14.3   8 360.0 245 3.21
## Merc 240D           24.4   4 146.7  62 3.69
## Merc 230            22.8   4 140.8  95 3.92
## Merc 280            19.2   6 167.6 123 3.92
## Merc 280C           17.8   6 167.6 123 3.92
## Merc 450SE          16.4   8 275.8 180 3.07
## Merc 450SL          17.3   8 275.8 180 3.07
## Merc 450SLC         15.2   8 275.8 180 3.07
## Cadillac Fleetwood  10.4   8 472.0 205 2.93
## Lincoln Continental 10.4   8 460.0 215 3.00
## Chrysler Imperial   14.7   8 440.0 230 3.23
## Fiat 128            32.4   4  78.7  66 4.08
## Honda Civic         30.4   4  75.7  52 4.93
## Toyota Corolla      33.9   4  71.1  65 4.22
## Toyota Corona       21.5   4 120.1  97 3.70
## Dodge Challenger    15.5   8 318.0 150 2.76
## AMC Javelin         15.2   8 304.0 150 3.15
## Camaro Z28          13.3   8 350.0 245 3.73
## Pontiac Firebird    19.2   8 400.0 175 3.08
## Fiat X1-9           27.3   4  79.0  66 4.08
## Porsche 914-2       26.0   4 120.3  91 4.43
## Lotus Europa        30.4   4  95.1 113 3.77
## Ford Pantera L      15.8   8 351.0 264 4.22
## Ferrari Dino        19.7   6 145.0 175 3.62
## Maserati Bora       15.0   8 301.0 335 3.54
## Volvo 142E          21.4   4 121.0 109 4.11
pairs(mtcars[,c(1:5)])

a1 <- 1:25
a2 <- rnorm(25,4,2)

x <- seq(0,10,0.1)
sin <- sin(x)

par(mfrow=c(2,2)) # membuat layout dan diisi urut baris ke bawah krn mfrow

plot(1:40,y,type="p",xlab="Sumbu x",ylab="Sumbu y",
     main="Bilangan Acak Normal",col=2,pch=16)

y=plot(x, sin, type="l")

plot(table(rpois(100,5)),type="h",col="red",
     lwd=1,main="rpois(100,lambda=5)")

plot(a1,a2,type="n", main="W")

text(a1,a2,labels=paste("w",1:25,sep=""),
     col=rainbow(25),cex=0.8)

yb <- rnorm(100,5,1)
xb <- 1:100

par(mfrow=c(2,2))

plot(xb, yb, pch=16, col=rainbow(100))
title("Scatter Plot Bilangan Acak Normal",cex.main=0.7)

x <- yb
hist(yb, freq=FALSE, main=NULL, ylim=c(0,0.5))
curve(dnorm(x,5,1), col="red", lty=2, lwd=2, add=TRUE)

title("Histogram Bilangan Acak Normal", cex.main=0.7)
boxplot(yb)

title("Boxplot Bilangan Acak Normal",cex.main=0.7)
plot(xb, yb, type="l",lwd=2,col="blue")

title("Line Plot Bilangan Acak Normal",cex.main=0.7)

Package ggplot2

Package ini mengasumsikan data yang diplotkan sudah berbentuk data frame dan plot yang terbentuk bisa disimpan dalam sebuah object.

library(ggplot2)

Histogram

hist1 <- ggplot(data.frame(value=rnorm(100)), aes(x=value)) + 
  geom_histogram(binwidth = 0.25)
hist1

Scatter Plot

# adanya color langsung membentuk legenda, sehingga kalau bisa factor tapi bisa numeric
ggplot(iris, aes(x=Species, y=Sepal.Width, color=Sepal.Length)) + 
    geom_point(size=3)

Perhatikan letak penempatan argumen color:

# Di dalam aes
scatter1 <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + 
    geom_point(size=3)
scatter1

# Di luar aes
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
    geom_point(size=3, color="red")

# Jika aes color diganti size
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, size=Species)) + 
    geom_point(color="red") # size yang di geom point harus dihapus karena ia yang diutamakan untuk dieksekusi
## Warning: Using size for a discrete variable is not advised.

Bar Plot

ggplot(mtcars, aes(x=as.factor(cyl), fill=as.factor(cyl) )) + 
  geom_bar( ) +
  theme(legend.position="none")

Pie Chart

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
# Create Data
data <- data.frame(
  group=LETTERS[1:5],
  value=c(13,7,9,21,2)
)

# Compute the position of labels
data <- data %>% 
  arrange(desc(group)) %>%
  mutate(prop = value / sum(data$value) *100) %>%
  mutate(ypos = cumsum(prop)- 0.5*prop )

# Basic piechart
ggplot(data, aes(x="", y=prop, fill=group)) +
  geom_bar(stat="identity", width=1, color="white") +
  coord_polar("y", start=0) +
  theme_void() + 
  theme(legend.position="none") +
  
  geom_text(aes(y = ypos, label = group), color = "white", size=6) +
  scale_fill_brewer(palette="Set1")

Line Plot

ggplot(data.frame(xValue=1:10,yValue=cumsum(rnorm(10))), aes(x=xValue, y=yValue)) +
  geom_line()

Package plotly

Memungkinkan plot lebih interaktif.

#install.packages("plotly")
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
fig <- plot_ly(z = volcano, type = "heatmap")
fig
plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

Saving Data to RDS Format

Untuk mengompress dan membuat beban program berkurang karena ukurannya lebih kecil

saveRDS(mtcars, "mtcars.rds")
# loading data 
mtcars2 <- readRDS("mtcars.rds")
head(mtcars2)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Keterangan: Minimal object yang disimpan dalam rds sebaiknya adalah vector.