##
#
# MEBn5033 Assignment 02 - Data visualization
#
##

### Instructions ###

# Save this script under the new name with your surname appended 
# E.g.: MVZn5033_02_Assignment_Ocelik.R

# Write the commands directly to this newly saved script
# It sufficient to use just one solution to each task
# Each task is awarded by 1 point when done correctly
# Upload the script into the corresponding Homework Vault when you are finished 

# ?function_names gives you a hint what function to use
# It also opens, often a quite comprehensive, documentation of the function
# Do not copy paste the complete function description, typically we are OK with 1-4 arguments 

#
# 0. load Eurostat data set on renewable energy consumption
#

setwd("paste_your_dir_here")

df <- read.csv("MEBn5033_02_Assignment_data.csv",row.names = 1)

#
# 1. visualize any chosen energy source as a bar chart
#

complete.cases(df) # check if all cases are complete (optional)

colnames(df) # check all available variables (optional)

barplot(df$solar.thermal)

#
# 2. a) add country names to chart, 
#    b) name the whole plot
#    c) add axis names
#

barplot(df$solar.thermal,
        names.arg = rownames(df),
        main = "Solar (thermal)",
        xlab = "Countries",
        ylab = "Installed capacity")

#
# 3. visualize the mean of the chosen variable as absolute line over the plot
#

barplot(df$solar.thermal,
        names.arg = rownames(df),
        main = "Solar (thermal)",
        xlab = "Countries",
        ylab = "Installed capacity")

m.solar.thermal <- mean(df$solar.thermal)

abline(h = m.solar.thermal)

#
# 4. combine steps 1-3 and add custom styling (choose at least 5 parameters to improve visual appearance of the plot)
#

barplot(df$solar.thermal,
        names.arg = rownames(df),
        main = "Solar (thermal)",
        xlab = "Countries",
        ylab = "Installed capacity",
        col = "gray40", 
        border = F, 
        horiz = T, 
        las = 1
          )

m.solar.thermal <- mean(df$solar.thermal)

abline(v = m.solar.thermal, lwd = 2, lty = 2, col = "red")

# hint - you may explore the parameters using par() function


#
# 5. export the chart to PDF (hint - explore the Cairo library and see the presentation)
#

library("cairoDevice")

Cairo_pdf("solar.thermal.pdf",width = 12,height = 8,pointsize = 7)

barplot(df$solar.thermal,
        names.arg = rownames(df),
        main = "Solar (thermal)",
        xlab = "Countries",
        ylab = "Installed capacity",
        col = "gray40", 
        border = F, 
        horiz = T, 
        las = 1
)

m.solar.thermal <- mean(df$solar.thermal)

abline(v = m.solar.thermal, lwd = 2, lty = 2, col = "red")

dev.off()

#
# 6. visualize the renewable power mix of the Czech Republic as a bar chart
#

cz <- df[rownames(df) == "CZ", ]

cz.vec <- as.numeric(cz)

names(cz.vec) <- colnames(cz) # optionally, you may use names of columns as names for numbers in the vector

cz.vec

barplot(cz.vec)

# alternatively, you may use colnames of object cz as names argument

barplot(cz.vec,
        names.arg = colnames(cz),
        las = 2)

# hint - you need to get a particular dataframe row (explore rownames()) and convert it into a numeric vector
# rownames(df) == "CZ"


#
# 7. change color theme in the plot
#

barplot(cz.vec, col = "steelblue2",
        las = 2)

#
# 8. convert absolute values (Czech Republic) to percentage values, store them in a vector and add names to the vector elements
#

cz.sum <- sum(cz.vec)

cz.perc <- cz.vec/cz.sum

barplot(cz.perc)

#
# 9. extract values from task 8, which are above 5 percent. Calculate the rest as a category "other"
#

cz.above.5 <- cz.perc[cz.perc > 0.05]

cz.below.5 <- cz.perc[cz.perc <= 0.05]

other <- sum(cz.below.5)

cz.filtered <- append(cz.above.5, other)

names <- colnames(cz)[cz.perc > 0.05]

names <- append(names, "other")

#
# 10. visualize the data from task 9 and use at least 5 parameters from par()
#

?par()

par(mar = c(4,10,4,3),lwd = 0.25)

barplot(cz.filtered,
        names.arg = names,
        col = "gray40",
        horiz = T, 
        las = 1,
        xlim = c(0,0.5),
        cex.names = 0.7,
        xaxs = "i",
        border = F,tck = -0.015,
        xaxp = c(0,0.5,10)
        )
