######### PROCVICENI 2 ############
### 1 ###
DATA <- read.delim("procviceni_2.txt", sep="\t", header = TRUE)
str(DATA)
attach(DATA)
summary(Lokace)
levels(Lokace)[levels(Lokace)=="Kotl"] <- "Kotlarska"
levels(Lokace)[levels(Lokace)=="Vid"] <- "Videnska"
summary(Lokace)

### 2 ###
data <- na.omit(DATA[Lokace=="Kotlarska",])
str(data)
dim(data)

### 3 ###
# install.packages("psych")
library(psych)
describe(data[,c("H1","F1")])

### 4 ###
# install.packages("reshape")
library(reshape)
?melt
?melt.data.frame
H1F1 <- melt(data, id.vars = c("Hrob", "Krabice", "Lokace", "Rok","Side"), variable_name = "rozmer");H1F1
colnames(H1F1)

### 5 ###
windows()
par(family = "serif")
boxplot(value ~ Side:rozmer, data=H1F1, col = c(rep("aquamarine1",2),rep("darkseagreen1",2)), las = 1,
        border = c(rep("aquamarine4",2),rep("darkseagreen4",2)), axes=FALSE, main = "Rozměry H1 a F1",
        ylim = c(25,55))
axis(1,at=2:3, labels = c("H1","F1"))       
axis(2, las = 1)                                       
axis(4, las = 1)
text(x=c(1,2), y=38, labels = c("dx","sin"), col = "darkslategray4", font = 3)
text(x=c(3,4), y=52, labels = c("dx","sin"), col = "darkseagreen4", font = 3)
mtext("Rozložení dat", side = 3, font = 3, line = 0.5)

### 6 ###
prava <- na.omit(DATA[Side=="dx",])
leva <- na.omit(DATA[Side=="sin",])
SOUBOR <- merge(prava, leva, by=c("Hrob", "Lokace", "Krabice", "Rok")); SOUBOR
# attach(SOUBOR)
dim(SOUBOR)

### 7 ###
library(scales)
windows(width=15, height=7)
par(family = "serif", mfrow=c(1,2))
hist(leva$H1, breaks = 7, col=alpha("darkorchid1",0.5), xlim = c(26,38), ylim = c(0,6), main = "Histogram: Rozměr H1", xlab = "Rozměr H1", ylab = "četnost", las = 1)
hist(prava$H1, add = TRUE, breaks = 7, col=alpha("forestgreen",0.5), xlim = c(26,38), ylim = c(0,6))
legend(34.8, 6, legend = c("levá","pravá"), pch = 15, col=c(alpha("darkorchid1",0.5),alpha("forestgreen",0.5)))
hist(leva$F1, breaks = 8, col=alpha("darkorchid1",0.5), xlim = c(38,50), ylim = c(0,6), main = "Histogram: Rozměr F1", xlab = "Rozměr F1", ylab = "četnost", las = 1)
hist(prava$F1,add = TRUE, breaks = 8, col=alpha("forestgreen",0.5), xlim=c(38,50), ylim = c(0,6))
legend(48.5, 6, legend = c("levá","pravá"), pch = 15, col=c(alpha("darkorchid1",0.5),alpha("forestgreen",0.5)))

graphics.off()

### 8 ###
windows(width=15, height=7)
par(family = "serif", mfrow=c(1,2))
hist(leva$H1, breaks = 7, col=alpha("darkorchid1", 0.5), xlim = c(26, 38), ylim = c(0, 0.4), main = "Rozměr H1", probability = TRUE,
     xlab = "Rozměr H1", ylab = "relativní četnost", las = 1)
lines(density(leva$H1), lty=1, col = "darkorchid1")
hist(prava$H1,add = TRUE, breaks = 7, col=alpha("forestgreen", 0.5), xlim=c(26, 38), ylim = c(0, 0.4), probability = TRUE)
lines(density(prava$H1), lty=1, col = "forestgreen")
legend(34.8, 0.4, legend = c("levá","pravá"), pch = 15, col=c(alpha("darkorchid1", 0.5),alpha("forestgreen", 0.5)))
hist(leva$F1, breaks = 8, col=alpha("darkorchid1", 0.5), xlim = c(38, 50), ylim = c(0, 0.4), main = "Rozměr F1", probability = TRUE,
     xlab = "Rozměr F1", ylab = "relativní četnost", las = 1)
lines(density(leva$F1), lty=1, col = "darkorchid1")
hist(prava$F1,add = TRUE, breaks = 8, col=alpha("forestgreen", 0.5), xlim=c(38, 50), ylim = c(0, 0.4), probability = TRUE)
lines(density(prava$F1), lty=1, col = "forestgreen")
legend(48.5, 0.4, legend = c("levá","pravá"), pch = 15, col=c(alpha("darkorchid1", 0.5),alpha("forestgreen", 0.5)))

graphics.off()

### 9 ###
windows(width=15, height=7)
par(family = "serif", mfrow=c(1,2))
plot(H1.x ~ H1.y, data = SOUBOR, pch=19, col="seagreen1", xlab="Hodnoty pravé strany [cm]", ylab="Hodnoty levé strany [mm]",
     main = "Závislost rozměru H1 levé a pravé strany", xlim = c(29,36), ylim = c(30,37), las = 1)
mtext("Proložení lineárním modelem", side = 3, font = 3, line = 0.5)
mod <- lm(H1.x ~ H1.y, data = SOUBOR)
summary(mod)
abline(mod, col="darkslategrey", lwd=2, lty=6)
plot(F1.x ~ F1.y, data = SOUBOR, pch=19, col="seagreen1", xlab="Hodnoty pravé strany [cm]", ylab="Hodnoty levé strany [mm]",
     main = "Závislost rozměru F1 levé a pravé strany", xlim = c(40,49), ylim = c(40,49), las = 1)
mtext("Proložení lineárním modelem", side = 3, font = 3, line = 0.5)
mod <- lm(F1.x ~ F1.y, data = SOUBOR)
summary(mod)
abline(mod, col="darkslategrey", lwd=2, lty=6)

graphics.off()

### 10 ###
windows()
par(family = "serif")
plot(H1 ~ F1, data = leva, pch=19, col="olivedrab2", xlab="F1 [cm]", ylab="H1 [cm]",
     main = "Závislost rozměru H1 a F1 levé a pravé strany", las = 1)
grid(nx = NULL, ny = NULL, col = "slategray3", lty = "dotted", lwd = par("lwd"), equilogs = TRUE)
points(H1 ~ F1, data = leva, pch=19, col="olivedrab2")
points(H1 ~ F1, data = prava, pch=19, col="orange1")
mod_leva <- lm(H1 ~ F1, data = leva)
mod_prava <- lm(H1 ~ F1, data = prava)
abline(mod_leva, col="olivedrab3", lwd=2, lty=6)
abline(mod_prava, col="orange3", lwd=2, lty=6)
legend(40,35,legend = c("levá", "pravá"), col = c("olivedrab3","orange3"), lty = 6, cex = 0.8)

graphics.off()






