This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the plot.
### Gambar 3.23–3.24
data3_2 <- data.frame(
tahun = 2001:2007,
barang_A = c(90, 110, 115, 130, 140, 150, 155),
barang_B = c(85, 90, 105, 110, 120, 125, 130),
barang_C = c(50, 55, 60, 65, 70, 80, 85)
)
write.csv(
data3_2,
"data3.2.csv",
row.names = FALSE
)
data3_2
## tahun barang_A barang_B barang_C
## 1 2001 90 85 50
## 2 2002 110 90 55
## 3 2003 115 105 60
## 4 2004 130 110 65
## 5 2005 140 120 70
## 6 2006 150 125 80
## 7 2007 155 130 85
tahun <- data3_2$tahun
jumlah_A <- data3_2$barang_A
plot(
tahun,
jumlah_A,
type = "o",
main = "Penjualan Barang A Tahun 2001-2007",
xlab = "Tahun",
ylab = "Jumlah",
ylim = c(40, 180)
)
### Gambar 3.26
plot(
tahun,
jumlah_A,
type = "o",
col = "red",
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah"
)
lines(
tahun,
data3_2$barang_B,
type = "o",
col = "blue"
)
### Gambar 3.27
plot(
tahun,
jumlah_A,
type = "o",
col = "red",
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah"
)
lines(
tahun,
data3_2$barang_B,
type = "o",
col = "blue"
)
lines(
tahun,
data3_2$barang_C,
type = "o",
col = "green"
)
### Gambar 3.28
plot(
tahun,
jumlah_A,
type = "b",
pch = 22,
lty = 1,
col = "red",
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah"
)
lines(
tahun,
data3_2$barang_B,
type = "b",
pch = 21,
lty = 2,
col = "blue"
)
lines(
tahun,
data3_2$barang_C,
type = "b",
pch = 23,
lty = 3,
col = "green"
)
### Gambar 3.29
plot(
tahun,
jumlah_A,
type = "o",
pch = 22,
lty = 1,
col = "red",
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah",
main = "Penjualan Barang A, B, dan C"
)
lines(
tahun,
data3_2$barang_B,
type = "o",
pch = 21,
lty = 2,
col = "blue"
)
lines(
tahun,
data3_2$barang_C,
type = "o",
pch = 23,
lty = 3,
col = "green"
)
### Gambar 3.30
simpan_garis <- read.csv("data3.2.csv")
simpan_garis
## tahun barang_A barang_B barang_C
## 1 2001 90 85 50
## 2 2002 110 90 55
## 3 2003 115 105 60
## 4 2004 130 110 65
## 5 2005 140 120 70
## 6 2006 150 125 80
## 7 2007 155 130 85
plot(
simpan_garis$tahun,
simpan_garis$barang_A,
main = "Penjualan Barang A",
xlab = "Tahun",
ylab = "Jumlah",
ylim = c(40, 180)
)
### Gambar 3.32
plot(
simpan_garis$tahun,
simpan_garis$barang_A,
type = "o",
col = "red",
xlab = "Tahun",
ylab = "Jumlah",
ylim = c(40, 180)
)
### Gambar 3.33
plot(
simpan_garis$tahun,
simpan_garis$barang_A,
type = "o",
col = "red",
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_B,
type = "o",
col = "blue"
)
### Gambar 3.34
plot(
simpan_garis$tahun,
simpan_garis$barang_A,
type = "o",
col = "red",
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_B,
type = "o",
col = "blue"
)
axis(2, at = seq(40, 180, by = 20))
### Gambar 3.35
plot(
simpan_garis$tahun,
simpan_garis$barang_A,
type = "o",
col = "red",
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_B,
type = "o",
col = "blue"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_C,
type = "o",
col = "green"
)
### Gambar 3.36
plot(
simpan_garis$tahun,
simpan_garis$barang_A,
type = "o",
col = "red",
lty = 2,
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_B,
type = "o",
col = "blue",
lty = 2
)
lines(
simpan_garis$tahun,
simpan_garis$barang_C,
type = "o",
col = "green",
lty = 2
)
### Gambar 3.37
plot(
simpan_garis$tahun,
simpan_garis$barang_A,
type = "o",
pch = 22,
col = "red",
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_B,
type = "o",
pch = 21,
col = "blue"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_C,
type = "o",
pch = 23,
col = "green"
)
### Gambar 3.38
plot(
simpan_garis$tahun,
simpan_garis$barang_A,
type = "p",
pch = 22,
col = "red",
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah"
)
points(
simpan_garis$tahun,
simpan_garis$barang_B,
pch = 21,
col = "blue"
)
points(
simpan_garis$tahun,
simpan_garis$barang_C,
pch = 23,
col = "green"
)
### Gambar 3.39
plot(
simpan_garis$tahun,
simpan_garis$barang_A,
type = "o",
pch = 22,
col = "red",
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_B,
type = "o",
pch = 21,
col = "blue"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_C,
type = "o",
pch = 23,
col = "green"
)
### Gambar 3.40
plot(
simpan_garis$tahun,
simpan_garis$barang_A,
type = "o",
col = "red",
ylim = c(40, 180),
xlab = "Tahun",
ylab = "Jumlah",
main = "Data Penjualan Barang A, B, C, dari Tahun 2001-2007",
font.main = 3,
col.main = "red"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_B,
type = "o",
col = "blue"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_C,
type = "o",
col = "green"
)
### Gambar 3.41
total <- simpan_garis$barang_A +
simpan_garis$barang_B +
simpan_garis$barang_C
plot(
simpan_garis$tahun,
simpan_garis$barang_A,
type = "o",
col = "red",
ylim = c(40, 400),
xlab = "Tahun",
ylab = "Jumlah",
main = "Penjualan Barang A, B, C, dari Tahun 2001-2007"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_B,
type = "o",
col = "blue"
)
lines(
simpan_garis$tahun,
simpan_garis$barang_C,
type = "o",
col = "green"
)
lines(
simpan_garis$tahun,
total,
type = "o",
col = "black"
)
legend(
"topleft",
legend = c("Barang A", "Barang B", "Barang C", "Total"),
col = c("red", "blue", "green", "black"),
lty = 1,
pch = 1,
cex = 0.7
)
### Gambar 3.42–3.43
data3_3 <- data.frame(
tahun = 2001:2007,
jenis_barang_A = c(90, 110, 115, 130, 140, 150, 160)
)
write.csv(
data3_3,
"data3.3.csv",
row.names = FALSE
)
data3_3
## tahun jenis_barang_A
## 1 2001 90
## 2 2002 110
## 3 2003 115
## 4 2004 130
## 5 2005 140
## 6 2006 150
## 7 2007 160
tahun <- data3_3$tahun
jumlah_A <- data3_3$jenis_barang_A
# Grafik batang sederhana
barplot(jumlah_A)
# Grafik batang dengan nama tahun
barplot(
jumlah_A,
names.arg = tahun
)
# Grafik batang dengan judul
barplot(
jumlah_A,
names.arg = tahun,
main = "Penjualan Barang A",
xlab = "Tahun",
ylab = "Jumlah"
)
# Grafik batang dengan warna
barplot(
jumlah_A,
names.arg = tahun,
col = "lightblue",
main = "Penjualan Barang A",
xlab = "Tahun",
ylab = "Jumlah"
)
# Grafik batang dengan garis tepi
barplot(
jumlah_A,
names.arg = tahun,
col = "lightblue",
border = "black",
main = "Penjualan Barang A",
xlab = "Tahun",
ylab = "Jumlah"
)
simpan_batang <- read.table(
"data3.3.csv",
header = TRUE,
sep = ","
)
simpan_batang
## tahun jenis_barang_A
## 1 2001 90
## 2 2002 110
## 3 2003 115
## 4 2004 130
## 5 2005 140
## 6 2006 150
## 7 2007 160
barplot(
simpan_batang$jenis_barang_A,
names.arg = simpan_batang$tahun,
main = "Penjualan Barang Jenis A dari Tahun 2001-2007",
xlab = "Tahun",
ylab = "Jumlah Barang Terjual",
col = "lightgray",
border = "black"
)
### Gambar 3.47
library(ggplot2)
ggplot(
data = simpan_batang,
aes(
x = factor(tahun),
y = jenis_barang_A,
fill = factor(tahun)
)
) +
geom_bar(stat = "identity") +
scale_fill_manual(
values = rainbow(7)
) +
labs(
title = "Penjualan Barang A dari Tahun 2001-2007",
x = "Tahun",
y = "Jumlah Barang A"
) +
theme_minimal() +
theme(legend.position = "none")
### Gambar 3.48–3.49
data3_4 <- data.frame(
jenis_kelamin = c(
"Laki-Laki",
"Laki-Laki",
"Perempuan",
"Perempuan"
),
hobi = c(
"Olahraga",
"Memasak",
"Olahraga",
"Memasak"
),
jumlah = c(90, 10, 25, 75)
)
write.csv(
data3_4,
"data3.4.csv",
row.names = FALSE
)
data3_4
## jenis_kelamin hobi jumlah
## 1 Laki-Laki Olahraga 90
## 2 Laki-Laki Memasak 10
## 3 Perempuan Olahraga 25
## 4 Perempuan Memasak 75
library(ggplot2)
ggplot(
data3_4,
aes(x = jenis_kelamin, y = jumlah, fill = jenis_kelamin)
) +
geom_bar(stat = "identity") +
labs(
title = "Jumlah Responden Berdasarkan Jenis Kelamin",
x = "Jenis Kelamin",
y = "Jumlah Responden"
) +
theme_minimal()
### Gambar 3.51
ggplot(
data3_4,
aes(
x = hobi,
y = jumlah,
fill = jenis_kelamin
)
) +
geom_bar(
stat = "identity",
position = "dodge"
) +
labs(
title = "Hubungan antara Jenis Kelamin dan Hobi",
x = "Hobi",
y = "Jumlah Responden",
fill = "Jenis Kelamin"
) +
theme_minimal()
### Gambar 3.52
simpan_hobi <- read.csv("data3.4.csv")
simpan_hobi
## jenis_kelamin hobi jumlah
## 1 Laki-Laki Olahraga 90
## 2 Laki-Laki Memasak 10
## 3 Perempuan Olahraga 25
## 4 Perempuan Memasak 75
frekuensi <- matrix(
c(90, 10, 25, 75),
nrow = 2,
byrow = TRUE
)
rownames(frekuensi) <- c("Laki-Laki", "Perempuan")
colnames(frekuensi) <- c("Olahraga", "Memasak")
barplot(
t(frekuensi),
beside = TRUE,
col = c("navy", "orange"),
names.arg = c("Laki-Laki", "Perempuan"),
main = "Hubungan antara Jenis Kelamin dan Hobi",
xlab = "Jenis Kelamin",
ylab = "Jumlah Responden",
legend.text = c("Olahraga", "Memasak"),
args.legend = list(x = "topright")
)
### Gambar 3.54
nilai <- data.frame(
kategori = c("A", "B", "C", "D"),
jumlah = c(10, 60, 80, 40)
)
barplot(
nilai$jumlah,
names.arg = nilai$kategori,
col = c("green", "yellow", "orange", "red"),
main = paste(
"Jumlah Mahasiswa yang Memperoleh Nilai A, B, C, dan D,",
"untuk Matakuliah Matematika 1"
),
xlab = "Nilai Mahasiswa",
ylab = "Jumlah Mahasiswa"
)
### Gambar 3.55
ggplot(
simpan_hobi,
aes(
x = jenis_kelamin,
y = jumlah,
fill = jenis_kelamin
)
) +
geom_bar(stat = "identity") +
labs(
title = "Jumlah Responden Berdasarkan Jenis Kelamin",
x = "Jenis Kelamin",
y = "Jumlah"
) +
theme_minimal()
### Gambar 3.56
data_hobi <- data.frame(
jenis_kelamin = factor(
c("Laki-Laki", "Laki-Laki", "Perempuan", "Perempuan"),
levels = c("Laki-Laki", "Perempuan")
),
hobi = factor(
c("Olahraga", "Memasak", "Olahraga", "Memasak"),
levels = c("Olahraga", "Memasak")
),
total = c(90, 10, 25, 75)
)
data_hobi
## jenis_kelamin hobi total
## 1 Laki-Laki Olahraga 90
## 2 Laki-Laki Memasak 10
## 3 Perempuan Olahraga 25
## 4 Perempuan Memasak 75
ggplot(
data_hobi,
aes(
x = hobi,
y = total,
fill = jenis_kelamin
)
) +
geom_bar(
stat = "identity",
position = "stack"
) +
geom_text(
aes(label = total),
position = position_stack(vjust = 0.5)
) +
labs(
title = "Universitas XYZ",
x = "Hobi Mahasiswa",
y = "Jumlah Mahasiswa",
fill = "Jenis Kelamin"
) +
theme_minimal()
### Gambar 3.58
ggplot(
data_hobi,
aes(
x = hobi,
y = total,
fill = jenis_kelamin
)
) +
geom_bar(
stat = "identity",
position = "dodge"
) +
geom_text(
aes(label = total),
position = position_dodge(width = 0.9),
vjust = -0.3
) +
labs(
title = "Universitas XYZ",
x = "Hobi Mahasiswa",
y = "Jumlah Mahasiswa",
fill = "Jenis Kelamin"
) +
theme_minimal()
### Gambar 3.59–3.60
data3_5 <- data.frame(
Produk = c("A", "B", "C", "D"),
Jumlah = c(12, 5, 8, 20)
)
write.csv(
data3_5,
"data3.5.csv",
row.names = FALSE
)
data3_5
## Produk Jumlah
## 1 A 12
## 2 B 5
## 3 C 8
## 4 D 20
simpan_pie <- read.csv("data3.5.csv")
# Diagram lingkaran sederhana
pie(
simpan_pie$Jumlah
)
# Diagram lingkaran dengan nama produk
pie(
simpan_pie$Jumlah,
labels = simpan_pie$Produk
)
# Diagram lingkaran dengan judul
pie(
simpan_pie$Jumlah,
labels = simpan_pie$Produk,
main = "Data Penjualan Produk A, B, C, dan D"
)
# Diagram lingkaran dengan warna
pie(
simpan_pie$Jumlah,
labels = simpan_pie$Produk,
main = "Data Penjualan Produk A, B, C, dan D",
col = c("red", "yellow", "orange", "lightyellow")
)
### Gambar 3.62
persentase <- round(
simpan_pie$Jumlah / sum(simpan_pie$Jumlah) * 100,
2
)
label_persen <- paste0(
simpan_pie$Produk,
"\n",
persentase,
"%"
)
pie(
simpan_pie$Jumlah,
labels = label_persen,
col = c("red", "yellow", "orange", "lightyellow"),
main = "Persentase Penjualan Produk A, B, C, dan D"
)
### Gambar 3.63
pie(
simpan_pie$Jumlah,
labels = simpan_pie$Jumlah,
main = "Data Penjualan Produk A, B, C, dan D",
col = c("red", "yellow", "orange", "lightyellow"),
clockwise = TRUE
)
legend(
"topright",
legend = paste("Produk", simpan_pie$Produk),
fill = c("red", "yellow", "orange", "lightyellow"),
cex = 0.8
)
### Gambar 3.64
pie(
simpan_pie$Jumlah,
labels = paste0(
round(simpan_pie$Jumlah / sum(simpan_pie$Jumlah) * 100, 2),
"%"
),
main = "Data Penjualan Produk A, B, C, dan D",
col = c("red", "yellow", "orange", "lightyellow"),
clockwise = TRUE
)
legend(
"topright",
legend = paste("Produk", simpan_pie$Produk),
fill = c("red", "yellow", "orange", "lightyellow"),
cex = 0.8
)
### Gambar 3.65
library(ggplot2)
ggplot(
simpan_pie,
aes(
x = "",
y = Jumlah,
fill = Produk
)
) +
geom_bar(
stat = "identity",
width = 1
) +
coord_polar(theta = "y") +
labs(
title = "Data Penjualan Produk A, B, C, dan D",
fill = "Produk"
) +
theme_void()
### Gambar 3.66
ggplot(
simpan_pie,
aes(
x = "",
y = Jumlah,
fill = Produk
)
) +
geom_bar(
stat = "identity",
width = 1
) +
coord_polar(theta = "y") +
geom_text(
aes(
label = paste0(
round(Jumlah / sum(Jumlah) * 100, 2),
"%"
)
),
position = position_stack(vjust = 0.5)
) +
labs(
title = "Data Penjualan Produk A, B, C, dan D",
fill = "Produk"
) +
theme_void()