Email          : albert.prayogo99@gmail.com
RPubs         : https://rpubs.com/albert23899
Jurusan      : Statistika
Address     : ARA Center, Matana University Tower
             Jl. CBD Barat Kav, RT.1, Curug Sangereng, Kelapa Dua, Tangerang, Banten 15810.
library(ggplot2) #Untuk Visualisasi
Marriage<-read.csv("https://raw.githubusercontent.com/Bakti-Siregar/dataset/master/Bookdown-Data-Science-for-Beginners/Marriage.csv") #Memuat Data
ggplot(Marriage, aes(x=zodiacs))+ #Memplot Distribusi dari Zodiacs
geom_bar(fill="cornflowerblue",
color="azure4")+ #Anda dapat mengganti warna
theme_minimal()+ #Menggunakan tema minimal
labs(x="Zodiacs",
y="Frequency",
title="Marriage Participiants by Zodiacs") #Mengganti label dan juga judul chart Menambahkan simbol persen untuk label y
library(ggplot2)
ggplot(Marriage,
aes(
x= zodiacs,
y=..count../sum(..count..)))+
geom_bar(fill=rainbow(12),color="azure4")+
theme_minimal()+ #Menggunakan tema minimal
labs(x="Zodiacs",
y="Percents",
title="Marriage Participiants in Percents")scale_y_continuous(labels=scales::percent) #Menggunakan simbol % untuk label## <ScaleContinuousPosition>
## Range:
## Limits: 0 -- 1
library(dplyr) #Untuk memanipulasi data##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2) #Untuk Visualisasi Data
plotdata<-Marriage %>% #Memuat Dataset
count(zodiacs) #Jumlah peserta di setiap Zodiacs
#Menyusun plot batang secara meningkat
ggplot(plotdata,
aes(
x=reorder(zodiacs,n),
y=n))+
geom_bar(stat="identity",
fill=rainbow(12),
color="azure4")+
theme_minimal()+ #Menggunakan tema minimal
labs(x="Zodiacs",
y="Frequency",
title="Sorting Categories") Memberi label setiap batang dengan nilai numeriknya
library(dplyr)
library(ggplot2)
library(scales)
plotdata<-Marriage %>%
count(zodiacs) %>%
mutate(pct=n/sum(n),
pctlabel=paste0(round(pct*100),"%"))
#Plot batang sebagai persentase, dalam urutan menurun dengan diagram batang
ggplot(plotdata,
aes(x=reorder(zodiacs,-pct),
y=pct))+
geom_bar(stat="identity",
fill=rainbow(12),
color="azure4")+
geom_text(aes(label=pctlabel),
vjust=-0.25)+
theme_minimal()+
scale_y_continuous(labels=percent)+
labs(x="zodiacs",
y="Percent",
title="Labeling Bars") Memutar Label Sumbu
library(ggplot2)
library(scales)
#Plot batang sebagai persentase, dalam urutan menurun dengan diagram batang
ggplot(plotdata,
aes(x=reorder(zodiacs,-pct),
y=pct))+
geom_bar(stat="identity",
fill=rainbow(12),
color="azure4")+
geom_text(aes(label=pctlabel),
vjust=-0.25)+
theme_minimal()+
scale_y_continuous(labels=percent)+
labs(x="Zodiacs",
y="Percent",
title="Overlapping Labels") +
theme(axis.text.x=element_text(angle=45,hjust=1)) Membalik sumbu x dan y
library(ggplot2)
library(scales)
#Plot batang sebagai persentase, dalam urutan menurun dengan diagram batang
ggplot(plotdata,
aes(x=reorder(zodiacs,-pct),
y=pct))+
geom_bar(stat="identity",
fill=rainbow(12),
color="azure4")+
geom_text(aes(label=pctlabel),
vjust=-0.10)+
theme_minimal()+
scale_y_continuous(labels=percent)+
labs(x="Zodiacs",
y="Percent",
title="Overlapping Labels") +
coord_flip() ## Diagram Pie
library(dplyr)
library(ggplot2)
library(scales)
#Persiapan Data
plotdata<-Marriage %>%
count(race) %>%
arrange(desc(race))%>%
mutate(prop=round(n*100/sum(n),1),
lab.ypos=cumsum(prop)-0.5*prop)
#Membuat Diagram Pie
mycols<-c("#0073C2FF","#EFC000FF","#868686FF","#CD534CFF")
ggplot(plotdata,aes(x="",y=prop,fill=race))+
geom_bar(width=1, stat="identity",color="white")+
coord_polar("y",start=0)+
geom_text(aes(y=lab.ypos,label=prop),color="white")+
scale_fill_manual(values = mycols)+
theme_void()+
labs(title="Marriage Participany by Race") Membuat lubang dalam diagram pie
library(ggplot2)
library(scales)
#Membuat diagram donat
ggplot(plotdata,aes(x=2,y=prop,fill=race))+
geom_bar(stat="identity",color="white")+
coord_polar(theta="y",start=0)+
geom_text(aes(y=lab.ypos,label=prop),color="white")+
scale_fill_manual(values = mycols)+
theme_void()+
xlim(0.5,2.5)+
labs(title="Marriage Participany by Race") Menambahkan label dan menghapus legenda
library(ggplot2)
library(scales)
#tambahkan label persen
plotdata$percent<-paste0(plotdata$race,"\n",
round(plotdata$prop),"%")
#membuat diagram donut dalam persen
ggplot(plotdata,aes(x=2,y=prop,fill=race))+
geom_bar(stat="identity",color="white")+
coord_polar(theta="y",start=0)+
geom_text(aes(y=lab.ypos,label=percent),color="white")+
scale_fill_manual(values = mycols)+
theme_void()+
xlim(0.5,2.5)+
labs(title="Marriage Participany by Race") ## Peta Pohon
library(ggplot2)
library(treemapify)
library(scales)
plotdata <-Marriage %>%
count(officialTitle)
ggplot(plotdata,
aes(fill=officialTitle,
area=n))+
geom_treemap()+
labs(title="Marriage Participants by Officiate") Menggunakan Label
ggplot(plotdata,
aes(fill=officialTitle,
area=n,
label=officialTitle))+
geom_treemap()+
geom_treemap_text(colour="white",
place="centre")+
labs(title="Marriage Participants by Officiate")+
theme(legend.position="none") # Variabel Kontinu ## Histogram
library(ggplot2)
ggplot(Marriage,aes(x=age))+
geom_histogram(fill="cornflowerblue",
color="white",bins=20)+
theme_minimal()+
labs(title="Marriage Participants by Age (Basic)",
x="Age")library(ggplot2)
library(scales)
ggplot(Marriage,
aes(x=age,
y=..count../sum(..count..)))+
geom_histogram(fill="cornflowerblue",
color="white",
binwidth = 5)+
theme_minimal()+
labs(title = "Marriage Participants by Age(Alternative Bins and Binwidth",
y="Percent",
x="Age")+
scale_y_continuous(labels=percent) Plot Densitas Kernel
library(ggplot2)
ggplot(Marriage,aes(x=age))+
geom_density(fill="indianred3")+
theme_minimal()+
labs(title="Marriage Participants by Age") Parameter Penghalusan
library(ggplot2)
bw.nrd0(Marriage$age)## [1] 5.181946
ggplot(Marriage,aes(x=age))+
geom_density(fill="deepskyblue",
bw=1)+
theme_minimal()+
labs(title="Participants by Age",
subtitle = "bandwidth=1") ## Diagram Titik
library(ggplot2)
ggplot(Marriage,aes(x=age))+
geom_dotplot(fill="gold",
color="azure4",
binwidth = 2)+
theme_minimal()+
labs(title="Participants by age",
y="Proportion",
x="Age") # Data Bivariat ## Kategorikal Vs Kategorikal Diagram Batang Bertumpuk
library(ggplot2)
mpg$drv<-factor(mpg$drv,
levels=c("f","r","4"),
labels=c("front-wheel","rear-wheel","4-wheel"))
#membuat diagram batang bertumpuk
ggplot(mpg,
aes(x=class,
fill=drv))+
geom_bar(position="fill")+
theme_minimal()+
labs(y="proportion") Diagram batang yang dikelompokkan
library(ggplot2)
ggplot(mpg,aes(x=class,fill=drv))+
theme_minimal()+
geom_bar(position=position_dodge(preserve="single")) Diagram Batang Tersegmentasi
library(dplyr)
library(ggplot2)
library(scales)
#membuat ringkasan dataset
plotdata<-mpg %>%
group_by(class,drv)%>%
dplyr::summarise(n=n())%>%
mutate(pct=n/sum(n),
lbl=scales::percent(pct))## `summarise()` has grouped output by 'class'. You can override using the `.groups` argument.
#membuat diagram batang tersegmentasi
#menambahkan label untuk setiap segmen
ggplot(plotdata,
aes(x=factor(class),
y=pct,
fill=factor(drv)))+
geom_bar(stat="identity",
position="fill")+
scale_y_continuous(breaks=seq(0,1,.2),
label=percent)+
geom_text(aes(label=lbl),
size=3,
position=position_stack(vjust=0.5))+
scale_fill_brewer(palette = "Set2")+
theme_minimal()+
labs(y="Percent",
fill="Drive Train",
x="Class",
title="Automobile Drive by Class")theme_minimal()## List of 93
## $ line :List of 6
## ..$ colour : chr "black"
## ..$ size : num 0.5
## ..$ linetype : num 1
## ..$ lineend : chr "butt"
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ rect :List of 5
## ..$ fill : chr "white"
## ..$ colour : chr "black"
## ..$ size : num 0.5
## ..$ linetype : num 1
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ text :List of 11
## ..$ family : chr ""
## ..$ face : chr "plain"
## ..$ colour : chr "black"
## ..$ size : num 11
## ..$ hjust : num 0.5
## ..$ vjust : num 0.5
## ..$ angle : num 0
## ..$ lineheight : num 0.9
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ title : NULL
## $ aspect.ratio : NULL
## $ axis.title : NULL
## $ axis.title.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 2.75points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 2.75points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.bottom : NULL
## $ axis.title.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : num 90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.75points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.y.left : NULL
## $ axis.title.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : num -90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.75points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : chr "grey30"
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 2.2points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 2.2points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.bottom : NULL
## $ axis.text.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 1
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.2points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.y.left : NULL
## $ axis.text.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.2points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.ticks : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.ticks.x : NULL
## $ axis.ticks.x.top : NULL
## $ axis.ticks.x.bottom : NULL
## $ axis.ticks.y : NULL
## $ axis.ticks.y.left : NULL
## $ axis.ticks.y.right : NULL
## $ axis.ticks.length : 'simpleUnit' num 2.75points
## ..- attr(*, "unit")= int 8
## $ axis.ticks.length.x : NULL
## $ axis.ticks.length.x.top : NULL
## $ axis.ticks.length.x.bottom: NULL
## $ axis.ticks.length.y : NULL
## $ axis.ticks.length.y.left : NULL
## $ axis.ticks.length.y.right : NULL
## $ axis.line : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.line.x : NULL
## $ axis.line.x.top : NULL
## $ axis.line.x.bottom : NULL
## $ axis.line.y : NULL
## $ axis.line.y.left : NULL
## $ axis.line.y.right : NULL
## $ legend.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
## ..- attr(*, "unit")= int 8
## $ legend.spacing : 'simpleUnit' num 11points
## ..- attr(*, "unit")= int 8
## $ legend.spacing.x : NULL
## $ legend.spacing.y : NULL
## $ legend.key : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.key.size : 'simpleUnit' num 1.2lines
## ..- attr(*, "unit")= int 3
## $ legend.key.height : NULL
## $ legend.key.width : NULL
## $ legend.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.text.align : NULL
## $ legend.title :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.title.align : NULL
## $ legend.position : chr "right"
## $ legend.direction : NULL
## $ legend.justification : chr "center"
## $ legend.box : NULL
## $ legend.box.just : NULL
## $ legend.box.margin : 'margin' num [1:4] 0cm 0cm 0cm 0cm
## ..- attr(*, "unit")= int 1
## $ legend.box.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.box.spacing : 'simpleUnit' num 11points
## ..- attr(*, "unit")= int 8
## $ panel.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ panel.border : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ panel.spacing : 'simpleUnit' num 5.5points
## ..- attr(*, "unit")= int 8
## $ panel.spacing.x : NULL
## $ panel.spacing.y : NULL
## $ panel.grid :List of 6
## ..$ colour : chr "grey92"
## ..$ size : NULL
## ..$ linetype : NULL
## ..$ lineend : NULL
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ panel.grid.major : NULL
## $ panel.grid.minor :List of 6
## ..$ colour : NULL
## ..$ size : 'rel' num 0.5
## ..$ linetype : NULL
## ..$ lineend : NULL
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ panel.grid.major.x : NULL
## $ panel.grid.major.y : NULL
## $ panel.grid.minor.x : NULL
## $ panel.grid.minor.y : NULL
## $ panel.ontop : logi FALSE
## $ plot.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ plot.title :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 1.2
## ..$ hjust : num 0
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 5.5points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.title.position : chr "panel"
## $ plot.subtitle :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 5.5points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.caption :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 0.8
## ..$ hjust : num 1
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 5.5points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.caption.position : chr "panel"
## $ plot.tag :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 1.2
## ..$ hjust : num 0.5
## ..$ vjust : num 0.5
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.tag.position : chr "topleft"
## $ plot.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
## ..- attr(*, "unit")= int 8
## $ strip.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ strip.background.x : NULL
## $ strip.background.y : NULL
## $ strip.placement : chr "inside"
## $ strip.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : chr "grey10"
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 4.4points 4.4points 4.4points 4.4points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ strip.text.x : NULL
## $ strip.text.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : num -90
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ strip.switch.pad.grid : 'simpleUnit' num 2.75points
## ..- attr(*, "unit")= int 8
## $ strip.switch.pad.wrap : 'simpleUnit' num 2.75points
## ..- attr(*, "unit")= int 8
## $ strip.text.y.left :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : num 90
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## - attr(*, "class")= chr [1:2] "theme" "gg"
## - attr(*, "complete")= logi TRUE
## - attr(*, "validate")= logi TRUE
Plot Mosaik
tbl<-xtabs(Freq~Survived+Class+Sex, Titanic)
ftable(tbl)## Sex Male Female
## Survived Class
## No 1st 118 4
## 2nd 154 13
## 3rd 422 106
## Crew 670 3
## Yes 1st 62 141
## 2nd 25 93
## 3rd 88 90
## Crew 192 20
#Membuat plot mosaik dari tabel
library(vcd)## Loading required package: grid
mosaic(tbl,main="Titanic data")mosaic(tbl,
shade=TRUE,
legend=TRUE,
labelin_args=list(set_varnames=c(Sex="Gender",
Survived="Survived",
Class="Passenger Class")),
set_labels=list(Survived=c("No","Yes",
Class=c("1st","2nd","3rd","Crew"),
Sex=c("F","M")),
main="Titanic data")
) # Kontinu Vs Kontinu Plot Sebaran
library(ggplot2)
library(hrbrthemes)## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
## Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
## if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
#Membuat data
d1<-data.frame(x=seq(1,100),
y=rnorm(100),
name="No trend")
d2<-d1%>%
mutate(y=x*10 +rnorm(100,sd=60))%>%
mutate(name="Linear Relationship")
d3<-d1%>%
mutate(y=x^2+rnorm(100,sd=140))%>%
mutate(name="Square")
d4<-data.frame(x=seq(1,10,0.1),
y=sin(seq(1,10,0.1))+
rnorm(91,sd=0.6))%>%
mutate(name="Sin")
don<-do.call(rbind,list(d1,d2,d3,d4))
#Plot
don%>%
ggplot(aes(x=x,y=y))+
geom_point(color="#69b3a2",alpha=0.8)+
theme_ipsum()+
facet_wrap(~name, scale="free")## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
library(ggplot2)
library(scales)
data(Salaries, package="carData")
#plot sebaran yang ditingkatkan
ggplot(Salaries,
aes(x=yrs.since.phd,
y=salary))+
geom_point(color="cornflowerblue",
size=2,
alpha=.8)+
scale_y_continuous(label=scales::dollar,
limits=c(50000,250000))+
scale_x_continuous(breaks=seq(0,60,10),
limits = c(0,60))+
theme_minimal()+
labs(x="Years Since PhD",
y="",
title="Experience Vs. Salary",
subtitle = "9-months salary for 2008-2009") Plot Sebaran Menyesuaikan Garis
library(ggplot2)
ggplot(Salaries,
aes(x=yrs.since.phd,
y=salary)) +
geom_point(color="cornflowerblue")+
geom_smooth(method="lm", color="brown1")+
theme_minimal()+
labs(x="Years Since PhD",
y="",
title="Experience Vs. Salary",
subtitle = "9-month salary for 2008-2009")## `geom_smooth()` using formula 'y ~ x'
library(ggplot2)
ggplot(Salaries,
aes(x=yrs.since.phd,
y=salary))+
geom_point(color="cornflowerblue",
size=2,
alpha=1)+
geom_smooth(size=1,
color="green")+
scale_y_continuous(label= scales::dollar,
limits=c(50000,250000))+
scale_x_continuous(breaks=seq(0,60,10),
limits=c(0,60))+
theme_minimal()+
labs(x="Years Since PhD",
y="",
title = "Experience vs. Salary",
subtitle="9-month salary for 2008-2009")## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
theme_minimal()## List of 93
## $ line :List of 6
## ..$ colour : chr "black"
## ..$ size : num 0.5
## ..$ linetype : num 1
## ..$ lineend : chr "butt"
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ rect :List of 5
## ..$ fill : chr "white"
## ..$ colour : chr "black"
## ..$ size : num 0.5
## ..$ linetype : num 1
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ text :List of 11
## ..$ family : chr ""
## ..$ face : chr "plain"
## ..$ colour : chr "black"
## ..$ size : num 11
## ..$ hjust : num 0.5
## ..$ vjust : num 0.5
## ..$ angle : num 0
## ..$ lineheight : num 0.9
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ title : NULL
## $ aspect.ratio : NULL
## $ axis.title : NULL
## $ axis.title.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 2.75points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 2.75points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.bottom : NULL
## $ axis.title.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : num 90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.75points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.y.left : NULL
## $ axis.title.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : num -90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.75points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : chr "grey30"
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 2.2points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 2.2points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.bottom : NULL
## $ axis.text.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 1
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.2points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.y.left : NULL
## $ axis.text.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.2points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.ticks : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.ticks.x : NULL
## $ axis.ticks.x.top : NULL
## $ axis.ticks.x.bottom : NULL
## $ axis.ticks.y : NULL
## $ axis.ticks.y.left : NULL
## $ axis.ticks.y.right : NULL
## $ axis.ticks.length : 'simpleUnit' num 2.75points
## ..- attr(*, "unit")= int 8
## $ axis.ticks.length.x : NULL
## $ axis.ticks.length.x.top : NULL
## $ axis.ticks.length.x.bottom: NULL
## $ axis.ticks.length.y : NULL
## $ axis.ticks.length.y.left : NULL
## $ axis.ticks.length.y.right : NULL
## $ axis.line : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.line.x : NULL
## $ axis.line.x.top : NULL
## $ axis.line.x.bottom : NULL
## $ axis.line.y : NULL
## $ axis.line.y.left : NULL
## $ axis.line.y.right : NULL
## $ legend.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
## ..- attr(*, "unit")= int 8
## $ legend.spacing : 'simpleUnit' num 11points
## ..- attr(*, "unit")= int 8
## $ legend.spacing.x : NULL
## $ legend.spacing.y : NULL
## $ legend.key : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.key.size : 'simpleUnit' num 1.2lines
## ..- attr(*, "unit")= int 3
## $ legend.key.height : NULL
## $ legend.key.width : NULL
## $ legend.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.text.align : NULL
## $ legend.title :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.title.align : NULL
## $ legend.position : chr "right"
## $ legend.direction : NULL
## $ legend.justification : chr "center"
## $ legend.box : NULL
## $ legend.box.just : NULL
## $ legend.box.margin : 'margin' num [1:4] 0cm 0cm 0cm 0cm
## ..- attr(*, "unit")= int 1
## $ legend.box.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.box.spacing : 'simpleUnit' num 11points
## ..- attr(*, "unit")= int 8
## $ panel.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ panel.border : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ panel.spacing : 'simpleUnit' num 5.5points
## ..- attr(*, "unit")= int 8
## $ panel.spacing.x : NULL
## $ panel.spacing.y : NULL
## $ panel.grid :List of 6
## ..$ colour : chr "grey92"
## ..$ size : NULL
## ..$ linetype : NULL
## ..$ lineend : NULL
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ panel.grid.major : NULL
## $ panel.grid.minor :List of 6
## ..$ colour : NULL
## ..$ size : 'rel' num 0.5
## ..$ linetype : NULL
## ..$ lineend : NULL
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ panel.grid.major.x : NULL
## $ panel.grid.major.y : NULL
## $ panel.grid.minor.x : NULL
## $ panel.grid.minor.y : NULL
## $ panel.ontop : logi FALSE
## $ plot.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ plot.title :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 1.2
## ..$ hjust : num 0
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 5.5points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.title.position : chr "panel"
## $ plot.subtitle :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 5.5points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.caption :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 0.8
## ..$ hjust : num 1
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 5.5points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.caption.position : chr "panel"
## $ plot.tag :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 1.2
## ..$ hjust : num 0.5
## ..$ vjust : num 0.5
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.tag.position : chr "topleft"
## $ plot.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
## ..- attr(*, "unit")= int 8
## $ strip.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ strip.background.x : NULL
## $ strip.background.y : NULL
## $ strip.placement : chr "inside"
## $ strip.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : chr "grey10"
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 4.4points 4.4points 4.4points 4.4points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ strip.text.x : NULL
## $ strip.text.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : num -90
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ strip.switch.pad.grid : 'simpleUnit' num 2.75points
## ..- attr(*, "unit")= int 8
## $ strip.switch.pad.wrap : 'simpleUnit' num 2.75points
## ..- attr(*, "unit")= int 8
## $ strip.text.y.left :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : num 90
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## - attr(*, "class")= chr [1:2] "theme" "gg"
## - attr(*, "complete")= logi TRUE
## - attr(*, "validate")= logi TRUE
Diagram Batang
library(dplyr)
library(ggplot2)
library(scales)
data(Salaries, package = "carData")
#Menghitung gaji rata-rata untuk setiap jabatan
plotdata <-Salaries %>%
group_by(rank) %>%
dplyr::summarize(mean_salary=mean(salary))
#Plot gaji rata-rata dengan cara yang lebih menarik
mycols<-c("#CD534CFF","#EFC000FF","#0073C2FF")
ggplot(plotdata,
aes(x=factor(rank,
labels=c("Assistant\nProfessor",
"Associate\nProfessor",
"Full\nProfessor")),
y=mean_salary))+
geom_bar(stat = "identity",
fill=mycols)+
geom_text(aes(label=dollar(mean_salary)),
vjust=-0.25) +
scale_y_continuous(breaks=seq(0,130000,20000),
label=dollar)+
theme_minimal()+
labs(title="Mean Salary by Rank",
subtitle="9-month academic salary for 2008-2009",
x="",
y="") Plot densitas Kernel yang dikelompokan
ggplot(Salaries,
aes(x=salary,
fill=rank))+
geom_density(alpha=0.4)+
theme_minimal()+
labs(title="Salary distribution by rank") Boxplot
mycols<-c("#CD534CFF","#EFC000FF","#0073C2FF")
ggplot(Salaries,aes(x=rank,
y=salary))+
geom_boxplot(notch=TRUE,
fill=mycols,
alpha=.7)+
theme_minimal()+
labs(title="Salary Distribution by Rank") Plot Biola
ggplot(Salaries,
aes(x=rank,
y=salary))+
geom_violin(fill="azure1")+
geom_boxplot(width=.2,
fill=mycols,
outlier.color="red",
outlier.size = 2)+
theme_minimal()+
labs(title="Salary Distribution by Rank") Plot Garis Punggung
library(dplyr)
library(ggplot2)
library(ggridges)
ggplot(mpg,
aes(x=cty,
y=class,
fill=class))+
geom_density_ridges(alpha=0.7)+
theme_ridges()+
labs("Highway mileage by auto class")+
theme(legend.position = "none")## Picking joint bandwidth of 0.929
Plot Garis
library(dplyr)
library(ggplot2)
library(ggridges)
#Menghitung rata-rata, deviasi standar,
#Kesalahan standar, dan 95% interval kepercayaan
#Berdasarkan Jabatan
plotdata<-Salaries %>%
group_by(rank,sex)%>%
dplyr::summarize(n=n(),
mean=mean(salary),
sd=sd(salary),
se=sd/sqrt(n),
ci=qt(0.975,df =n-1)*sd/sqrt(n))## `summarise()` has grouped output by 'rank'. You can override using the `.groups` argument.
#Meningkatkan plot rata-rata kesalahan
pd<-position_dodge(0.2)
ggplot(plotdata,
aes(x=factor(rank,
labels=c("Assistant\nProfessor",
"Associate\nProfessor",
"Full\nProfessor")),
y=mean,
group=sex,
color=sex))+
geom_point(position = pd,
size=3)+
geom_line(position=pd,
size=1)+
geom_errorbar(aes(ymin=mean-se,
ymax=mean+se),
width=.1,
position=pd,
size=1)+
scale_y_continuous(label=scales::dollar)+
scale_color_brewer(palette = "Set1")+
theme_minimal()+
labs(title="Mean salary by rank and sex",
subtitle = "(mean +/- standard error)",
x="",
y="",
color="Gender") Plot Strip
library(ggplot2)
library(scales)
ggplot(Salaries,
aes(y=factor(rank,
labels=c("Assistant\nProfessor",
"Associate\nProfessor",
"Full\nProfessor")),
x=salary,
color=rank))+
geom_jitter(alpha=0.7,
size=1.5)+
scale_x_continuous(label=dollar)+
labs(title="Academic Salary by Rank",
subtitle="9-month salarty for 2008-2009",
x="",
y="")+
theme_minimal()+
theme(legend.position = "none") Menggabungkan Jitter dan PlotKotak
library(ggplot2)
library(scales)
ggplot(Salaries,
aes(y=factor(rank,
labels=c("Assistant\nProfessor",
"Associate\nProfessor",
"Full\nProfessor")),
x=salary,
color=rank))+
geom_boxplot(size=1,
outlier.shape = 1,
outlier.color="black",
outlier.size = 3)+
geom_jitter(alpha=0.5,
size=.2)+
scale_x_continuous(label=dollar)+
labs(title="Academic Salary by Rank",
subtitle="9-month salarty for 2008-2009",
x="",
y="")+
theme_minimal()+
theme(legend.position = "none")+
coord_flip() Hibrid Geom Plot Jitter dan Box
library(ggplot2)
library(scales)
library(ggpol)
ggplot(Salaries,
aes(x=factor(rank,
labels=c("Assistant\nProfessor",
"Associate\nProfessor",
"Full\nProfessor")),
y=salary,
fill=rank))+
geom_boxjitter(color="black",
jitter.color="darkgrey",
errorbar.draw=TRUE)+
scale_y_continuous(label=dollar)+
labs(title="Academic Salary by Rank",
subtitle="9-month salarty for 2008-2009",
x="",
y="")+
theme_minimal()+
theme(legend.position = "none") Plot Kawanan Lebah
library(ggplot2)
library(scales)
library(ggbeeswarm)
ggplot(Salaries,
aes(x=factor(rank,
labels=c("Assistant\nProfessor",
"Associate\nProfessor",
"Full\nProfessor")),
y=salary,
color=rank))+
geom_quasirandom(alpha=0.7,
size=1.5)+
scale_y_continuous(label=dollar)+
labs(title="Academic Salary by Rank",
subtitle="9-month salarty for 2008-2009",
x="",
y="")+
theme_minimal()+
theme(legend.position = "none") Diagram Titik Cleveland
library(dplyr)
library(ggplot2)
library(scales)
library(ggbeeswarm)
library(gapminder)
data(gapminder,package="gapminder")
#Subset negara-negara Asian 2007
library(dplyr)
plotdata<-gapminder %>%
filter(continent=="Asia"&
year==2007)
#Plot Cleveland
ggplot(plotdata,
aes(x=lifeExp,
y=reorder(country,lifeExp)))+
geom_point(color="blue",
size=2)+
geom_segment(aes(x=40,
xend=lifeExp,
y=reorder(country,lifeExp),
yend=reorder(country,lifeExp)),
color="azure3")+
labs(x="Life Expectancy (years)",
y="",
title="Life Expectancy by Country",
subtitle="GapMinder data for Asia-2007")+
theme_minimal()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank()) # Data Multivariat Pengelompokan
library(carData)
library(ggplot2)
data(Salaries,package="carData")
ggplot(Salaries,aes(x=yrs.since.phd,
y=salary,
color=rank))+
geom_point()+
theme_minimal()+
labs(title="Academic salary by rank and years since degree") Menambahkan Jenis Kelamin
library(carData)
library(ggplot2)
ggplot(Salaries,aes(x=yrs.since.phd,
y=salary,
color=rank,
shape=sex))+
geom_point(size=3, alpha=.6)+
theme_minimal()+
labs(title="Academic salary by rank and years since degree") Membedakan pria dan wanita
library(carData)
library(ggplot2)
ggplot(Salaries,aes(x=yrs.since.phd,
y=salary,
color=rank,
size=yrs.service))+
geom_point(alpha=.8)+
theme_minimal()+
labs(title="Academic salary by rank and years since degree") Menambahkan garis kuadrat yang cukup
library(carData)
library(ggplot2)
ggplot(Salaries,aes(x=yrs.since.phd,
y= salary,
color=sex))+
geom_point(alpha=.4,
size=3)+
geom_smooth(se=FALSE,
method = "lm",
formula=y~poly(x,2),
size=1.5)+
labs(x="Years Since Ph.D",
title="Academic salary by rank and years since degree",
subtitle = "9-month salary for 2008-2009",
y="",
color="Sex")+
scale_y_continuous(label=scales::dollar)+
scale_color_brewer(palette="Set1")+
theme_minimal() Pembagian Faset
library(carData)
library(ggplot2)
ggplot(Salaries,aes(x=salary))+
geom_histogram(fill="cornflowerblue",
color="white")+
facet_wrap(~rank,ncol=1)+
theme_minimal()+
labs(title="Salary Histograms by Rank")## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Menetapkan jenis kelamin ke baris dan jabatan ke kolom
library(carData)
library(ggplot2)
ggplot(Salaries,aes(x=salary/10000))+
geom_histogram(fill="cornflowerblue",
color="white")+
facet_grid(sex~rank)+
theme_minimal()+
labs(title="Salary Histograms by Rank")## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Menggunakan warna pada pembagian faset
library(carData)
library(ggplot2)
library(dplyr)
#Menghitung rata-rata dan kesalahan standar berdasarkan
#jenis kelamin, jabatan dan disiplin ilmu
plotdata<-Salaries %>%
group_by(sex,rank,discipline)%>%
dplyr::summarize(n=n(),
mean=mean(salary),
sd=sd(salary),
se=sd/sqrt(n))## `summarise()` has grouped output by 'sex', 'rank'. You can override using the `.groups` argument.
#Membuat label yang lebih baik untuk disiplin ilmu
plotdata$discipline<-factor(plotdata$discipline,
labels=c("Theoretical",
"Applied"))
#Membuat plot
ggplot(plotdata,
aes(x=sex,
y=mean,
color=sex))+
geom_point(size=3)+
geom_errorbar(aes(ymin=mean-se,
ymax=mean+se),
width=.1)+
scale_y_continuous(breaks=seq(70000,140000,10000),
label=scales::dollar)+
facet_grid(.~rank+discipline)+
theme_bw()+
theme(legend.position = "none",
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank())+
labs(x="",
y="",
title="Nine month academic salaries by gender, discipline, and rank",
subtitle = "(Means and standard errors)")+
scale_color_brewer(palette = "Set1") Memodifikasi sumbu x memperkecil tulisan dan menyederhanakan warna background
library(gapminder)
library(ggplot2)
library(dplyr)
#Memplot harapan hidup berdasarkan tahun secara terpisah
#Untuk setiap negara di Asia
data(gapminder,package="gapminder")
#Pilih data Asia
plotdata<-dplyr::filter(gapminder,
continent=="Asia")
#Memplot harapan hidup berdasarkan tahun, untuk setiap negara
ggplot(plotdata, aes(x=year,y=lifeExp))+
geom_line(color="grey")+
geom_point(color="blue")+
facet_wrap(~country)+
theme_minimal(base_size=9)+
theme(axis.text.x = element_text(angle = 45,
hjust=1))+
labs(title="Change in Life Expectancy",
x="Year",
y="Life Expectancy")