---
title: "Graphical basic"
author: "Lukas Pecinka"
date: "11 11 2020"
output: html_document
---

Budete potrebovat knihovnu [Rmarkdown](https://rmarkdown.rstudio.com/). Help v R (Help ->Markdown wuick reference)
Karta se zakladnimi prikazy pro praci s [R markdown](https://rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf).


```{r include=FALSE}
x <- rnorm(n = 100, mean = 5)
hist(x)
boxplot(x)
```

# Histogram
```{r}
?plot
hist(x, xlab = "osa x", ylab = "osa y", main = "Histogram", col="blue")
```

# mocniny
```{r echo=FALSE}
x<-seq(0,10,1)
y<-x^2
y2<-x^3

plot(y~x, type = "b", pch = "x", col = "red", xlab = "hodnoty x", main = "Exponenciální funkce", ylim=c(0,500))
points(y2~x, type = "b", lty = 2)
legend("topleft", c("X^2", "x^3"), col = c("red", "black"), lty = 1:2)
```

```{r}
library(readxl)
x<-read_excel("a.xlsx", sheet = "List1")
getwd()
dir()
str(x)
plot(km~rychlost, x)
plot(x$rychlost, x$km, axes = T, main = "Závislost rychlosti na počtu ujetých km", type = "p", pch="$", col="blue", cex=2,
     xlab = "rychlost [km]", ylab = "ujete km", xlim = c(0,250), frame.plot = F)
points(150,100, cex=5)
model<-lm(km~rychlost, x)
plot(km~rychlost, x)
abline(model, col="red", lty=2)
model
summary(model)
predikce <- predict(model, interval = "confidence", level = 0.95)
predikce
x<-as.matrix(x)
points(x[,1], predikce[,2], type = "l", col="blue")
points(x[,1], predikce[,3], type = "l", col="blue")
```

# DATA IRIS
VYSVETLENI DAT [KOSATCE](https://i.imgur.com/PQqYGaW.png)
```{r}
#library(datasets)
iris
str(iris)
summary(iris)
minimum <- sapply(iris, function(x) min(as.numeric(x))); minimum
```

```{r}
plot(iris$Sepal.Length~iris$Petal.Length,
     ylab = "Sepal Lenght", xlab = "Petal Lenght", col = "red")
hist(iris$Sepal.Width, xlab = "Sepal width", ylab= "Distribution of Sepal width", col = "red", main = "Histogram Sepal width")
boxplot(iris$Sepal.Width~iris$Species,xlab = "Species", ylab= "Sepal Width", col = "red", main = "Sepal Width of species")
```

## GGPLOT2
1. **DATA** - DATA SET
2. **AESTHETIC** - DATA COLUMNS
3. **GEOMETRY** - TYPE OF GRAPH
4. **FACED** - GROUPS BY WHICH THE DATA IS DIVIDED

```{r}
library(ggplot2)
ggplot(data=iris)
ggplot(data = iris, aes(y=Sepal.Length, x=Petal.Length))
ggplot(data = iris, aes(y=Sepal.Length, x=Petal.Length)) +
  geom_point()

ggplot(data = iris, aes(y=Sepal.Length, x=Petal.Length, col=Species, shape=Species)) +
  geom_point() +
  #theme_classic()
  theme_bw() +
  theme_light()

ggplot(data = iris, aes(y=Sepal.Length, x=Petal.Length, col=Species, shape=Species)) +
  geom_point() +
  #theme_classic()
  theme_bw() +
  theme_light() +
  theme(
  panel.background = element_rect(fill = "lightblue",
                                colour = "lightblue",
                                size = 0.5, linetype = "solid"))


```

```{r}
ggplot(iris, aes(x=Sepal.Width)) +
  geom_histogram(bins = 12)

ggplot(iris, aes(x=Sepal.Width)) +
  geom_histogram(bins = 12, fill = "green", col = "black") +
  theme_classic()

ggplot(iris, aes(x=Sepal.Width, fill = Species)) +
  geom_histogram(bins = 12, col = "black", alpha=0.5) +
  theme_classic() +
  theme(legend.position="top")

#positions are identity, stack, dodge. Default value is stack
ggplot(iris, aes(x=Sepal.Width, fill = Species)) +
  geom_histogram(bins = 12, col = "black", alpha=0.3, position = "identity") +
  theme_classic() +
  theme(legend.position="top")

#Split the plot into multiple panels

ggplot(iris, aes(x=Sepal.Width, fill = Species)) +
  geom_histogram(bins = 12, col = "black") +
  theme_classic() +
  facet_grid(Species ~ .) 


library(plyr)
mu <- ddply(iris, "Species", summarise, grp.median=median(Sepal.Width))
mu_2 <- ddply(iris, "Species", summarise, grp.mean=mean(Sepal.Width))

ggplot(iris, aes(x=Sepal.Width, fill = Species)) +
  geom_histogram(bins = 12, col = "black") +
  theme_classic() +
  facet_grid(Species ~ .) +
geom_vline(data=mu, aes(xintercept=grp.median, color="red"),
             linetype="dashed", size = 1.5, col = "black") +
geom_vline(data=mu_2, aes(xintercept=grp.mean, color="red"),
             linetype="dashed", size = 1.5, col = "black")

ggplot(iris, aes(x=Sepal.Width, fill = Species)) +
  geom_histogram(bins = 12, col = "black", position = "fill") +
  theme_classic()
```

[BOXPLOT](https://cs.wikipedia.org/wiki/Boxplot#/media/Soubor:Boxplot_vs_PDF.svg)
```{r}
ggplot(iris, aes(x=factor(Species), y= Sepal.Width)) +
  geom_boxplot()
  #theme_classic()
  
ggplot(iris, aes(x=factor(Species), y= Sepal.Width, fill = factor(Species))) +
  geom_boxplot()
  #theme_classic()

```

## LM MODEL
```{r}
ggplot(data = iris, aes(y=Sepal.Length, x=Petal.Length, col=Species, shape=Species)) +
  geom_point() +
  geom_smooth(method = "lm", se = F) +
  #theme_classic()
  theme_bw() +
  theme_light()

ggplot(data = iris, aes(y=Sepal.Length, x=Petal.Length, col=Species, shape=Species)) +
  geom_point() +
  geom_smooth(method = "lm", se = T) +
  facet_grid(Species ~ .) +
  #theme_classic()
  theme_bw() +
  theme_light()
```

## LABELS
```{r}
ggplot(iris, aes(x=Sepal.Width, fill = Species)) +
  geom_histogram(bins = 12, col = "black", position = "fill") +
  theme_classic() +
  labs(title = "HISTOGRAM CONTRIBUTION", tag = "1", x = "SEPAL WIDTH", y = "% OF CONTRIBUTION", subtitle = "12.12.2020", caption = "(based on data from No.xx, Lukas Pecinka)") +
  theme(legend.position="top", plot.title = element_text(hjust = 0.5, face = "bold", colour = "blue")) 
```

### UV-Vis stanoveni dusicnanu v pivu

```{r}
library(readxl)
kalibrace<-read_excel("UV-Vis.xlsx", sheet = "List1")
str(kalibrace)
ggplot(kalibrace, aes(x = wavelength, y = calibrant)) +
  geom_line() +
  theme_classic() 

ggplot(kalibrace, aes(x = wavelength, y = sample)) +
  geom_line() +
  geom_line(aes(x = wavelength, y = calibrant), color = "red", linetype = "dotted") +
  theme_classic() +
  labs(title = "Absorpni spektrum dusicnanu pro λ = 400 – 650 nm", tag = "1", x = "λ [nm]", y = "A", subtitle = "12.12.2020", caption = "(based on data from No.xx, Lukas Pecinka)") +
  theme(legend.position="top", plot.title = element_text(hjust = 0.5, face = "bold", colour = "blue"))
```

### KALIBRACNI KRIVKA
```{r}
standardy<-read_excel("UV-Vis.xlsx", sheet = "List2")
str(standardy)
ggplot(standardy, aes(x = c, y = A)) +
  geom_point() +
   theme_classic() +
  geom_smooth(method = "lm", se = T)

mod <- lm(A ~ c, data = standardy)
summary(mod)
mod$coefficients
# y = ax + b
a <- mod$coefficients[2]
b <- mod$coefficients[1]

predikce <- predict(mod, interval = "confidence", level = 0.95)
predikce
standardy<-as.matrix(standardy)

plot(A~c, standardy)
points(standardy[,1], predikce[,2], type = "l", col="blue")
points(standardy[,1], predikce[,3], type = "l", col="blue")
```

[LATEX](https://www.caam.rice.edu/~heinken/latex/symbols.pdf) pro zapis rovnic

$$ y = ax + b $$
$$ y = `r a `x + `r b ` $$
### NEZNAMY VZOREK

```{r}
vzorek <- read_excel("UV-Vis.xlsx", sheet = "List3")
str(vzorek)

x1 <- (vzorek$A[1] - b) / a
x2 <- (vzorek$A[2] - b) / a
x3 <- (vzorek$A[3] - b) / a

vypocteny <- c(x1, x2, x3)
vzorek$koncentrace <- vypocteny

library(xlsx)
write.xlsx(as.data.frame(vzorek),"vystup.xlsx")
```


