library(ggplot2)
setwd("~/AUCA courses/Intro to Data Science with R/Seminar3")
dat <- read.csv("EconomistData.csv")
ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+geom_point()

ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+geom_point(size=2, shape=23)

- geom_text(): adds text directly to the plot
- geom_label(): draws a rectangle underneath the text, making it easier to read.
- annotate(): useful for adding small text annotations at a particular location on the plot
- annotation_custom(): Adds static annotations that are the same in every panel
ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+geom_point(size=2, shape=23)+geom_text(label=rownames(dat))

rownames(dat) <- dat[,2]
ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+geom_point(size=2, shape=23)+geom_text(label=rownames(dat))

pointsToLabel <- c("Russia", "Kyrgyzstan", "Uzbekistan", "France", "Germany", "Moldova", "Tajikistan")
ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+geom_point(size=2, shape=5)+geom_text(aes(label=Country), data = subset(dat, Country %in% pointsToLabel))

library(ggrepel)
ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+geom_point(size=2)+geom_text_repel(aes(label=Country), data = subset(dat, Country %in% pointsToLabel), force=1)

ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+geom_point(size=2)+geom_label_repel(aes(label=Country), data = subset(dat, Country %in% pointsToLabel), force=1)

ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+geom_label_repel(aes(label=Country), data = subset(dat, Country %in% pointsToLabel), force=40)+geom_point(size=2)

ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+
geom_label_repel(aes(label=Country), data = subset(dat, Country %in% pointsToLabel), force=40, size=2.5, min.segment.length = 0)+
geom_point(size=2)

ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+
geom_label_repel(aes(label=Country), data = subset(dat, Country %in% pointsToLabel), force=40, size=2.5, min.segment.length = 0)+
geom_point(size=2) + labs(x="Corruption Percieved Index", y="Human Development Index", title="Corruption and Human Development")

ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+
geom_label_repel(aes(label=Country), data = subset(dat, Country %in% pointsToLabel), force=40, size=2.5, min.segment.length = 0)+
geom_point(size=2) + labs(x="Corruption Percieved Index", y="Human Development Index", title="Corruption and Human Development") + theme_bw()

ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+
geom_label_repel(aes(label=Country), data = subset(dat, Country %in% pointsToLabel), force=40, size=2.5, min.segment.length = 0)+
geom_point(size=2) + labs(x="Corruption Percieved Index", y="Human Development Index", title="Corruption and Human Development") + theme_dark()

ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+
geom_label_repel(aes(label=Country), data = subset(dat, Country %in% pointsToLabel), force=40, size=2.5, min.segment.length = 0)+
geom_point(size=2) + labs(x="Corruption Percieved Index", y="Human Development Index", title="Corruption and Human Development") + theme_bw() +geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(data = dat, aes(x=CPI, y=HDI))+
geom_label_repel(aes(label=Country), data = subset(dat, Country %in% pointsToLabel), force=40, size=2.5, min.segment.length = 0)+
geom_point(aes(color = Region),size=2) + labs(x="Corruption Percieved Index", y="Human Development Index", title="Corruption and Human Development") + theme_bw() +geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(data = dat, aes(x=CPI, y=HDI))+
geom_label_repel(aes(label=Country), data = subset(dat, Country %in% pointsToLabel), force=40, size=2.5, min.segment.length = 0)+
geom_point(aes(color = Region),size=2) + labs(x="Corruption Percieved Index", y="Human Development Index", title="Corruption and Human Development") + theme_bw() +geom_smooth(method=lm, color="red", se=FALSE)
## `geom_smooth()` using formula 'y ~ x'

library(hrbrthemes)
ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+
geom_label_repel(aes(label=Country), data = subset(dat, Country %in% pointsToLabel), force=40, size=2.5, min.segment.length = 0)+
geom_point(size=2) + labs(x="Corruption Percieved Index", y="Human Development Index", title="Corruption and Human Development") + theme_ipsum()
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): семейство
## шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): семейство
## шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): семейство
## шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): семейство
## шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## семейство шрифтов не найдено в базе данных шрифтов Windows

library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+
geom_label_repel(aes(label=Country), data = dat %>% filter(HDI<0.5), force=20, size=2.5)+
geom_point(size=2)
## Warning: ggrepel: 35 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

library(dplyr)
ggplot(data = dat, aes(x=CPI, y=HDI, color = Region))+
geom_label_repel(aes(label=Country), data = dat %>% filter(HDI<0.4), force=20, size=2.5, max.overlaps = Inf)+
geom_point(size=2)
