###https://www.r-graph-gallery.com/

library(ggplot2)
library(plotly)
library(dplyr)
library(tidyr)

data<-airquality

####bodovy graf####

p1<-ggplot(data,aes(x=Day,y=Ozone,color=factor(Month)))+
  geom_point()
p1

p1<-ggplot(data,aes(x=Wind,y=Ozone,size=factor(Month),color=factor(Day)))+
  geom_point()
p1

p2<-ggplot(data,aes(x=Day,y=Ozone))+
  geom_point()+
  facet_wrap(~Month)

p2

ggplotly(p1)

# p<-p1+transition_manual(Month)
# anim_save("271-ggplot2-animated-gif-chart-with-gganimate2.gif",animation=p)


####line####

datavyb<-data%>%
  select(Day,Month,Ozone)%>%
  na.omit()

p1<-ggplot(datavyb,aes(x=Day,y=Ozone,color=factor(Month)))+
  geom_line(na.rm = T)
p1

ggplotly(p1)


plot_ly(data = datavyb, x = ~Day, y = ~Ozone,color =~factor(Month),
        type = "scatter", mode = 'lines+markers'   ,connectgaps = TRUE) 



####boxploty####

p1<-ggplot(data,aes(x=Month,y=Ozone,color=factor(Month)))+
  geom_boxplot()
p1

datanew<-data%>%
  mutate(ozonekat = cut(Ozone, breaks = c(0,quantile(Ozone, probs = seq(0.25, 1, 0.25),na.rm = T))))
  
ggplot(datanew, aes(Ozone, Wind,color=ozonekat)) +
  geom_boxplot(aes(group = ozonekat),alpha=0.6 ,outlier.size = 0,outlier.colour = 'white',outlier.alpha = 0)+
  geom_point()

####korelacni matice####



#klasika
pairs(data)


y <- as.matrix(data)
rt <- rcorr(y,type="spearman")
library(corrplot)
corrplot(rt$r, method="square",  
             type="upper", 
             #addCoef.col = "black", # Add coefficient of correlation
             tl.col="black", tl.srt=45, #Text label color and rotation
             # Combine with significance
             p.mat = rt$P, insig = "label_sig",
             sig.level = c(.001, .01, .05), pch.cex = 1, pch.col = "black", 
             # hide correlation coefficient on the principal diagonal
             diag=FALSE,col= colorRampPalette(c("blue","white", "red"))(200),
             mar = c(2, 4.5, 0, 0)
)

#ggplot

library(Hmisc)

library(GGally)


p1<-ggpairs(data)
p1
p2<-ggcorr(data, method = c("pairwise", "pearson")) 
p2
p3<-ggpairs(data,columns = 1:4,aes(color=factor(Month)))
p3







library(ggcorrplot)

p4<-ggcorrplot(rt$r)
p4
p5<- ggcorrplot(rt$r, method = "circle",type = "lower",p.mat = rt$r)
p5



mtlr <- reshape2::melt(round(rt$r,3))
mtlr<-mtlr%>%
  mutate(Var1=factor(Var1,ordered = T),Var2=factor(Var2,ordered = T))

mtlp <- reshape2::melt(round(rt$P,3))
mtlp<-mtlp%>%
  mutate(Var1=factor(Var1,ordered = T),Var2=factor(Var2,ordered = T))

#heatmap
p.value <- mtlp$value
p6 <- ggplot(mtlr, aes(Var1, Var2, fill = value, label=p.value)) + geom_tile() + 
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
                       midpoint = 0, limit = c(-1,1), space = "Lab", 
                       name="Spearman\nCorrelation")+
  theme_minimal()+
  theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 10, hjust = 1))
p6




#plotly

ggplotly(p5)





