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

plot(y~x, data=xy, pch=16)
plot(y~x, data=xy, type='n')
points(y~x, data=xy[xy$type=="a",],
       col=2, pch=16)
points(y~x, data=xy[xy$type=="b",],
       col=3, pch=16)

cols<-data.frame(
  type=levels(xy$type),
  col=1:4)

plot(y~x, data=xy, 
     col=cols$col[cols$type%in%xy$type], pch=16,
     cex=0.8, cex.axis=1.3,
     cex.lab=1.5, las=1)

### Practicals #4
?legend
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)
?mtext

cor(xy$x, xy$y)^2

text(x=15, y=50, 
     expression(paste(italic("r")^2,
                      " = 0.456")))

boxplot(y~type, data=xy, col="grey")


means<-tapply(xy$y, xy$type, mean)
ses<-tapply(xy$y, xy$type, sd)/
  sqrt(tapply(xy$y, xy$type, length))
upper<-means+ses
lower<-means-ses
bp.x<-barplot(means, ylim=c(0, 40))
arrows(x0=bp.x, y0=lower, 
       y1=upper, angle=90, code=3,
       length=.1)

plot(means~c(1:4), ylim=c(25,38), pch=16,
     xlim=c(0.8, 4.2), xaxt='n', xlab=NA)
axis(1,at=1:4, labels = levels(xy$type))
arrows(x0=1:4, y0=lower, 
       y1=upper, angle=90, code=3,
       length=.1)

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)))
plot(y~fact, data=big.data)

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