## Practicals #3 and #4 - GRAPH PLOTTING
##### Section I 

lettuce<-read.delim2("clipboard")
summary(lettuce)

### Tasks 2 and 3
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.

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

## Tasks 4-6
cols<-as.numeric(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, which is different between green and red varieties
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

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

## Task 7
plot(harv.mass~harv.days, data=lettuce, pch=16, col=cols,
     cex=0.7 # cex paramter adjusts the point size (<1 means smaller size, >1 increases the size)
     ) 

## Task 8
plot(harv.mass~harv.days, data=lettuce, pch=16, col=cols,
     las=1, ## makes all axis values perpendicular to axes
     cex.axis=0.8, #decreases font size of axis values
     cex.lab=1.2 #increases font size of axis labels 
     ) 
## Task 9
plot(seed.prod~harv.mass, data=lettuce, pch=16, col=cols,
     log='y') #creates the plot with log-scaled y-axis
plot(seed.prod~harv.mass, data=lettuce, pch=16, col=cols,
     log='xy') #creates the plot with both axes log-scaled

## Task 10
plot(harv.mass~harv.days, data=lettuce, pch=16, col=cols
     ) 
text(x=65, y=325, "lettuce varieties") ## adds the text into the plot region as specified
#by the coordinates
mtext("lettuce varieties", 3, 1)# adds the text onto the upper margin (margin 3), in 1 text line 
# distance from the plot region

##### Section II 
#Task 11
plot(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 = "se")

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

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


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

# Task 15

library(beanplot)
beanplot(y~fact, data=big.data)
