##Practicals 3 (Graph plotting 1)
getwd()

xy<-read.csv("03_xy.csv")

summary(xy)
xy<-xy[,-1]

?par
# svg("histograms.svg", 8, 6)
svg("histograms_2.svg", 6, 4)
# svg("histograms_2.svg", 3, 2)
par(mfrow=c(1,2), mar=c(5, 4, 2, 0))
hist(xy$x, main=NULL, ylim=c(0,23))
par(mar=c(5, 2, 2, 2))
hist(xy$y, main=NULL, ylim=c(0,23))
dev.off()


par(mfrow=c(1,1))
cols<-data.frame(type=levels(xy.data$type),
                 cols=c("purple","red", "lightblue", 
                        "darkgreen"))
#cols<-data.frame(type=levels(xy.data$type),
#                 cols=c(5,2,3,4))


plot(y~x, data=xy.data, pch=16,
     col=as.character(cols$cols[match(xy.data$type,cols$type)]),
     cex=1.5, las=1, cex.axis=1.2, cex.lab=1.2,
     log='xy')
legend("topleft", levels(xy.data$type), col=as.character(cols$cols), pch=16,
       title="Type", cex=1.5)
text(15,50, "Colorful points")
mtext("Colorful points", side=3, line=2, cex=1.5, font=2)

r2<-cor(xy.data$x,xy.data$y)^2
text(25, 10, expression(paste(italic("r")^2, " = 0.456")))	 

?points
?mtext
?par
?legend
??"cex.axis"


	 
### Practicals #4

legend(x="topleft", legend=c("trees", 
      "herbs", "mosses", "lichens"),
       col=1:4, pch=16, ncol=2,
       bty='n')

text(x=15, y=50, "Colourful points")
mtext("Colourful points", side=3,
      line=0.5, font=4, cex=1.5)

par(mfrow=c(1,1))

boxplot(y~type, data=xy.data)

mean(xy.data$y)
means<-tapply(xy.data$y, xy.data$type, mean)
x.coords<-barplot(means, ylim=c(0,40))

# Three ways how to compute standard error
ses<-tapply(xy.data$y, xy.data$type, sd)/sqrt(tapply(xy.data$y, xy.data$type, length))
std <- function(x) sd(x)/sqrt(length(x))
ses<-tapply(xy.data$y, xy.data$type, std)
library(sciplot)
ses<-tapply(xy.data$y, xy.data$type, se)

upper<-means+ses
lower<-means-ses
arrows(x0=x.coords, y0=lower, y1=upper, code=3, angle=90)

plot(1:4, means, pch=16, cex=2, ylim=c(25,40), xlim=c(0.5, 4.5), 
     xaxt='n', ylab="Mean y values", xlab="Type")
arrows(x0=1:4, y0=lower, y1=upper, code=3, angle=90)
axis(1, at=1:4, labels=levels(xy.data$type))

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)))

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