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.

install.packages("tidyverse", "readxl","scales", "glue","knitr", "plotly", "ggrepel", "cowplot")

2.1.2 Packages settings

Before the project starts, packages are required as follows:

  • A tidyverse package 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 ggplot2 package is for data visualization.
    • A dplyr package is for data manipulation.
    • A tidyr package is for data tidying.
    • A readr package is for data import.
    • A purrr package is for functional programming.
    • A tibble package is a modern reimagining of the data.frame.
    • A stringr package is for strings.
    • A forcats package is for factors.
    • A lubridate package is for date and time.
  • A readxl package is used for read data from Excel into R.
  • A scales package is applied to scale plots in the ggplot2 package.
  • A glue package interprets strings literal. The package embeds R expressions and inserts into argument string.
  • A knitr package 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 plotly package is an alternative package to a ggplot2 package. 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 ggrepel package provides geoms for ggplot2 to repel overlapping text labels.
    • geom_text_repel(), and
    • geom_label_repel()
  • A cowplot package is a simple package add-on a ggplot2 package. It allows to arrange multiple plots into a publication-quality figure.
    • A plot_grid function is a function used in this project.
library(tidyverse) #For data manipulation
library(readxl) #For reading the excel sheet
library(scales)
library(glue)
library(knitr)
library(plotly)
library(ggrepel)
library(cowplot)

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.
  • linepalette1 and linepalette1 variables 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")

2.1.4 Directory setting

Lists are create to store variables.

#----- Make a list of profile to store data -----####

profiledata <- list()
profilefigure <- list()
summarydata <- list()
#----- Make variable for output directory -----####
outfigdir <- c("figures/")

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.