##### Practicals #3
library(readxl)
#1
lettuce<-read_excel("lettuce.xlsx")
summary(lettuce)

### 2 - 4 
### Individual steps
# 2
# Histograms of harvest days for each color
hist(lettuce$harv.days[lettuce$leaf.col=="green"])
hist(lettuce$harv.days[lettuce$leaf.col=="red"])

# Combining the histograms in a single plot, removing titles
par(mfrow=c(1,2))
hist(lettuce$harv.days[lettuce$leaf.col=="green"], main=NULL)
hist(lettuce$harv.days[lettuce$leaf.col=="red"], main=NULL)

# 3 Improving the graphics by parameters: Adjusting margins , fixing the ranges of axes,
# setting x-axis labels, changing the histogram colors

par(mfrow=c(1,2), mar=c(5, 4, 1, 0)) # setting two-panel graphical device and graph margins
hist(lettuce$harv.days[lettuce$leaf.col=="green"],# plotting the historgram of green-leved lettuce verieties
     main=NULL, xlab="Days to harvest", # suppressing of graph title, setting x axis label
     xlim=c(50, 70), ylim=c(0,18), col=3) #setting the ranges of x and y axes; setting color to green
par(mar=c(5, 2, 1, 2))# resetting graph margins
hist(lettuce$harv.days[lettuce$leaf.col=="red"],
     main=NULL, xlab="Days to harvest", 
     xlim=c(50, 70), ylim=c(0,18), col=2, 
     yaxt='n', ylab=NULL)#supression of y-axis and its label



### Tasks 2-3 alternative approaches
library(car)
scatterplot(harv.mass~harv.days|leaf.col, data=lettuce,
            regLine=F, smooth=F, grid=F)

library(ggplot2)
ggplot(data=lettuce)+
geom_point(data=lettuce[lettuce$leaf.col=="red",], aes(x=harv.days, y=harv.mass), colour="red")+
geom_point(data=lettuce[lettuce$leaf.col=="green",], aes(x=harv.days, y=harv.mass), colour="green")+
theme_bw()


# 4 Direct drawing of figures in a pdf
pdf("histograms.pdf", 6, 4) # Creation of the target pdf file
par(mfrow=c(1,2), mar=c(5, 4, 1, 0)) # setting two-panel graphical device and graph margins
hist(lettuce$harv.days[lettuce$leaf.col=="green"],# plotting the historgram of green-leved lettuce verieties
     main=NULL, xlab="Days to harvest", # suppressing of graph title, setting x axis label
     xlim=c(50, 70), ylim=c(0,18), col=3) #setting the ranges of x and y axes; setting color to green
par(mar=c(5, 2, 1, 2))# resetting graph margins
hist(lettuce$harv.days[lettuce$leaf.col=="red"],
     main=NULL, xlab="Days to harvest", 
     xlim=c(50, 70), ylim=c(0,18), col=2, 
     yaxt='n', ylab=NULL)#supression of y-axis and its label
dev.off()# closing the pdf file. DO NOT FORGET!

svg("histograms.svg", 6, 4) # Creation of the target svg file
par(mfrow=c(1,2), mar=c(5, 4, 1, 0)) # setting two-panel graphical device and graph margins
hist(lettuce$harv.days[lettuce$leaf.col=="green"],# plotting the historgram of green-leved lettuce verieties
     main=NULL, xlab="Days to harvest", # suppressing of graph title, setting x axis label
     xlim=c(50, 70), ylim=c(0,18), col=3) #setting the ranges of x and y axes; setting color to green
par(mar=c(5, 2, 1, 2))# resetting graph margins
hist(lettuce$harv.days[lettuce$leaf.col=="red"],
     main=NULL, xlab="Days to harvest", 
     xlim=c(50, 70), ylim=c(0,18), col=2, 
     yaxt='n', ylab=NULL)#supression of y-axis and its label
dev.off()# closing the svg file. DO NOT FORGET!


#5
par(mfrow=c(1,1), mar=c(5, 4, 2, 2))# resetting the graphical device to single panel and correspoding margins


###### Tasks 6-13
# 6 The scatterplot
plot(harv.mass~harv.days, data=lettuce)

#7 Setting the point symbols; setting the colors of the points
# preparation of the color vector
cols<-as.numeric(as.factor(lettuce$leaf.col))# transformation of leaf.col factor to numeric values (1s and 2s)
cols[cols==1]<-3 #changing the 1 values to 3 (3 is the code for green color)

plot(harv.mass~harv.days, data=lettuce, pch=16,
     col=cols) # Plotting the scatterplot; pch=16 sets flled points; col sets color

# 8
plot(harv.mass~harv.days, data=lettuce, pch=16,
     col=cols, cex=1.5) # parameter cex increases point size 1.5x

# 9
plot(harv.mass~harv.days, data=lettuce, pch=16,
     col=cols, las=1, cex.axis=1.25, cex.lab=1.25) # parameter las changex the axis value oriantation
# cex.axis and cex.lab adjust the font size of axis values and labels

# 10 
mtext("Lettuce varieties", line=0.5, font=2, cex=1.1)

# 12 The legend
legend("topright", col=c(3,2), pch=16, legend=c("Green lf", "Red lf"), 
       bg=rgb(0.9, 0.9, 0.9),box.col=rgb(0.9, 0.9, 0.9) )##Adding of the legend to the plot


##### Practicals #4

# 13 Boxplot
boxplot(harv.mass~leaf.col, data=lettuce)

# Task 12  ##Note that you need to install the barplotN and dotchartN functions (by copy-paste from .R files
# available in IS) 
barplotN(lettuce$leaf.col, lettuce$harv.mass, err.bars = "range")
barplotN(lettuce$leaf.col, lettuce$harv.mass, err.bars = "ci")
barplotN(lettuce$leaf.col, lettuce$harv.mass, err.bars = "range")

# Task 13
dotchartN(lettuce$leaf.col, lettuce$harv.mass, err.bars = "se")
dotchartN(lettuce$leaf.col, lettuce$harv.mass, err.bars = "ci")
dotchartN(lettuce$leaf.col, lettuce$harv.mass, err.bars = "range")

# Task 14
big.data<-data.frame(y=rlnorm(30000, 4, 0.5)+sample(c(1,50, 8, 100, 
9, 1, 20, 40, 20), 30000, replace=T), fact=c(rep("a", 12000), 
rep("b", 18000)))


# Task 14 Boxplot
boxplot(y~fact, data=big.data)


# Task 15 Log scaled y axis
boxplot(y~fact, data=big.data, log='y')

# Task 16 Beanplot with examination of y-axis scaling
library(beanplot)
beanplot(y~fact, data=big.data)

beanplot(y~fact, data=big.data, log='')
beanplot(y~fact, data=big.data, log='y')





