## ggplot2

## Dati un paketes

# Pamatpakete attēlu atveidošanai
library(ggplot2)
# Pakete nepieciešama, ja jāizmanto funkcija unit() (mērvienības)
library(grid)
# Pakete, kurā ir papildus skalas un to transformācijas
library(scales)
data(mtcars)
head(mtcars)


## geom_point()

ggplot(mtcars, aes(wt, mpg)) + geom_point()


ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(colour = qsec))

ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(alpha = qsec))

ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(colour = factor(cyl)))

ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(shape = factor(cyl)))

ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(size = qsec))

ggplot(mtcars, aes(wt, mpg)) + geom_point(colour = "red", size = 3)

## geom_bar()

ggplot(mtcars, aes(factor(cyl)))+geom_bar()

ggplot(mtcars, aes(factor(cyl))) + geom_bar(width=.5)

ggplot(mtcars, aes(factor(cyl))) + geom_bar() + coord_flip()

ggplot(mtcars, aes(factor(cyl))) + geom_bar(fill="white", colour="darkgreen")

ggplot(mtcars, aes(factor(cyl),fill=factor(gear))) + geom_bar()

ggplot(mtcars, aes(factor(cyl),fill=factor(gear))) + geom_bar(position="dodge")

df<-data.frame(Dzimums=c("F","M"),Skaits=c(23,19))
ggplot(df,aes(Dzimums,Skaits))+geom_bar(stat="identity")

## geom_boxplot()

ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()

ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot() + geom_jitter()

ggplot(mtcars, aes(factor(cyl), mpg)) + 
      geom_boxplot(outlier.colour = "green", outlier.size = 3)

ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot(aes(fill = cyl))

ggplot(mtcars, aes(factor(cyl), mpg))+ geom_boxplot(aes(fill = factor(cyl)))

ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot(aes(fill = factor(vs)))

ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot(fill = "grey80", colour = "#3366FF")

## geom_dotplot()

ggplot(mtcars, aes(x=mpg)) + geom_dotplot()

ggplot(mtcars, aes(x=mpg)) + geom_dotplot(binwidth = 1.5)

ggplot(mtcars, aes(x=mpg)) + geom_dotplot(binwidth = 1.5, stackdir = "center")

## geom_histogram()

df<-data.frame(gar=rnorm(1000,165,15))
ggplot(df, aes(x=gar)) + geom_histogram()

ggplot(df, aes(x=gar)) + geom_histogram(aes(y = ..density..)) + geom_density()

ggplot(df, aes(x=gar))+ geom_histogram(binwidth = 10)

ggplot(df, aes(x=gar)) + geom_histogram(binwidth = 5)

ggplot(df, aes(x=gar)) + geom_histogram(binwidth = 0.5)

ggplot(df, aes(x=gar)) + 
      geom_histogram(colour = "darkgreen", fill = "white", binwidth = 5)

## geom_hline()

ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point()+geom_hline(aes(yintercept=mpg))

ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point()+geom_hline(yintercept=20)

ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point() + 
      geom_hline(yintercept=seq(10, 30, by=5))

## geom_vline

ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point() + geom_vline(xintercept = 5)

ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point() + geom_vline(xintercept = 1:5)

ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point() + 
      geom_vline(xintercept = 1:5, colour="green", linetype = "longdash")

ggplot(mtcars, aes(x = wt, y=mpg)) + geom_line()

## geom_line()

ggplot(mtcars, aes(x = wt, y=mpg)) + geom_line(aes(size = as.factor(cyl)))

ggplot(mtcars, aes(x = wt, y=mpg)) + geom_line(aes(colour = as.factor(cyl)))

ggplot(mtcars, aes(x = wt, y=mpg))  + geom_line(colour = "red", size = 1)

## geom_text()

ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars))) + geom_text()

ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars))) + geom_text(size=10)

ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars))) + geom_point() + geom_text()

ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars))) + 
      geom_point() + geom_text(hjust=0, vjust=0)

ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars))) + geom_point()  + 
      geom_text(angle = 45)

ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars))) + geom_point()+ 
      geom_text(aes(colour=factor(cyl)))

ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars))) + geom_point() + 
      geom_text(aes(size=wt))

ggplot(mtcars, aes(wt, mpg)) + geom_point() + 
      geom_text(data = NULL, x = 5, y = 30, label = "plot mpg vs. wt")

## stat_smooth()

ggplot(mtcars, aes(qsec, wt)) + stat_smooth()

ggplot(mtcars, aes(qsec, wt)) + stat_smooth() + geom_point()

ggplot(mtcars, aes(qsec, wt)) + stat_smooth(se = FALSE) + geom_point()

ggplot(mtcars, aes(qsec, wt)) + stat_smooth(method = "lm") + geom_point()

ggplot(mtcars, aes(qsec, wt)) + stat_smooth(method=lm, aes(fill = factor(cyl))) +
      geom_point()

## annotate()

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + 
      annotate("text", x = 4, y = 25, label = "Teksts attela")

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + 
      annotate("text", x = 2:5, y = 25, label = "Teksts attela")

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()  + 
      annotate("text", x = 2:3, y = 20:21, label = c("teksts1", "teksts2"))

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()  + 
      annotate("rect", xmin = 3, xmax = 4.2, ymin = 12, ymax = 21, alpha = .2)

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()  + 
      annotate("segment", x = 2.5, xend = 4, y = 15, yend = 25, colour = "blue")

## Skalas
## scale_colour_brewer()

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point()

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point() + 
      scale_colour_brewer()

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point() + 
      scale_colour_brewer("Cilindri")

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point() + 
      scale_colour_brewer(palette="Set1")

## scale_colour_grey()

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point() + 
      scale_colour_grey()

## scale_colour_manual()

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point() + 
      scale_colour_manual(values = c("red","blue", "green"))

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point() + 
      scale_colour_manual(values = c("8" = "red","4" = "blue","6" = "green"))

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point() + 
      scale_colour_manual("Cilindri",values = c("4" = "blue","6" = "green","8" = "red"),
                          labels=c("Cetri","Sesi","Astoni"))

## scale_continuous()

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point()

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point() + 
      scale_y_continuous("Judzes/galons")

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point() + 
      scale_y_continuous("Judzes/galons",breaks=seq(10,40,7.5))

ggplot(mtcars, aes(x = wt, y = mpg,color=factor(cyl))) + geom_point() + 
      scale_y_continuous("Judzes/galons",breaks=c(10,20,30),
                         labels=c("Maz","Videji","Daudz"))

df<-data.frame(grupa=c("A","B","C"),proporcija=c(0.5,0.3,0.2))
ggplot(df,aes(grupa,proporcija))+geom_bar(stat="identity")

library(scales)
ggplot(df,aes(grupa,proporcija))+geom_bar(stat="identity") + 
      scale_y_continuous(labels=percent)

## scale_x_log10()

ggplot(mtcars, aes(x = disp, y = mpg,color=factor(cyl))) + geom_point()  + 
      scale_x_log10(limits=c(1,1000),breaks=c(1,10,100,1000))

## scale_y_sqrt()

ggplot(mtcars, aes(x = disp, y = mpg,color=factor(cyl)))+ geom_point() + scale_y_sqrt()


## scale_y_reverse()

ggplot(mtcars, aes(x = disp, y = mpg,color=factor(cyl)))+ geom_point() + 
      scale_y_reverse()

## scale_x_discrete()

ggplot(mtcars, aes(x = factor(cyl), y = mpg))+ geom_boxplot()

ggplot(mtcars, aes(x = factor(cyl), y = mpg))+ geom_boxplot() + 
      scale_x_discrete("Cilindri")

ggplot(mtcars, aes(x = factor(cyl), y = mpg))+ geom_boxplot() + 
      scale_x_discrete("Cilindri",labels=c("Cetri","Sesi","Astoni"))

ggplot(mtcars, aes(x = factor(cyl), y = mpg))+ geom_boxplot() + 
      scale_x_discrete("Cilindri",limits=c("4","8"))

## Nosaukumi

ggplot(mtcars, aes(x = disp, y = mpg,color=factor(cyl)))+ geom_point() + 
      labs(x = "X ass",y="Y ass",title="Galvenais",color="Krasas")


## Vērtību diapozons

ggplot(mtcars, aes(x = disp, y = mpg,color=factor(cyl)))+ 
      geom_smooth(method="lm")+geom_point()

ggplot(mtcars, aes(x = disp, y = mpg,color=factor(cyl)))+ 
      geom_smooth(method="lm")+geom_point()+
      scale_x_continuous(limits=c(100,400))

ggplot(mtcars, aes(x = disp, y = mpg,color=factor(cyl)))+ 
      geom_smooth(method="lm")+geom_point()+
      coord_cartesian(xlim = c(100, 400))

## coord_fixed()

ggplot(mtcars, aes(x = mpg, y = wt)) +geom_point()+coord_fixed(ratio = 1)

ggplot(mtcars, aes(x = mpg, y = wt)) +geom_point()+coord_fixed(ratio = 5)

## Attēlu sadalīšana daļās
## facet_grid()

ggplot(mtcars, aes(mpg, wt)) + geom_point()

ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(. ~ cyl)

ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(cyl ~ .)

ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(vs ~ am)

ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(vs ~ am, margins=TRUE)

ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(. ~ cyl, scales = "free")

ggplot(mtcars, aes(mpg, wt)) + geom_point() + 
      facet_grid(. ~ cyl, scales = "free", space = "free")

ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(vs ~ am, scales = "free")

ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(vs ~ am, scales = "free_x")

## facet_wrap()

ggplot(mtcars, aes(mpg, wt)) + geom_point()

ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(~ carb)

ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(~ carb,ncol=1)

## Attēla izskata maiņa
## theme_bw()

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point()

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + theme_bw()

## theme_minimal()

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme_minimal()

## theme_classic()

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme_classic()

## theme_excel()

library(ggthemes)
ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme_excel()

## theme_wsj()

library(ggthemes)
ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + theme_wsj()

## theme() 

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point()+
      labs(title="Virsraksts")

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point()+
      labs(title="Virsraksts") + 
      theme(plot.title = element_text(size = rel(2),colour="blue"))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(axis.line = element_line(size = 3, colour = "red", linetype = "dotted"))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(axis.text = element_text(colour = "blue"))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(axis.text.y = element_blank())

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(axis.ticks = element_line(size = 2))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(axis.title.y = element_text(size = rel(1.5), angle = 90))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(axis.title.x = element_blank())

library(grid)
ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(axis.ticks.length = unit(.85, "cm"))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(legend.position = "none")

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(legend.position = "bottom")

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(legend.position = c(.5, .5))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(legend.background = element_rect(colour = "black"))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(legend.key = element_rect(colour = "black"))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() +
      theme(legend.key = element_rect(fill = "yellow"))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(legend.key.size = unit(2.5, "cm"))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(legend.text = element_text(size = 20, colour = "red", angle = 45))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(panel.grid.major = element_line(colour = "blue"))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(panel.grid.minor = element_line(colour = "red", linetype = "dotted"))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(panel.grid.major = element_line(size = 2))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(panel.grid.major.y = element_blank(), 
            panel.grid.minor.y = element_blank())

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(plot.background = element_rect(fill = "green"))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point() + 
      theme(panel.background = element_rect(colour = "pink",size=3))

ggplot(mtcars, aes(x = wt, y = mpg,colour = as.factor(cyl))) + geom_point()+
      labs(color="Cilindri")+
      theme(axis.text.y=element_text(size=rel(1.2),face="bold"),
            axis.text.x=element_text(size=rel(1.2),face="bold",angle=90,vjust=0.5),
            axis.title=element_text(size=rel(1.5),face="bold"),
            axis.line=element_line(color="black"),
            panel.background=element_blank(),
            panel.grid.minor=element_blank(),
            panel.grid.major=element_line(color="grey90"),
            legend.position="top",
            legend.key=element_rect(fill="white"),
            legend.title=element_text(size=rel(1.5)),
            legend.text=element_text(size=rel(1.5)))


## Attēlu saglabāšana

ggsave("1_attels.png",width=8,height=5)

## Attēla veidošana no objektiem

p<-ggplot(mtcars,aes(as.factor(cyl),mpg))+geom_boxplot()

noformejums<-  theme(axis.text.y=element_text(size=rel(1.2),face="bold"),
                     axis.text.x=element_text(size=rel(1.2),face="bold",angle=90,vjust=0.5),
                     axis.title=element_text(size=rel(1.5),face="bold"),
                     axis.line=element_line(color="black"),
                     panel.background=element_blank(),
                     panel.grid.minor=element_blank(),
                     panel.grid.major=element_line(color="grey90"),
                     legend.position="top",
                     legend.key=element_rect(fill="white"),
                     legend.title=element_text(size=rel(1.5)),

p+noformejums

ggplot(mtcars,aes(mpg))+geom_histogram()+noformejums


# Tipiskās problēmas veidojot ggplot2 attēlus
## Stabiņu secība

df<-data.frame(x=c("A","B","C","D"),y=c(14,23,18,8))
ggplot(df,aes(x,y))+geom_bar(stat="identity")

ggplot(df,aes(reorder(x,y),y))+geom_bar(stat="identity")


## Vairākas līnijas attēlā

df<-data.frame(
      gads=1900:2000,
      viens=rep(1,101),
      divi=rep(2,101),
      tris=rep(3,101))
head(df)

library(reshape2)
df.long<-melt(df,id.vars="gads")
head(df.long)

ggplot(df.long,aes(gads,value,color=variable))+geom_line()

ggplot(df,aes(x=gads))+
      geom_line(aes(y=viens,color="viens"))+
      geom_line(aes(y=divi,color="divi"))+
      geom_line(aes(y=tris,color="tris"))
