R Markdown

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

Including Plots

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.

data <- 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(data, "data3.4.csv", row.names = FALSE)
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
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 Statistika 1", names.arg=c("A","B","C","D"),
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)
## Warning: package 'ggplot2' was built under R version 4.6.2
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(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, label=total), position=position_stack(vjust=0.5))

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, label=total), position=position_dodge(width=1))