#visualisasi scatter plot menggunakan R
#Data3.1
simpan=read.table(file.choose(),header=TRUE,sep=",")
simpan
##    jenis.kelamin pendapatan pengeluaran
## 1      laki-laki          1         0.5
## 2      laki-laki          2         0.9
## 3      laki-laki          3         1.1
## 4      laki-laki          4         2.0
## 5      laki-laki          5         4.0
## 6      laki-laki          6         5.0
## 7      laki-laki          7         6.2
## 8      laki-laki          8         5.5
## 9      laki-laki          9         5.8
## 10     perempuan         10         4.5
## 11     perempuan          1         0.7
## 12     perempuan          2         1.4
## 13     perempuan          3         2.8
## 14     perempuan          4         3.3
## 15     perempuan          5         4.3
## 16     perempuan          6         5.0
## 17     perempuan          7         6.7
## 18     perempuan          8         5.9
## 19     perempuan          9         5.7
## 20     perempuan         10         8.0
plot(simpan[2:3],main="Pendapatan dan Pengeluaran per-Bulan, dalam Jutaan Rupiah")

pendapatan=simpan$pendapatan
pengeluaran=simpan$pengeluaran
plot(pendapatan, pengeluaran)

library(ggplot2)
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 per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

library(ggplot2)
jenis = simpan$jeniskelamin
qplot(PENDAPATAN, PENGELUARAN, main = "Pendapatan dan Pengeluaran per-Bulan, dalam jutaan", xlab = "Pendapatan", ylab = "Pengeluaran", color = jenis)

qplot(PENDAPATAN, PENGELUARAN, main = "Pendapatan dan pengeluaran per-bulan, dalam jutaan", xlab = "PENDAPATAN", ylab = "PENGELUARAN", color = jenis, shape = jenis)

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

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

grafik <- ggplot(simpan, aes(pendapatan, pengeluaran)) + geom_point(aes(color = jenis, shape = jenis))
grafik + scale_colour_manual(values = c("blue", "orange"))

grafik + scale_shape_manual(values = c(16, 5))

grafik + scale_colour_manual(values = c("blue", "orange")) + scale_shape_manual(values = c(5, 5))

grafik + facet_grid(. ~ jenis.kelamin)

grafik + facet_grid(. ~ jenis.kelamin) + scale_colour_manual(values = c("blue", "orange"))

grafik + geom_vline(xintercept = 2.5)

grafik + geom_vline(xintercept = 2.5) + geom_vline(xintercept = 5)

grafik + geom_vline(xintercept = 1:5)

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

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

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")

#penyajian data dengan menggunakan grafik garis
#Data3.2
simpan=read.table(file.choose(), header=TRUE, sep=",")
simpan
##   tahun jenis.barang.A jenis.barang.B jenis.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             75
## 6  2006            155            125             80
## 7  2007            160            130             85
Tahun=simpan$tahun
Jumlah_A=simpan$jenis.barang.A
Jumlah_B=simpan$jenis.barang.B
Jumlah_C=simpan$jenis.barang.C

Jumlah_A
## [1]  90 110 115 130 140 155 160
Jumlah_B
## [1]  85  90 105 110 120 125 130
Jumlah_C
## [1] 50 55 60 65 75 80 85
plot(Tahun,Jumlah_A)

plot(Tahun,Jumlah_A, type="o")

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

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

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

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

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")

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

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")

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="o", col="green")

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")

plot(Tahun,Jumlah_A, type="o", 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="l", col="green")

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)

Total = Jumlah_A
plot.new()
plot(Tahun,Total, 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)

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

Total = Jumlah_A
plot.new()
plot(Tahun,Total, type="o", col="red", ylim=c(40,180))
lines(Tahun, Jumlah_B, type="o", col="blue")
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)

Total = Jumlah_A
plot.new()
plot(Tahun,Total, type="o", col="red", ylim=c(40,180))
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)

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)

Total = Jumlah_A
plot.new()
plot(Tahun,Total, type="o", col="red", ylim=c(40,180), lty=23)
lines(Tahun, Jumlah_B, type="s", 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)

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)

Total = Jumlah_A
plot.new()
plot(Tahun,Total, type="o", col="red", ylim=c(40,180))
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)

Total = Jumlah_A
plot(Tahun,Total, type="o", col="red", ylim=c(40,180), xaxt="n")
Axis(at=2001:2007, side = 1, labels = c("A","B","C","D","E","F","G"))
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)

Total = Jumlah_A
plot.new()
plot(Tahun,Total, type="o", col="red", ylim=c(40,180))
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)

Total = Jumlah_A
plot(Tahun,Total, type="o", col="red", ylim=c(40,180), xaxt="n")
Axis(at=2001:2007, side = 1, labels = c("A","B","C","D","E","F","G"))
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)

Total = Jumlah_A
plot(Tahun,Total, type="o", col="red", ylim=c(40,180), xaxt="n")
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="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)

Total = Jumlah_A
plot(Tahun,Total, type="o", col="red", ylim=c(40,180), xaxt="n")
Axis(at=2001:2007, side = 3, labels = c("Tahun 1","Tahun 2","Tahun 3","Tahun 4","Tahun 5","Tahun 6","Tahun 7"))
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)

#Penyajian data dengan menggunakan grafik batang (1)
#Data3.3
simpan = read.table(file.choose(), header = TRUE, sep = ",")
simpan
##   tahun jenis.barang.A
## 1  2001             90
## 2  2002            110
## 3  2003            115
## 4  2004            130
## 5  2005            140
## 6  2006            155
## 7  2007            160
Tahun=simpan$tahun
Jumlah_A=simpan$jenis.barang.A
barplot(Jumlah_A,Tahun)

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"))

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"))

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")

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")

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

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

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

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

#Penyajian data dengan menggunakan grafik batang (2)
#Data3.4
simpan = read.table(file.choose(), header = TRUE, sep = ",")
simpan
##   Jenis.Kelamin     Hobi Jumlah
## 1     Laki-Laki Olahraga     90
## 2     Laki-Laki  Memasak     10
## 3     Perempuan Olahraga     25
## 4     Perempuan  Memasak     75
frekuensi=c(90,10,25,75)
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"))

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") )

dat = data.frame(
  jenis_kelamin=factor(c("Laki-Laki","Perempuan"), levels=c("Laki-Laki","Perempuan")), total=c(20,70))

dat
##   jenis_kelamin total
## 1     Laki-Laki    20
## 2     Perempuan    70
library(ggplot2)
ggplot(data=dat, aes(x=jenis_kelamin, y=total))+geom_bar(stat="identity")

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

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 per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

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")

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
##   jenis_kelamin     hobi total
## 1     Laki-Laki Olahraga    80
## 2     Laki-Laki  Memasak    20
## 3     Perempuan Olahraga    40
## 4     Perempuan  Memasak    60
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" )

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")

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

#Penyajian data dengan menggunakan diaram lingkaran
#Data3.5
simpan = read.table(file.choose(), header = TRUE, sep = ",") 
simpan
##   Produk Jumlah
## 1      A     12
## 2      B      5
## 3      C      8
## 4      D     20
pie(simpan$Jumlah,labels=simpan$Produk, main="Data Penjualan Produk A, B, C, dan D")

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

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

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 )

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 )

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 )

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

library(ggplot2)
library(grid)
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.5.3
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")
)

library(scales)
pie + blank_theme + geom_text(aes(y=Jumlah/4 + c(0,cumsum(Jumlah)[-length(Jumlah)]), label=Jumlah), size=5)

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

Persen=round(simpan$Jumlah/sum(simpan$Jumlah)*100,2)
Persen=paste(Persen, "%",sep="")
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)))

# Penyajian data dengan menggunakan histogram
#Data3.6
simpan = read.table(file.choose(), header = TRUE, sep = ",")
simpan
##     IQ
## 1  111
## 2  111
## 3  111
## 4  111
## 5  111
## 6  111
## 7  111
## 8  110
## 9  110
## 10 110
## 11 110
## 12 110
## 13 110
## 14 110
## 15 112
## 16 112
## 17 112
## 18 112
## 19 112
## 20 112
## 21 113
## 22 113
## 23 113
## 24 113
## 25 113
## 26 113
## 27 114
## 28 114
## 29 114
## 30 114
## 31 115
## 32 115
## 33 115
## 34 116
## 35 116
## 36 117
## 37  90
## 38  91
## 39  92
## 40  92
## 41  93
## 42  93
## 43  93
## 44  94
## 45  94
## 46  94
## 47  94
## 48  95
## 49  95
## 50  95
## 51  95
## 52  95
## 53  95
## 54  96
## 55  96
## 56  96
## 57  96
## 58  97
## 59  97
## 60  97
## 61  97
## 62  97
## 63  98
## 64  98
## 65  98
## 66 101
## 67 101
## 68 101
## 69 102
## 70 102
## 71 103
## 72 104
## 73 103
## 74 102
## 75 108
## 76 109
## 77 118
simpan_skor_IQ=simpan$IQ
hist(simpan_skor_IQ)

hist(simpan_skor_IQ, col="lightblue")

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

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

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

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

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

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

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'

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'

library(ggplot2)

ggplot(data=simpan, aes(x=IQ)) + 
  geom_histogram(breaks=c(90,93,96,99,102,105,108,111,114,115,120), col="darkblue", aes(fill=..count..)) + 
  labs(title="Contoh Histogram", x="IQ", y="Jumlah") + 
  xlim(c(90,125)) + 
  ylim(c(0,20)) + 
  scale_fill_gradient(name="count", low="yellow", high="red")
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#Data3.6
simpan <- read.table(file.choose(), header = TRUE, sep = ",")
simpan
##     IQ
## 1  111
## 2  111
## 3  111
## 4  111
## 5  111
## 6  111
## 7  111
## 8  110
## 9  110
## 10 110
## 11 110
## 12 110
## 13 110
## 14 110
## 15 112
## 16 112
## 17 112
## 18 112
## 19 112
## 20 112
## 21 113
## 22 113
## 23 113
## 24 113
## 25 113
## 26 113
## 27 114
## 28 114
## 29 114
## 30 114
## 31 115
## 32 115
## 33 115
## 34 116
## 35 116
## 36 117
## 37  90
## 38  91
## 39  92
## 40  92
## 41  93
## 42  93
## 43  93
## 44  94
## 45  94
## 46  94
## 47  94
## 48  95
## 49  95
## 50  95
## 51  95
## 52  95
## 53  95
## 54  96
## 55  96
## 56  96
## 57  96
## 58  97
## 59  97
## 60  97
## 61  97
## 62  97
## 63  98
## 64  98
## 65  98
## 66 101
## 67 101
## 68 101
## 69 102
## 70 102
## 71 103
## 72 104
## 73 103
## 74 102
## 75 108
## 76 109
## 77 118
# Mengambil data IQ
simpan_skor_IQ <- as.numeric(simpan$IQ)

# Histogram dasar
hist(simpan_skor_IQ)

# Histogram dengan warna lightblue
hist(simpan_skor_IQ, col = "lightblue")

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

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

# Histogram dengan 6 kelas
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))

# Histogram dengan interval lebih banyak
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))

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

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

# Histogram dengan frekuensi
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'

# Histogram dengan 4 kelas
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'

# Histogram menggunakan ggplot2
library(ggplot2)

ggplot(data = simpan, aes(x = IQ)) +
  geom_histogram(
    breaks = c(90,93,96,99,102,105,108,111,114,115,120),
    col = "darkblue",
    aes(fill = after_stat(count))
  ) +
  labs(
    title = "Contoh Histogram",
    x = "IQ",
    y = "Jumlah"
  ) +
  xlim(c(90,125)) +
  ylim(c(0,20)) +
  scale_fill_gradient(
    name = "count",
    low = "yellow",
    high = "red"
  )

class(simpan_skor_IQ)
## [1] "numeric"
str(simpan)
## 'data.frame':    77 obs. of  1 variable:
##  $ IQ: int  111 111 111 111 111 111 111 110 110 110 ...