0% found this document useful (0 votes)
10 views26 pages

Code For Ggplot

Code for ggplot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views26 pages

Code For Ggplot

Code for ggplot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10.

Code_ggplot2
# Clear all variables
rm(list = ls(all = TRUE))

# (1) Basic operations of ggplot2----


# library()----
library(quantmod) # For downloading data
library(ggplot2) # For plotting static figures
library(ggpubr) # For Arranging multiple graphs on the same page

# Prepare data for ggplot2 (dataframe)


D1<-get(getSymbols(Symbols="[Link]",from = "2017-01-01", to = "2022-12-31",
src="yahoo") )
D1<-D1[,4]; colnames(D1)<-c("TSMC")

D2 <-get(getSymbols(Symbols="[Link]",from = "2017-01-01", to = "2022-12-31",


src="yahoo") )
D2<-D2[,4]; colnames(D2)<-c("Obank")

D<-cbind(D1,D2);D<-[Link](D)
D<-[Link](Date=index(D),coredata(D))

# Your first graph ----


f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC));f1
f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC),color="red");f2

1
# (2) Different graphs in different figures----
# Generate two figures, f1 and f2.
f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC));f1
f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=Obank));f2

# ggarrange(): Arrange multiple graphs on the same page.


# (1) set a grid
# row=1; ncol=2
p1<-ggarrange(f1, f2, nrow = 1, ncol = 2);p1
# row=2; ncol=1
p2<-ggarrange(f1, f2, nrow = 2, ncol = 1);p2
# row=2; ncol=2
p3<-ggarrange(f1, f2, nrow = 2, ncol = 2);p3

# (2) set labels of the figures


# Auto-generate upper-case labels
p4<-ggarrange(f1, f2, nrow = 2, ncol = 1,labels="AUTO");p4
# Auto-generate small-case labels
p5<-ggarrange(f1, f2, nrow = 2, ncol = 1,labels="auto");p5
# set labels by users
p5<-ggarrange(f1, f2, nrow = 2, ncol = 1,labels=c("甲","乙"));p5

# (3) set the title of the page


p<-ggarrange(f1, f2, nrow = 2, ncol = 1);p
p6<-annotate_figure(p,top=text_grob("Multiple figures on one page",face = "plain", size
= 14));p6
p7<-annotate_figure(p,bottom=text_grob("Multiple figures on one page",face = "bold",
size = 20));p7
p8<-annotate_figure(p,right=text_grob("Multiple figures on one page",face = "italic", size
= 25));p8
p9<-annotate_figure(p,left=text_grob("Multiple figures on one page",face = "[Link]",
size = 10));p9

2
# (3) Modify titles, axis labels, and caption for a figure----
# library()
library(quantmod) # For downloading data
library(ggplot2) # For plotting static figures
library(ggpubr) # For Arranging multiple graphs on the same page

# Prepare data for ggplot2 (dataframe)


D<-get(getSymbols(Symbols="[Link]",from = "2017-01-01", to = "2022-12-31",
src="yahoo") )
D<-D[,4]; colnames(D)<-c("TSMC")
D<-[Link](D)
D<-[Link](Date=index(D),coredata(D))

# A: Default
f1<-ggplot(data=D)+geom_line(mapping=aes(x=Date,y=TSMC))

# B: Method 1: xlab(),ylab(),ggtitle()
f2<-ggplot(data=D)+geom_line(mapping=aes(x=Date,y=TSMC))+
xlab("DATE")+ylab("PRICE")+
ggtitle(label="Method 1: xlab(),ylab(),ggtitle()",
subtitle="2017-01-01 ~ 2022-12-31")

# C: Method 2: labs()
f3<-ggplot(data=D)+geom_line(mapping=aes(x=Date,y=TSMC))+
labs(x="DATE",
y ="PRICE",
title="Method 2:labs()",
subtitle= "2017-01-01 ~ 2022-12-31",
caption="Designed by Paul"
)

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f2,f3,nrow = 3, ncol = 1,labels="AUTO");p

3
# (4) Aesthetics of geom_line()----
# library()
library(quantmod) # For downloading data
library(ggplot2) # For plotting static figures
library(ggpubr) # For Arranging multiple graphs on the same page

# Prepare data for ggplot2 (dataframe)


D<-get(getSymbols(Symbols="[Link]",from = "2017-01-01", to = "2022-12-31",
src="yahoo") )
D<-D[,4]; colnames(D)<-c("TSMC")
D<-[Link](D)
D<-[Link](Date=index(D),coredata(D))

# Line color----
# A: Default
f1<-ggplot(data=D)+geom_line(mapping=aes(x=Date,y=TSMC))+
xlab("DATE")+ylab("PRICE")+ggtitle("Default color:Black")

# B: color="red"
f2<-ggplot(data=D)+geom_line(mapping=aes(x=Date,y=TSMC),color="red")+
xlab("DATE")+ylab("PRICE")+ggtitle('color="red"')

# C: color="#1E90FF"
f3<-ggplot(data=D)+geom_line(mapping=aes(x=Date,y=TSMC),color="#1E90FF")+
xlab("DATE")+ylab("PRICE")+ggtitle('color="#1E90FF"')

# Arrange multiple graphs on the same page.


p<-ggarrange(f1, f2, f3, nrow =3, ncol = 1,labels="AUTO")
p<-annotate_figure(p,top=text_grob("geom_line(...,color) ",face = "bold", size = 16));p

# Line size----

# A: Default

4
f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC))+
xlab("DATE")+ylab("PRICE")+ggtitle("Default size")

# B: Default
f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC),size=0.5)+
xlab("DATE")+ylab("PRICE")+ggtitle("size=0.5")

# C: Default
f3<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC),size=1)+
xlab("DATE")+ylab("PRICE")+ggtitle("size=1")

# D: Default
f4<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC),size=2)+
xlab("DATE")+ylab("PRICE")+ggtitle("size=2")

# Arrange multiple graphs on the same page.


p<-ggarrange(f1, f2, f3,f4, nrow =4, ncol = 1,labels="AUTO")
p<-annotate_figure(p,top=text_grob("geom_line(...,size) ",face = "bold", size = 16));p

# Line type----
# A: Default
f1<-ggplot(data=D[1:10,])+
geom_line(mapping=aes(x=Date,y=TSMC))+
xlab("DATE")+ylab("PRICE")+ggtitle("Default linetype")
# B: linetype = "solid" or 1
f2<-ggplot(data=D[1:10,])+
geom_line(mapping=aes(x=Date,y=TSMC),linetype=1)+
xlab("DATE")+ylab("PRICE")+ggtitle('linetype = "solid" or 1')
# C: linetype = "dashed" or 2
f3<-ggplot(data=D[1:10,])+

5
geom_line(mapping=aes(x=Date,y=TSMC),linetype="dashed")+
xlab("DATE")+ylab("PRICE")+ggtitle('linetype = "dashe"d or 2')
# D: linetype = "dotted" or 3
f4<-ggplot(data=D[1:10,])+
geom_line(mapping=aes(x=Date,y=TSMC),linetype=3)+
xlab("DATE")+ylab("PRICE")+ggtitle('linetype = "dotted" or 3')

# E: linetype = "dotdash" or 4
f5<-ggplot(data=D[1:10,])+
geom_line(mapping=aes(x=Date,y=TSMC),linetype="dotdash")+
xlab("DATE")+ylab("PRICE")+ggtitle('linetype = "dotdash" or 4')

# F: linetype = "longdash" or 5
f6<-ggplot(data=D[1:10,])+
geom_line(mapping=aes(x=Date,y=TSMC),linetype=5)+
xlab("DATE")+ylab("PRICE")+ggtitle('linetype = "longdash" or 5')

# G: linetype = "twodash" or 6
f7<-ggplot(data=D[1:10,])+
geom_line(mapping=aes(x=Date,y=TSMC),linetype="twodash")+
xlab("DATE")+ylab("PRICE")+ggtitle('linetype = "twodash" or 6')

# H: linetype = "blank 0" or 0


f8<-ggplot(data=D[1:10,])+
geom_line(mapping=aes(x=Date,y=TSMC),linetype=0)+
xlab("DATE")+ylab("PRICE")+ggtitle('linetype = "blank" or 0')

# Arrange multiple graphs on the same page.


p<-ggarrange(f1, f2, f3, f4, f5, f6, f7, f8, nrow =4, ncol = 2,labels="AUTO")
p<-annotate_figure(p,top=text_grob("geom_line(...,type) ",face = "bold", size = 16));p

6
# Line transparency----

# A: Default
f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC))+
xlab("DATE")+ylab("PRICE")+ggtitle("Default alpha")
# B: alpha=1
f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC),alpha=1)+
xlab("DATE")+ylab("PRICE")+ggtitle("alpha=1")

# C: alpha=0.5
f3<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC),alpha=0.5)+
xlab("DATE")+ylab("PRICE")+ggtitle("alpha=0.5")

# D: alpha=0
f4<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC),alpha=0)+
xlab("DATE")+ylab("PRICE")+ggtitle("alpha=0")

# Arrange multiple graphs on the same page.


p<-ggarrange(f1, f2, f3, f4, nrow =4, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("geom_line(...,alpha) ",face = "bold", size = 16));p

# Exercise----
f<-ggplot(data=D)+
# Set data and aesthetics
geom_line(mapping=aes(x=Date,y=TSMC),
color="#1E90FF",
size=1,
linetype="dashed")+
# Set titles, subtitle, and caption
labs(x="DATE",

7
y ="PRICE",
title="The price of TSMC",
subtitle= "2017-01-01 ~ 2022-12-31",
caption="Designed by Paul"
);f

8
# (5) Different graphs in one figure -----
# library()
library(quantmod) # For downloading data
library(ggplot2) # For plotting static figures
library(ggpubr) # For Arranging multiple graphs on the same page

# Prepare data for ggplot2 (dataframe)


D1<-get(getSymbols(Symbols="[Link]",from = "2017-01-01", to = "2022-12-31",
src="yahoo") )
D1<-D1[,4]; colnames(D1)<-c("TSMC")

D2<-get(getSymbols(Symbols="[Link]",from = "2017-01-01", to = "2022-12-31",


src="yahoo") )
D2<-D2[,4]; colnames(D2)<-c("UMC")

D<-cbind(D1,D2);D<-[Link](D)
D<-[Link](Date=index(D),coredata(D))

# Step 1: Distinguish graphs by aesthetics----

# Distinguish graphs by color


f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
labs(x="Date",y ="Price", title="Distinguish graphs by color");f1

# Distinguish graphs by size


f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,size="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,size="UMC Price"))+
labs(x="Date",y ="Price", title="Distinguish graphs by size");f2

9
# Distinguish graphs by linetype
f3<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,linetype="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,linetype="UMC Price"))+
labs(x="Date",y ="Price", title="Distinguish graphs by linetype");f3

# Distinguish graphs by alpha


f4<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,alpha="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,alpha="UMC Price"))+
labs(x="Date",y ="Price", title="Distinguish graphs by alpha");f4

# Step 2: Modify aesthetics----


# scale_color_manual()----
# values----
f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
labs(x="Date",y ="Price", title="Default");f1

f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","red"))+
labs(x="Date",y ="Price", title="Set the colors for different graphs")

# Arrange multiple graphs on the same page.


p<-ggarrange(f1, f2, nrow =2, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("Modify aesthetics - line color",face = "bold", size
= 16));p

10
# name----
f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"))+
labs(x="Date",y ="Price", title="Default")

f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company")+
labs(x="Date",y ="Price", title="Set the name of legend table");

f3<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="")+
labs(x="Date",y ="Price", title="No name of legend table")

# Arrange multiple graphs on the same page.


p<-ggarrange(f1, f2, f3,nrow =3, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("Modify aesthetics - name of legend table",face =
"bold", size = 16));p

# breaks----
f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company"
)+labs(x="Date",y ="Price", title="Default")

11
f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company",
breaks=c("TSMC Price","UMC Price")
)+labs(x="Date",y ="Price", title='breaks=c("TSMC
Price","UMC Price")')
f3<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company",
breaks=c("TSMC Price","")
)+labs(x="Date",y ="Price", title='breaks=c("TSMC
Price","")')

f4<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company",
breaks=c("","UMC Price")
)+labs(x="Date",y ="Price", title='breaks=c("","UMC
Price")')

f5<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company",
breaks=c("","")
)+ labs(x="Date",y ="Price", title='breaks=c("","")')

12
f6<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company",
breaks=""
)+labs(x="Date",y ="Price", title='breaks=""')

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f2,f3,f4,f5,f6,nrow =3, ncol = 2,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("Modify aesthetics - Show or not show the
legends",face = "bold", size = 16));p

# labels----
f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company"
)+ labs(x="Date",y ="Price", title='Default')
f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company",
labels=c("A:TSMC","B:UMC")
)+labs(x="Date",y ="Price",
title='labels=c("A:TSMC","B:UMC")')
f3<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company",
labels=c("TSMC","")

13
)+labs(x="Date",y ="Price", title='labels=c("TSMC","")')
f4<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company",
labels=c("","UMC")
)+labs(x="Date",y ="Price", title='labels=c("","UMC")')
f5<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company",
labels=c("","")
)+labs(x="Date",y ="Price", title='labels=c("","")')
f6<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company",
labels=""
)+labs(x="Date",y ="Price", title='labels=""')

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f2,f3,f4,f5,f6,nrow =3, ncol = 2,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("Modify aesthetics - Set the labels of the
legends",face = "bold", size = 16));p

# guide----
f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company"

14
)+
labs(x="Date",y ="Price", title='Default')

f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,color="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,color="UMC Price"))+
scale_color_manual(values=c("blue","#009100"),
name="Company",
guide=NULL
)+
labs(x="Date",y ="Price", title='guide=NULL')

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f2,nrow =2, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("Modify aesthetics - legend table or not",face =
"bold", size = 16));p

# scale_size_manual()----
# values
f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,size="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,size="UMC Price"))+
labs(x="Date",y ="Price", title="Default")

f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,size="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,size="UMC Price"))+
scale_size_manual(values=c(1,0.3))+
labs(x="Date",y ="Price", title="Set the sizes of the graphs")
# Arrange multiple graphs on the same page.
p<-ggarrange(f1,f2,nrow =2, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("scale_size_manual()",face = "bold", size = 16));p

15
# scale_linetype_manual()----
# values
f1<-ggplot(data=D[1:10,])+
geom_line(mapping=aes(x=Date,y=TSMC,linetype="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,linetype="UMC Price"))+
labs(x="Date",y ="Price", title="Default")
f2<-ggplot(data=D[1:10,])+
geom_line(mapping=aes(x=Date,y=TSMC,linetype="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,linetype="UMC Price"))+
scale_linetype_manual(values=c(2,6))+
labs(x="Date",y ="Price", title="Set the linetype of the group graphs")
# Arrange multiple graphs on the same page.
p<-ggarrange(f1,f2,nrow =2, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("scale_linetype_manual()",face = "bold", size =
16));p

# scale_alpha_manual()----
# values
f1<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,alpha="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,alpha="UMC Price"))+
labs(x="Date",y ="Price", title="Default")

f2<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC,alpha="TSMC Price"))+
geom_line(mapping=aes(x=Date,y=UMC,alpha="UMC Price"))+
scale_alpha_manual(values=c(0.5,1))+
labs(x="Date",y ="Price", title="Set the alpha values of the group graphs")
# Arrange multiple graphs on the same page.
p<-ggarrange(f1,f2,nrow =2, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("scale_alpha_manual()",face = "bold", size = 16));p

16
# (6&7) scale_axis_type()----
# library()
library(quantmod) # For downloading data
library(ggplot2) # For plotting static figures
library(ggpubr) # For Arranging multiple graphs on the same page

# Prepare data for plotting


D1<-get(getSymbols(Symbols="[Link]",from = "2017-01-01", to = "2022-12-31",
src="yahoo") )
D1<-D1[,4]; colnames(D1)<-c("TSMC")
D2<-get(getSymbols(Symbols="[Link]",from = "2017-01-01", to = "2022-12-31",
src="yahoo") )
D2<-D2[,4]; colnames(D2)<-c("UMC")
D3<-get(getSymbols(Symbols="[Link]",from = "2017-01-01", to = "2022-12-31",
src="yahoo") )
D3<-D3[,4]; colnames(D3)<-c("Obank")
D<-cbind(D1,D2,D3);D<-[Link](D)
r<-(D-lag(D))/lag(D);r<-[Link](r)
r<-[Link](Date=index(r),coredata(r))

# (6) scale_y_continuous-----

# Default axis
f1<-ggplot(data=r)+
geom_line(mapping=aes(x=Date,y=TSMC,colour="TSMC price"))+
geom_line(mapping=aes(x=Date,y=UMC,colour="UMC price"))+
labs(x="Date", title="Default axis ")

# name setting
f2<-ggplot(data=r)+
geom_line(mapping=aes(x=Date,y=TSMC,colour="TSMC price"))+
geom_line(mapping=aes(x=Date,y=UMC,colour="UMC price"))+
scale_y_continuous(name="Price",
)+

17
labs(x="Date", title='name="Price"')

f3<-ggplot(data=r)+
geom_line(mapping=aes(x=Date,y=TSMC,colour="TSMC price"))+
geom_line(mapping=aes(x=Date,y=UMC,colour="UMC price"))+
scale_y_continuous(name="",
)+
labs(x="Date", title='name=""')

# limits setting
f4<-ggplot(data=r)+
geom_line(mapping=aes(x=Date,y=TSMC,colour="TSMC price"))+
geom_line(mapping=aes(x=Date,y=UMC,colour="UMC price"))+
scale_y_continuous(name="Price",
limits=c(-0.2, 0.2),
)+
labs(x="Date", title="limits=c(-0.2, 0.2)")

# breaks setting
f5<-ggplot(data=r)+
geom_line(mapping=aes(x=Date,y=TSMC,colour="TSMC price"))+
geom_line(mapping=aes(x=Date,y=UMC,colour="UMC price"))+
scale_y_continuous(name="Price",
limits=c(-0.2, 0.2),
breaks=seq(from=-0.2,to=0.2,by=0.05)
)+
labs(x="Date", title="breaks=seq(from=-0.2,to=0.2,by=0.05)")

# labels setting
f6<-ggplot(data=r)+
geom_line(mapping=aes(x=Date,y=TSMC,colour="TSMC price"))+
geom_line(mapping=aes(x=Date,y=UMC,colour="UMC price"))+
scale_y_continuous(name="Price (%)",
limits=c(-0.2, 0.2),

18
breaks=seq(from=-0.2,to=0.2,by=0.05),
labels=seq(from=-0.2,to=0.2,by=0.05)*100
)+
labs(x="Date", title='name="Price (%)"";labels=seq(from=-0.2,to=0.2,by=0.05)*100')

# guide setting
f7<-ggplot(data=r)+
geom_line(mapping=aes(x=Date,y=TSMC,colour="TSMC price"))+
geom_line(mapping=aes(x=Date,y=UMC,colour="UMC price"))+
scale_y_continuous(guide = NULL)+
labs(x="Date", title="guide = NULL")

f8<-ggplot(data=r)+
geom_line(mapping=aes(x=Date,y=TSMC,colour="TSMC price"))+
geom_line(mapping=aes(x=Date,y=UMC,colour="UMC price"))+
scale_y_continuous(name="",guide = NULL

)+
labs(x="Date", title=' " " ; guide = NULL')

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f2,f3,f4,f5,f6,f7,f8,nrow =4, ncol = 2,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("scale_y_continuous()",face = "bold", size = 16));p

# (7) scale_x_date-----
# Default axis
f1<-ggplot(data=r)+
geom_line(mapping=aes(x=Date,y=TSMC,colour="TSMC"))+
geom_line(mapping=aes(x=Date,y=UMC,colour="UMC price"))+
scale_y_continuous(name="Return (%)",
limits=c(-0.2, 0.2),
breaks=seq(from=-0.2,to=0.2,by=0.05),
labels=seq(from=-0.2,to=0.2,by=0.05)*100
)+

19
labs(x="Date", title='Default')
# guide
f2<-f1+scale_x_date(guide = NULL)+labs(x="Date", title='guide=NULL')
# name
f3<-f1+scale_x_date(name="DATE" )+labs(x="Date", title='name="DATE"')
# Arrange multiple graphs on the same page.
p<-ggarrange(f1,f2,f3,nrow =3, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("scale_x_date()",face = "bold", size = 16));p

# limits
f4<-f1+scale_x_date(name="DATE",
limits=[Link](c("2016-01-01","2022-12-31"))
)+labs(x="Date", title='limits')
# date_breaks
f5<-f1+
scale_x_date(name="DATE",
limits=[Link](c("2016-01-01","2022-12-31")),
date_breaks="1 year"
)+
labs(x="Date", title='date_breaks')
# date_minor_breaks
f6<-f1+
scale_x_date(name="DATE",
limits=[Link](c("2016-01-01","2022-12-31")),
date_breaks="1 year",
date_minor_breaks="3 month"
)+
labs(x="Date", title='date_minor_breaks')

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f4,f5,f6,nrow =4, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("scale_x_date()",face = "bold", size = 16));p

# date_labels

20
f7<-f1+
scale_x_date(date_labels="%y")+
labs(x="Date", title='%y')

f8<-f1+
scale_x_date(date_labels="%Y-%m")+
labs(x="Date", title="%Y-%m")

f9<-f1+
scale_x_date(date_labels="%Y-%m-%d")+
labs(x="Date", title="%Y-%m-%d")

f10<-f1+
scale_x_date(date_labels="%Y/%B")+
labs(x="Date", title="%Y-%B")

f11<-f1+
scale_x_date(date_labels="%Y/%b")+
labs(x="Date", title="%Y-%b")

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f7,f8,f9,f10,f11,nrow =3, ncol = 2,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("scale_x_date()",face = "bold", size = 16));p

21
# (8) Modify theme----
# library()
library(quantmod) # For downloading data
library(ggplot2) # For plotting static figures
library(ggpubr) # For Arranging multiple graphs on the same page
library(ggThemeAssist) # For modifying theme

# Prepare data for plotting


D1<-get(getSymbols(Symbols="[Link]",from = "2017-01-01", to = "2022-12-31",
src="yahoo") )
D1<-D1[,4]; colnames(D1)<-c("TSMC")
D2<-get(getSymbols(Symbols="[Link]",from = "2017-01-01", to = "2022-12-31",
src="yahoo") )
D2<-D2[,4]; colnames(D2)<-c("UMC")
D<-cbind(D1,D2);D<-[Link](D)
D<-[Link](Date=index(D),coredata(D))

# Modify theme----
f<-ggplot(data=D)+
geom_line(mapping=aes(x=Date,y=TSMC))
f1<-f+theme_gray() #
f2<-f+theme_bw()
f3<-f+theme_linedraw()
f4<-f+theme_light()
f5<-f+theme_dark()
f6<-f+theme_minimal()
f7<-f+theme_classic()
f8<-f+theme_void()

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f2,f3,f4,f5,f6,f7,f8,nrow =4, ncol = 2,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("Different Themes",face = "bold", size = 16));p

f + theme([Link] = element_rect(fill = "gray87"))

22
# (9) geom_col()----

# library()
library(quantmod) # For downloading data
library(ggplot2) # For plotting static figures
library(ggpubr) # For Arranging multiple graphs on the same page

# Prepare data for plotting


M<-[Link](Q=paste0("Q",1:4),V=c(43,30,28,44))

# Using geom_col() to create a bar chart


# Default
f1<-ggplot(data=M) +
geom_col(aes(x=Q, y=V))+
labs(x="Quarter",y ="Volume", title="Default")

# Modify the widths of bars


f2<-ggplot(data=M)+
geom_col(aes(x=Q, y=V),width=0.5)+
labs(x="Quarter",y ="Volume", title="width=40")

# Modify color and fill


f3<-ggplot(data=M)+
geom_col(aes(x=Q, y=V),
color="red",
fill="#BBFFBB")+
labs(x="Quarter",y ="Volume", title='color="red",fill="#BBFFB"')

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f2,f3,nrow =3, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("geom_col()",face = "bold", size = 16));p

# Adding labels to a bar chart by using geom_text()

23
# Default (No labels)
f1<-ggplot(data=M)+
geom_col(aes(x=Q, y=V),
color="red",
fill="#BBFFBB")+
labs(x="Quarter",y ="Volume", title="Default (No labels)")

# label
f2<-f1+
geom_text(aes(x=Q, y=V, label=V))+
labs(x="Quarter",y ="Volume", title="label=V")

# size, color
f3<-f1+
geom_text(aes(x=Q, y=V,label=V),size=7,color="blue")+
labs(x="Quarter",y ="Volume", title='size=7,color="blue"')

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f2,f3,nrow =3, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("geom_col()+geom_text()",face = "bold", size =
16));p

# Positions of labels

# Default
f<-ggplot(data=M)+
geom_col(aes(x=Q, y=V),
color="red",
fill="#BBFFBB")+
labs(x="Quarter",y ="Volume", title="Default (No labels)")

24
# vjust
f1<-f+
geom_text(aes(x=Q, y=V, label=V),vjust=-1)+
labs(x="Quarter",y ="Volume", title="vjust=-1")
f2<-f+
geom_text(aes(x=Q, y=V, label=V),vjust=-0.5)+
labs(x="Quarter",y ="Volume", title="vjust=-0.5")
f3<-f+
geom_text(aes(x=Q, y=V, label=V),vjust=0)+
labs(x="Quarter",y ="Volume", title="vjust=0 (default)")
f4<-f+
geom_text(aes(x=Q, y=V, label=V),vjust=0.5)+
labs(x="Quarter",y ="Volume", title="vjust=0.5")
f5<-f+
geom_text(aes(x=Q, y=V, label=V),vjust=1)+
labs(x="Quarter",y ="Volume", title="vjust=1")

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f2,f3,f4,f5,nrow =1, ncol = 5,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("geom_col()+geom_text()",face = "bold", size =
16));p
# hjust

f1<-f+
geom_text(aes(x=Q, y=V, label=V),hjust=-0.5)+
labs(x="Quarter",y ="Volume", title="hjust=-0.5")
f2<-f+
geom_text(aes(x=Q, y=V, label=V),hjust=0)+
labs(x="Quarter",y ="Volume", title="hjust=0")
f3<-f+
geom_text(aes(x=Q, y=V, label=V),hjust=0.5)+
labs(x="Quarter",y ="Volume", title="hjust=0.5 (default)")
f4<-f+

25
geom_text(aes(x=Q, y=V, label=V),hjust=1)+
labs(x="Quarter",y ="Volume", title="hjust=1")
f5<-f+
geom_text(aes(x=Q, y=V, label=V),hjust=1.5)+
labs(x="Quarter",y ="Volume", title="hjust=1.5")

# Arrange multiple graphs on the same page.


p<-ggarrange(f1,f2,f3,f4,f5, nrow =5, ncol = 1,labels="AUTO");p
p<-annotate_figure(p,top=text_grob("geom_col()+geom_text()",face = "bold", size =
16));p

26

You might also like