library(tidyverse)
library(ggpubr) 
library(ggplot2)
library(grid)
library(gridExtra)
theme_set(
  theme_bw(base_size=26) + 
    theme(legend.position = "top")
)

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
library("ggpubr")
p <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  geom_smooth(method = lm) +
  stat_cor(method = "pearson", label.x = 20)
p

ggplot(mpg, aes(cty, hwy)) +
  geom_jitter(size = 2, width = 1, alpha=0.3)

ggplot(mpg, aes(cty, hwy)) +
  geom_count()

df <- mtcars %>%
  rownames_to_column() %>%
  as_data_frame() %>%
  mutate(cyl = as.factor(cyl)) %>%
  select(rowname, wt, mpg, cyl)
df
## # A tibble: 32 x 4
##    rowname              wt   mpg cyl  
##    <chr>             <dbl> <dbl> <fct>
##  1 Mazda RX4          2.62  21   6    
##  2 Mazda RX4 Wag      2.88  21   6    
##  3 Datsun 710         2.32  22.8 4    
##  4 Hornet 4 Drive     3.22  21.4 6    
##  5 Hornet Sportabout  3.44  18.7 8    
##  6 Valiant            3.46  18.1 6    
##  7 Duster 360         3.57  14.3 8    
##  8 Merc 240D          3.19  24.4 4    
##  9 Merc 230           3.15  22.8 4    
## 10 Merc 280           3.44  19.2 6    
## # ... with 22 more rows
ggplot(df, aes(x = reorder(rowname, mpg), y = mpg)) +
  geom_col()  +
  rotate_x_text(angle = 65)+xlab("model")

ggplot(df, aes(x = reorder(rowname, mpg), y = mpg)) +
  geom_col( aes(fill = cyl)) + 
  geom_text(aes(label = mpg), nudge_y = 2) + 
  coord_flip() +
  scale_fill_viridis_d()+xlab("model")

library(ggforce)
theme_set(
  theme_bw(base_size=20) + 
    theme(legend.position = "top")
)

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
  geom_point(cex=3) +
  facet_zoom(x = Species == "versicolor")

library("ggalt")
circle.df <- iris %>% filter(Species == "setosa")
ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_point(aes(colour = Species), cex=4) + 
  geom_encircle(data = circle.df, linetype = 4)

p= ggscatterhist(
  iris, x = "Sepal.Length", y = "Sepal.Width",
  color = "Species", size = 6, alpha = 0.6,
  palette = c("#798E87", "#046C9A", "#C7B19C"),
  margin.params = list(fill = "Species", 
                       color = "black", size = 1)
) 

ggpar(p,
        font.main = c(25,"bold.italic", "red"),
        font.x = c(20, "bold", "black"),
        font.y = c(20, "bold", "black"),
        font.xtickslab= c(14,  "black"),
        font.ytickslab= c(14,  "black"),
         font.legend= c(18,  "black")
      )

p= ggscatterhist(
  iris, x = "Sepal.Length", y = "Sepal.Width",
  color = "Species", size = 3, alpha = 0.6,
  palette = c("#798E87", "#046C9A", "#B40F20"),
  margin.plot = "boxplot",
  ggtheme = theme_bw()
)

ggpar(p,
      font.main = c(25,"bold.italic", "red"),
      font.x = c(20, "bold", "black"),
      font.y = c(20, "bold", "black"),
      font.xtickslab= c(14,  "black"),
      font.ytickslab= c(14,  "black"),
      font.legend= c(18,  "black")
)

library(ggridges)
p= ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(aes(fill = Species)) +
  scale_fill_manual(values = c("#798E87", "#046C9A", "#B40F20"))

ggpar(p,
      font.main = c(25,"bold.italic", "red"),
      font.x = c(20, "bold", "black"),
      font.y = c(20, "bold", "black"),
      font.xtickslab= c(14,  "black"),
      font.ytickslab= c(14,  "black"),
      font.legend= c(18,  "black")
)

# Data
df3 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                  dose=rep(c("D0.5", "D1", "D2"),2),
                  len=c(6.8, 15, 33, 4.2, 10, 29.5))

# Line plot
ggplot(df3, aes(x = dose, y = len, group = supp)) +
  geom_line(aes(linetype = supp), cex=2) +
  geom_point(aes(shape = supp))

# Data preparation
df <- economics %>%
  select(date, psavert, uempmed) %>%
  gather(key = "variable", value = "value", -date)
head(df, 3)
## # A tibble: 3 x 3
##   date       variable value
##   <date>     <chr>    <dbl>
## 1 1967-07-01 psavert   12.6
## 2 1967-08-01 psavert   12.6
## 3 1967-09-01 psavert   11.9
theme_set(
  theme_bw(base_size=26) + 
    theme(legend.position = "top")
)

ggplot(df, aes(x = date, y = value)) + 
  geom_line(aes(color = variable), size = 3) +
  scale_color_manual(values = c("#00AFBB", "#E7B800"))

library(ggplot2)
library(ssdtools)
data(boron_data)
fit <- ssd_fit_dists(boron_data, dists = c("llogis", "lnorm", "gamma"))
fit.plot <- autoplot(fit)
fit.plot

#### https://github.com/adamribaudo/storytelling-with-data-ggplot/tree/master/plot%20output