# Gambar 3.1
simpan=read.table("data3.1.csv", header=TRUE, sep=";", dec=",")
simpan
# Gambar 3.2
plot(simpan[2:3], main="Pendapatan dan Pengeluaran per-Bulan, dalam Jutaan Rupiah")

# Gambar 3.3
Pendapatan=simpan$pendapatan
Pengeluaran=simpan$pengeluaran
plot(Pendapatan, Pengeluaran)

# Gambar 3.4
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.3
PENDAPATAN=simpan$pendapatan
PENGELUARAN=simpan$pengeluaran
qplot(PENDAPATAN, PENGELUARAN,
main="Pendapatan dan Pengeluaran per-Bulan, dalam Jutaan",
xlab="Pendapatan per-Bulan",
ylab="Pengeluaran per-Bulan")
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Gambar 3.5
jenis=simpan$jeniskelamin
qplot(PENDAPATAN, PENGELUARAN,
main="Pendapatan dan Pengeluaran per-Bulan, dalam Jutaan",
xlab="PENDAPATAN",
ylab="PENGELUARAN",
color=jenis)

# Gambar 3.6
qplot(PENDAPATAN, PENGELUARAN,
main="Pendapatan dan Pengeluaran per-Bulan, dalam Jutaan",
xlab="PENDAPATAN",
ylab="PENGELUARAN",
color=jenis,
shape=jenis)

# Gambar 3.7
library(ggplot2)
ggplot(simpan, aes(pendapatan, pengeluaran)) +
geom_point()

# Gambar 3.8
ggplot(simpan,aes(pendapatan, pengeluaran)) +
geom_point(aes(color=jenis, shape=jenis))

# Gambar 3.9
# Menyimpan grafik ke dalam variabel grafik
grafik <- ggplot(simpan,aes(pendapatan, pengeluaran)) +
geom_point(aes(color=jenis, shape=jenis))
# Mengubah warna
grafik +
scale_colour_manual(values=c("blue","orange"))

# Gambar 3.10
# Mengubah bentuk titik
grafik +
scale_shape_manual(values=c(16,5))

# Gambar 3.11
# Mengubah warna dan bentuk sekaligus
grafik +
scale_colour_manual(values=c("blue","orange")) +
scale_shape_manual(values=c(5,5))

# Gambar 3.12
# Membagi grafik berdasarkan jenis kelamin
grafik +
facet_grid(. ~ jeniskelamin)

# Gambar 3.13
# Membagi grafik berdasarkan jenis kelamin
# sekaligus mengatur warna
grafik +
facet_grid(. ~ jeniskelamin) +
scale_colour_manual(values=c("blue","orange"))

# Gambar 3.14
grafik +
geom_vline(xintercept=2.5)

# Gambar 3.15
grafik +
geom_vline(xintercept=2.5) +
geom_vline(xintercept=5)

# Gambar 3.16
grafik +
geom_vline(xintercept=1:5)

# Gambar 3.17
grafik +
geom_vline(xintercept=c(2.5,5,7.5))

# Gambar 3.18
grafik +
geom_vline(
xintercept=c(2.5,5,7.5),colour="green",linetype="longdash")

# Gambar 3.19
grafik +
geom_vline(
xintercept=c(2.5,5,7.5),
colour="green",
linetype="longdash"
) +
geom_hline(
yintercept=c(2,4,6),
colour="red",
linetype="longdash"
)

# Gambar 3.20
simpan=read.table("data3.2.csv",header=TRUE, sep=";", dec=",")
simpan
# Gambar 3.21
Tahun=simpan$tahun
Jumlah_A=simpan$jenis.barang.A
Jumlah_B=simpan$jenis.barang.B
Jumlah_C=simpan$jenis.barang.C
# Menampilkan masing-masing jumlah
Jumlah_A
## [1] 90 110 115 130 140 155 160
# Gambar 3.22
Jumlah_B
## [1] 85 90 105 110 120 125 130
# Gambar 3.23
Jumlah_C
## [1] NA NA NA NA NA NA NA
# Gambar 3.24
plot(Tahun,Jumlah_A)

# Gambar 3.25
plot(Tahun,Jumlah_A,type="o")

# Gambar 3.26
plot(Tahun,Jumlah_A,type="o",col="blue")

# Gambar 3.27
plot(Tahun,Jumlah_A,type="o",col="green")

# Gambar 3.28
plot(Tahun,Jumlah_A,type="o",col="red")
plot(Tahun,Jumlah_A,type="o",col="red")
lines(Tahun,Jumlah_B,type="o",col="blue")

# Gambar 3.29
plot(Tahun,Jumlah_A,
type="o",
col="red",
ylim=c(70,180))
lines(Tahun,Jumlah_B,
type="o",
col="blue")

# Gambar 3.30
plot(Tahun,Jumlah_A,
type="o",
col="red",
ylim=c(40,180))
lines(Tahun,Jumlah_B,
type="o",
col="blue")
lines(Tahun,Jumlah_C,
type="o",
col="green")

# Gambar 3.31
plot(Tahun,Jumlah_A,
type="o",
col="red",
ylim=c(40,180))
lines(Tahun,Jumlah_B,
type="o",
pch=22,
lty=2,
col="blue")
lines(Tahun,Jumlah_C,
type="o",
col="green")

# Gambar 3.32
plot(Tahun,Jumlah_A,
type="o",
pch=22,
lty=2,
col="red",
ylim=c(40,180))
lines(Tahun,Jumlah_B,
type="o",
pch=22,
lty=2,
col="blue")
lines(Tahun,Jumlah_C,
pch=22,
lty=2,
type="p",
col="green")

# Gambar 3.33
plot(Tahun,Jumlah_A,
type="p",
pch=22,
lty=2,
col="red",
ylim=c(40,180))
lines(Tahun,Jumlah_B,
type="p",
pch=22,
lty=2,
col="blue")
lines(Tahun,Jumlah_C,
pch=22,
lty=2,
type="p",
col="green")

# Gambar 3.34
plot.new()
plot(Tahun,Jumlah_A,
type="o",
col="red",
ylim=c(40,180))
lines(Tahun,Jumlah_B,
type="o",
col="blue")
lines(Tahun,Jumlah_C,
type="o",
col="green")
title(
main="Data Penjualan Barang A, B, C, dari Tahun 2001-2007",
col.main="red",
font.main=4
)

# Gambar 3.35
Total=Jumlah_A
plot.new()
plot(Tahun,Total,
type="o",
col="red",
ylim=c(40,180),
lty=23)
lines(Tahun,Jumlah_B,
type="o",
col="blue",
lty=23)
lines(Tahun,Jumlah_C,
type="o",
col="green",
lty=23)
legend(
2001,160,
c("Jenis Barang A",
"Jenis Barang B",
"Jenis Barang C"),
cex=0.8,
col=c("red","blue","green"),
lty=30
)
title(
main="Penjualan Barang A, B, C, dari Tahun 2001-2007",
col.main="red",
font.main=4
)

# Gambar 3.36
Total=Jumlah_A
plot.new()
plot(Tahun,Total,
type="o",
col="red",
ylim=c(40,180),
lty=23)
lines(Tahun,Jumlah_B,
type="l",
col="blue",
lty=23)
lines(Tahun,Jumlah_C,
type="o",
col="green",
lty=23)
legend(
2001,160,
c("Jenis Barang A",
"Jenis Barang B",
"Jenis Barang C"),
cex=0.8,
col=c("red","blue","green"),
lty=30
)
title(
main="Penjualan Barang A, B, C, dari Tahun 2001-2007",
col.main="red",
font.main=4
)

# Gambar 3.37
Total=Jumlah_A
plot.new()
plot(Tahun,Total,
type="o",
col="red",
ylim=c(40,180),
lty=23)
lines(Tahun,Jumlah_B,
type="o",
col="blue",
lty=23)
lines(Tahun,Jumlah_C,
type="o",
col="green",
lty=23)
legend(
2001,160,
c("Jenis Barang A",
"Jenis Barang B",
"Jenis Barang C"),
cex=0.8,
col=c("red","blue","green"),
lty=30
)
title(
main="Penjualan Barang A, B, C, dari Tahun 2001-2007",
col.main="red",
font.main=4
)

# Gambar 3.38
Total=Jumlah_A
plot.new()
plot(
Tahun,Total,
type="o",
col="red",
ylim=c(40,180),
lty=23
)
axis(
at=2001:2007,
side=1,
labels=c("A","B","C","D","E","F","G")
)
lines(
Tahun,Jumlah_B,
type="o",
col="blue",
lty=23
)
lines(
Tahun,Jumlah_C,
type="o",
col="green",
lty=23
)
legend(
2001,160,
c("Jenis Barang A",
"Jenis Barang B",
"Jenis Barang C"),
cex=0.8,
col=c("red","blue","green"),
lty=30
)
title(
main="Penjualan Barang A, B, C, dari Tahun 2001-2007",
col.main="red",
font.main=4
)

# Gambar 3.39
Total=Jumlah_A
plot.new()
plot(
Tahun,Total,
type="o",
col="red",
ylim=c(40,180),
lty=23
)
axis(
at=2001:2007,
side=1,
labels=c(
"Tahun 1",
"Tahun 2",
"Tahun 3",
"Tahun 4",
"Tahun 5",
"Tahun 6",
"Tahun 7"
)
)
lines(
Tahun,Jumlah_B,
type="o",
col="blue",
lty=23
)
lines(
Tahun,Jumlah_C,
type="o",
col="green",
lty=23
)
legend(
2001,160,
c("Jenis Barang A",
"Jenis Barang B",
"Jenis Barang C"),
cex=0.8,
col=c("red","blue","green"),
lty=30
)
title(
main="Penjualan Barang A, B, C, dari Tahun 2001-2007",
col.main="red",
font.main=4
)

# Gambar 3.40
Total=Jumlah_A
plot(
Tahun,Total,
type="o",
col="red",
ylim=c(40,180)
)
axis(
at=2001:2007,
side=1,
labels=c(
"Tahun 1",
"Tahun 2",
"Tahun 3",
"Tahun 4",
"Tahun 5",
"Tahun 6",
"Tahun 7"
)
)
lines(Tahun,Jumlah_B,
type="o",
col="blue",
lty=23)
lines(Tahun,Jumlah_C,
type="o",
col="green",
lty=23)
legend(
2001,160,
c("Jenis Barang A",
"Jenis Barang B",
"Jenis Barang C"),
cex=0.8,
col=c("red","blue","green"),
lty=30
)
title(
main="Penjualan Barang A, B, C, dari Tahun 2001-2007",
col.main="red",
font.main=4
)

# Gambar 3.41
simpan=read.table("data3.3.csv",header=TRUE, sep=";", dec=",")
simpan
# Gambar 3.42
Tahun=simpan$tahun
Jumlah_A=simpan$jenis.barang.A
# Grafik batang sederhana
barplot(Jumlah_A,Tahun)

# Gambar 3.43
barplot(
Jumlah_A,Tahun,
main="Penjualan Barang Jenis A dari Tahun 2001-2007",
xlab="Tahun",
ylab="Jumlah Barang yang Terjual",
names.arg=c(
"2001","2002","2003",
"2004","2005","2006","2007"
)
)

# Gambar 3.44
barplot(
Jumlah_A,Tahun,
main="Penjualan Barang Jenis A dari Tahun 2001-2007",
xlab="Tahun",
ylab="Jumlah Barang yang Terjual",
names.arg=c(
"2001","2002","2003",
"2004","2005","2006","2007"
),
border="blue"
)

# Gambar 3.45
barplot(
Jumlah_A,Tahun,
main="Penjualan Barang Jenis A dari Tahun 2001-2007",
xlab="Tahun",
ylab="Jumlah Barang yang Terjual",
names.arg=c(
"2001","2002","2003",
"2004","2005","2006","2007"
),
border="red"
)

# Gambar 3.46
barplot(
Jumlah_A,Tahun,
main="Penjualan Barang Jenis A dari Tahun 2001-2007",
xlab="Tahun",
ylab="Jumlah Barang yang Terjual",
names.arg=c(
"2001","2002","2003",
"2004","2005","2006","2007"
),
border="green",
density=c(10,20,30,40,50,60,70)
)

# Gambar 3.47
library(ggplot2)
ggplot(
data=simpan,
aes(x=Tahun,y=Jumlah_A)
) + geom_bar(stat="identity")

# Gambar 3.48
ggplot(
data=simpan,
aes(x=Tahun,y=Jumlah_A)
) +
geom_bar(
stat="identity",
fill="darkblue"
)

# Gambar 3.49
ggplot(
data=simpan,
aes(x=Tahun,y=Jumlah_A)
) +
geom_bar(
stat="identity",
fill=heat.colors(7)
)

# Gambar 3.50
simpan=read.table("data3.4.csv",header=TRUE, sep=";", dec=",")
simpan
# Gambar 3.51
frekuensi=c(90,10,25,75)
# Grafik hubungan jenis kelamin dan hobi
barplot(
t(matrix(
frekuensi,
ncol=2,
byrow=TRUE,
dimnames=list(
c("Laki-Laki","Perempuan"),
c("Olahraga","Memasak")
)
)),
main="Hubungan antara Jenis Kelamin dan Hobi",
xlab="Jenis Kelamin",
col=c("darkblue","orange"),
beside=TRUE,
ylim=c(0,150),
legend.text=TRUE,
args.legend=list(x="topright")
)

# Gambar 3.52
frekuensi2=c(2,12,16,6)
barplot(
frekuensi2,
ylim=c(0,20),
main="Jumlah Mahasiswa yang Memperoleh Nilai A, B, C, dan D, untuk Matakuliah Matematika 1",
names.arg=c("A","B","C","D"),
ylab="Jumlah Mahasiswa",
xlab="Nilai Mahasiswa",
cex.names=0.8,
col=c("green","yellow","orange","red")
)

# Gambar 3.53
dat=data.frame(
jenis_kelamin=factor(
c("Laki-Laki","Perempuan"),
levels=c("Laki-Laki","Perempuan")
),
total=c(20,70)
)
dat
# Gambar 3.54
library(ggplot2)
# Grafik dasar
ggplot(
data=dat,
aes(x=jenis_kelamin,y=total)
) +
geom_bar(stat="identity")

# Gambar 3.55
ggplot(
data=dat,
aes(
x=jenis_kelamin,
y=total,
fill=jenis_kelamin
)
) +
geom_bar(stat="identity")

# Gambar 3.56
ggplot(
data=dat,
aes(
x=jenis_kelamin,
y=total,
fill=jenis_kelamin
)
) +
geom_bar(stat="identity") +
guides(fill=FALSE)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Gambar 3.57
ggplot(
data=dat,
aes(
x=jenis_kelamin,
y=total,
fill=jenis_kelamin
)
) +
geom_bar(stat="identity") +
xlab("Jenis Kelamin") +
ylab("Jumlah Mahasiswa") +
ggtitle("Universitas XYZ")

# Gambar 3.58
dat=data.frame(
jenis_kelamin=factor(
c(
"Laki-Laki",
"Laki-Laki",
"Perempuan",
"Perempuan"
)
),
hobi=factor(
c(
"Olahraga",
"Memasak",
"Olahraga",
"Memasak"
),
levels=c("Olahraga","Memasak")
),
total=c(80,20,40,60)
)
dat
# Gambar 3.59
ggplot(
data=dat,
aes(
x=hobi,
y=total,
fill=jenis_kelamin
)
) +
geom_bar(stat="identity") +
xlab("Hobi Mahasiswa") +
ylab("Jumlah Mahasiswa") +
ggtitle("Universitas XYZ") +
geom_text(
aes(y=total/1.3,label=total),
position="stack"
)

# Gambar 3.60
ggplot(
data=dat,
aes(
x=hobi,
y=total,
fill=jenis_kelamin
)
) +
geom_bar(
stat="identity",
position=position_dodge()
) +
xlab("Hobi Mahasiswa") +
ylab("Jumlah Mahasiswa") +
ggtitle("Universitas XYZ")

# Gambar 3.61
ggplot(
data=dat,
aes(
x=hobi,
y=-total,
fill=jenis_kelamin
)
) +
geom_bar(
stat="identity",
position=position_dodge()
) +
xlab("Hobi Mahasiswa") +
ylab("Jumlah Mahasiswa") +
ggtitle("Universitas XYZ") +
geom_text(
aes(y=total/4,label=total),
position=position_dodge(width=1)
)

# Gambar 3.62
simpan=read.table("data3.5.csv",header=TRUE, sep=";", dec=",")
simpan
# Gambar 3.63
pie(
simpan$Jumlah,
labels=simpan$Produk,
main="Data Penjualan Produk A, B, C, dan D"
)

# Gambar 3.64
pie(
simpan$Jumlah,
labels=simpan$Produk,
main="Data Penjualan Produk A, B, C, dan D",
col=heat.colors(4)
)

# Gambar 3.65
pie(
simpan$Jumlah,
labels=simpan$Jumlah,
main="Data Penjualan Produk A, B, C, dan D",
col=heat.colors(4)
)
# Membuat warna
colors=heat.colors(4)
# Legenda
legend(
1,0.5,
c(
"Produk A",
"Produk B",
"Produk C",
"Produk D"
),
cex=0.8,
fill=colors
)

# Gambar 3.66
pie(
simpan$Jumlah,
labels=simpan$Jumlah,
main="Data Penjualan Produk A, B, C, dan D",
col=c(
"darkblue",
"orange",
"yellow",
"red"
)
)
colors=c(
"darkblue",
"orange",
"yellow",
"red"
)
legend(
1,0.5,
c(
"Produk A",
"Produk B",
"Produk C",
"Produk D"
),
cex=0.8,
fill=colors
)

# Gambar 3.67
Persen=round(
simpan$Jumlah/sum(simpan$Jumlah)*100,
4
)
Persen=paste(Persen,"%",sep="")
pie(
simpan$Jumlah,
labels=Persen,
main="Data Penjualan Produk A, B, C, dan D",
col=c(
"darkblue",
"orange",
"yellow",
"red"
)
)
colors=c(
"darkblue",
"orange",
"yellow",
"red"
)
legend(
1,0.5,
c(
"Produk A",
"Produk B",
"Produk C",
"Produk D"
),
cex=0.8,
fill=colors
)

# Gambar 3.68
Jumlah=simpan$Jumlah
Produk=simpan$Produk
library(ggplot2)
pie=ggplot(
simpan,
aes(x="",y=Jumlah,fill=Produk)
) +
geom_bar(
width=1,
stat="identity"
) +
coord_polar("y",start=0)
pie

# Gambar 3.69
library(ggplot2)
library(grid)
blank_theme=theme(
axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
panel.border=element_blank(),
panel.grid=element_blank(),
axis.ticks=element_blank(),
plot.title=element_text(size=14,face="bold")
)
# Menampilkan jumlah pada diagram
library(scales)
pie +
blank_theme +
geom_text(
aes(
y=Jumlah/4 +
c(0,cumsum(Jumlah)[-length(Jumlah)]),
label=Jumlah
),
size=5
)

# Gambar 3.70
pie +
blank_theme +
geom_text(
aes(
y=Jumlah/4 +
c(0,cumsum(Jumlah)[-length(Jumlah)]),
label=Jumlah
),
size=5
) +
scale_fill_manual(
values=c(heat.colors(4))
)

# Gambar 3.71
Persen=round(
simpan$Jumlah/sum(simpan$Jumlah)*100,
2
)
Persen=paste(Persen,"%",sep="")
# Menampilkan persentase
pie +
blank_theme +
geom_text(
aes(
y=Jumlah/4 +
c(0,cumsum(Jumlah)[-length(Jumlah)]),
label=Persen
),
size=5
) +
scale_fill_manual(
values=c(heat.colors(4))
)

# Gambar 3.72
simpan=read.table("IQ.csv",header=TRUE, sep=";", dec=",")
simpan
# Gambar 3.73
simpan_skor_IQ=simpan$IQ
# Histogram sederhana
hist(simpan_skor_IQ)

# Gambar 3.74
hist(
simpan_skor_IQ,
col="lightblue"
)

# Gambar 3.75
hist(
simpan_skor_IQ,
col="darkblue",
ylim=c(0,40),
main="Contoh Histogram",
ylab="Frekuensi"
)

# Gambar 3.76
hist(
simpan_skor_IQ,
col="orange",
ylim=c(0,40),
main="Contoh Histogram",
ylab="Frekuensi",
breaks=c(90,100,110,120)
)

# Gambar 3.77
hist(
simpan_skor_IQ,
col=heat.colors(6),
ylim=c(0,30),
main="Contoh Histogram",
ylab="Frekuensi",
breaks=c(
90,95,100,105,110,115,120
),
xlim=c(90,125)
)

# Gambar 3.78
hist(
simpan_skor_IQ,
col=heat.colors(6),
ylim=c(0,30),
main="Contoh Histogram",
ylab="Frekuensi",
breaks=c(
90,93,96,99,102,
105,108,111,114,117,120
),
xlim=c(90,125)
)

# Gambar 3.79
hist(
simpan_skor_IQ,
breaks=6,
col=heat.colors(6),
ylim=c(0,30),
main="Contoh Histogram",
ylab="Frekuensi",
xlim=c(90,125)
)

# Gambar 3.80
hist(
simpan_skor_IQ,
breaks=c(90,117,120),
ylim=c(0,50),
xlim=c(90,125),
main="Contoh Histogram",
col=heat.colors(2)
)

# Gambar 3.81
hist(
simpan_skor_IQ,
breaks=c(90,117,120),
ylim=c(0,80),
xlim=c(90,125),
main="Contoh Histogram",
col=heat.colors(2),
freq=TRUE
)
## Warning in plot.histogram(r, freq = freq1, col = col, border = border, angle =
## angle, : the AREAS in the plot are wrong -- rather use 'freq = FALSE'

# Gambar 3.82
hist(
simpan_skor_IQ,
breaks=c(90,92,97,117,120),
ylim=c(0,80),
xlim=c(90,125),
main="Contoh Histogram",
col=heat.colors(4),
freq=TRUE
)
## Warning in plot.histogram(r, freq = freq1, col = col, border = border, angle =
## angle, : the AREAS in the plot are wrong -- rather use 'freq = FALSE'

# Gambar 3.83
ggplot(
data=simpan,
aes(IQ)
) +
geom_histogram(
breaks=c(
90,95,100,105,110,115,120
),
col="darkblue",
fill=heat.colors(6)
)

# Gambar 3.84
ggplot(
data=simpan,
aes(IQ)
) +
geom_histogram(
breaks=c(
90,95,100,105,110,115,120
),
col="red",
aes(fill=after_stat(count))
) +
labs(title="Contoh Histogram") +
labs(x="IQ",y="Jumlah") +
xlim(c(90,125)) +
ylim(c(0,20)) +
scale_fill_gradient(
"count",
low="green",
high="red"
)
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_bar()`).

# Gambar 3.85
library(ggplot2)
ggplot(
data=simpan,
aes(IQ)
) +
geom_histogram(
breaks=c(
90,93,96,99,102,105,
111,114,115,120
),
col="darkblue",
fill=heat.colors(9),
aes(fill=after_stat(count))
) +
labs(title="Contoh Histogram") +
labs(x="IQ",y="Jumlah") +
xlim(c(90,125)) +
ylim(c(0,20)) +
scale_fill_gradient(
"count",
low="white",
high="darkblue"
)
