Chapter 2 Getting started with R
The R language is used in this project. It is applied to analyze and visualize the electricity generation, electricity demand, electricity profiles. Therefore, R language is needed to install in your computer. R can be run on several operating system.
There is an integrated development environment (IDE) for R language. The IDE is an excellent text editor with syntax highlight and project viewer. The Rstudio IDE can be downloaded here.
2.1 Project initial settings
2.1.1 Installing packages
Required packages can be downloaded by using the command install.packages(). A R code chunk is given below.
2.1.2 Packages settings
Before the project starts, packages are required as follows:
- A
tidyversepackage is a bunch of packages to handle with a data analysis (for more information click here). It consists of data manipulation and visualization as follows:- A
ggplot2package is for data visualization. - A
dplyrpackage is for data manipulation. - A
tidyrpackage is for data tidying. - A
readrpackage is for data import. - A
purrrpackage is for functional programming. - A
tibblepackage is a modern reimagining of the data.frame. - A
stringrpackage is for strings. - A
forcatspackage is for factors. - A
lubridatepackage is for date and time.
- A
- A
readxlpackage is used for read data from Excel into R. - A
scalespackage is applied to scale plots in the ggplot2 package. - A
gluepackage interprets strings literal. The package embeds R expressions and inserts into argument string. - A
knitrpackage is a lightweight API’s designed to give users full control of the output without heavy coding work. In this project, the package is used for making a HTML. - A
plotlypackage is an alternative package to aggplot2package. It is an interactive web-base graphic (Ployly 2023; Schork 2023). Plotly uses an open source JavaScript graphing library ployly.js to render dynamic plots. - A
ggrepelpackage provides geoms for ggplot2 to repel overlapping text labels.geom_text_repel(), andgeom_label_repel()
- A
cowplotpackage is a simple package add-on aggplot2package. It allows to arrange multiple plots into a publication-quality figure.- A
plot_gridfunction is a function used in this project.
- A
2.1.3 Theme settings
Theme and lines for figures are set in a variable named Themeline as follows:
theme_bw()function provides a black and white theme.theme()function applied for setting theme and line represented in plots.linepalette1andlinepalette1variables set line colors.
ThemeLine <-
theme_bw() +
theme(
panel.border=element_rect(fill=NA),
panel.grid.minor = element_line(color = NA),
# axis.title=element_text(size=5),
# axis.text.x = element_text(hjust=1,size = 10, angle = 0),
axis.line=element_line(colour="black"),
panel.background=element_rect(fill = "white"),
panel.grid.major.x=element_line(linetype="dashed",colour="grey",linewidth = 0.5),
panel.grid.major.y = element_blank(),
# panel.grid.major=element_blank(),
strip.background=element_rect(fill="white", colour="white"),
strip.text.x = element_text(size=10, colour = "black", angle = 0,face="bold"),
axis.text.x=element_text(size = 10,angle=45, vjust=0.9, hjust=1, margin = unit(c(t = 0.3, r = 0, b = 0, l = 0), "cm")),
axis.text.y=element_text(size = 10,margin = unit(c(t = 0, r = 0.3, b = 0, l = 0), "cm")),
legend.text = element_text(size = 10),
legend.title = element_text(size = 10),
axis.ticks.length=unit(-0.15,"cm")
)
linepalette1 <- c("#4DAF4A","#FF7F00","#377EB8","#E41A1C","#984EA3","#F781BF","#8DD3C7","#FB8072","#80B1D3","#FDB462","#B3DE69","#FCCDE5","#D9D9D9","#BC80BD","#CCEBC5","#FFED6F","#7f878f","#A65628","#FFFF33")
linepalette2 <- c("#E41A1C","#FF7F00","#377EB8","#B3DE69","#4DAF4A","#984EA3","#F781BF","#8DD3C7","#FB8072","#80B1D3","#FDB462","#FCCDE5","#D9D9D9","#BC80BD","#CCEBC5","#FFED6F","#7f878f","#A65628","#FFFF33")References
Ployly. 2023. “Getting Started with Plotly in r.” https://plotly.com/r/getting-started/.
Schork, Joachim. 2023. “Introduction to the Plotly Package in r.” https://statisticsglobe.com/plotly-r-package.