Menyajikan data dengan grafik batang (bagian satu)

simpan=read.table("data3.3.csv",header=TRUE, sep=",") #membaca data
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=jenis.barang.A)) + geom_bar(stat="identity")

ggplot(data=simpan, aes(x=TAHUN, y=jenis.barang.A)) + geom_bar(stat="identity", fill="darkblue")

ggplot(data=simpan, aes(x=TAHUN, y=jenis.barang.A)) + geom_bar(stat="identity", fill=heat.colors(7))

## Menyajika data dengan grafik batang (bagain kedua)

simpan=read.table("data3.4.csv",header=TRUE, sep=",") #membaca data
simpan
##   jenis.kelamin     hobi jumlah
## 1     laki-laki olahraga     90
## 2     laki-laki  memasak     10
## 3     perempuan olahraga     25
## 4     perempuan  memasak     75
frekuensi1=c(90,10,25,75)
barplot(t(matrix(frekuensi1, 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))

## Menyajikan data dengan diagram lingkaran

simpan=read.table("data3.5.csv",header=TRUE, sep=",") #membaca data
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)

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

## Menyajikan Data dengan Histogram

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

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

ggplot(data=simpan, aes(IQ)) + geom_histogram(breaks=c(90,95,100,105,110,115,120), col="red",
aes(fill=..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: 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.
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_bar()`).

library(ggplot2)

library(ggplot2)

library(ggplot2)

ggplot(data = simpan, aes(x = simpan_skor_IQ)) + 
  geom_histogram(breaks = c(90,93,96,99,102,105,108,111,114,115,120), 
                 col = "darkblue", 
                 fill = heat.colors(10)) + 
  labs(title = "Contoh Histogram", x = "IQ", y = "Jumlah") + 
  xlim(c(90, 125)) + 
  ylim(c(0, 20))