Sheet A

Column

geom_boxplot()

geom_boxplot(color = "blue") | coord_flip()

Column

scale_fill_brewer(palette = "Set2") | scale_x_discrete(limits = c("Europe", "Oceania", "Asia", "Americas", "Africa"))

scale_fill_manual(values = c("lightblue", "green", "yellow", "red", "purple" ))

Column

geom_boxplot(fill="limegreen", alpha=0.2

geom_boxplot(fill="cornflowerblue", color= "darkblue", alpha=0.2, notch = TRUE, notchwidth = 0.5, outlier.color = "firebrick1", outlier.fill = "red", outlier.size = 3)

Sheet B

Column

grafico_gdppercap$type=factor(ifelse(grafico_gdppercap$continent=="Europe","A","B")) | labs(fill= NULL)

scale_fill_manual(values = c("cornflowerblue", "firebrick1")) + scale_alpha_manual(values = c(0.6, 0.6)) |

Column

ggplot(data=gapminder_b, aes (x=continent, y=lifeExp, fill=year))

geom_boxplot(varwidth = TRUE, alpha = 0.3) | scale_y_continuous(labels = scales::dollar)

Column

x = reorder(continent, -pop)

continuous variable: aes(group = cut_width(lifeExp, 5))

Sheet C

Column

facet_wrap(~year, ncol=3)

Column

facet_wrap(~year, ncol=3, scale="free")

Sheet D

Column

facet_wrap(~year, ncol=2) | scale_fill_brewer(palette = "OrRd")

Column

facet_wrap(~year, ncol=2, scale="free") | scale_fill_brewer(palette = "OrRd")

Sheet E

Column

stat_summary(fun.y = mean, geom = "point", shape = 20, size = 5, color = "brown", fill = "black")

geom_point(position = "jitter", color= "grey50", alpha= 0.6)

Column

qplot( x=continent , y=gdpPercap , data=grafico_gdppercap , geom=c("boxplot","jitter") , fill=continent)

geom_hline(yintercept= 11680.07, linetype = "dashed", color = "red", size = 1)

Column

geom_jitter(position = position_jitter(0.2), color= "grey40", alpha= 0.6)

geom_dotplot(binaxis='y', stackdir='center', dotsize=1)

Sheet F

Column

You can customize the graph to your liking using the function theme()

---
title: "Data Visualizations: Box plots"
author: "Rubén F. Bustillo"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    source_code: embed
    vertical_layout: fill
    theme: spacelab
---

```{r setup, include=FALSE}


# PACKAGES / LIBRARIES:

library(flexdashboard)
library(tidyverse)
library(gapminder)
library(ggthemes)
library(ggthemr)



# MAIN DATASET:

gapminder <- gapminder::gapminder


```



Sheet A
====================================================================================


Column 
-----------------------------------------------------------------------

### `geom_boxplot()`

```{r}

# DATASET: GDP IN SELECTED COUNTRIES BY CONTINENT IN 2007

grafico_gdppercap <- gapminder %>%
  filter(year == 2007) %>%
  group_by(continent)


# GGTHEMR THEME: FLAT (Ful range of ggthemr themes in: https://rquer.netlify.com/ggplot_themes#themes-a)



ggthemr("flat")

# PLOT: BASIC BOXPLOT

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap, 
                              fill = continent)) +
  geom_boxplot() +
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita") 

```


### `geom_boxplot(color = "blue")` |  `coord_flip()`

```{r}

# PLOT: CHANGE COLOR TO BLUE:

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap, 
                              fill = continent)) +
  geom_boxplot(color = "blue") +
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita") +
  coord_flip() +
  scale_y_continuous(labels = scales::comma)

```



Column 
-----------------------------------------------------------------------


### `scale_fill_brewer(palette = "Set2")` | `scale_x_discrete(limits = c("Europe", "Oceania", "Asia", "Americas", "Africa"))`

```{r}

#PLOT: PALETTE = SET2 . ORDER OF X-LABELS HAVE BEEN MODIFIED 

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap, 
                              fill = continent)) +
  geom_boxplot() +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "GDp per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita") +
  scale_x_discrete(limits = c("Europe", "Oceania", "Asia", "Americas", "Africa"))

```


### `scale_fill_manual(values = c("lightblue", "green", "yellow", "red", "purple" ))`

```{r}

# PLOT: COLORS CHANGED MANUALLY AND LEGEND REMOVED

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap, 
                              fill = continent)) +
  geom_boxplot() +
  scale_fill_manual(values = c("lightblue", "green", "yellow", "red", "purple" )) +
  labs(title = "GDP percapita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita") +
  theme(legend.position="none")

```



Column 
-----------------------------------------------------------------------

### `geom_boxplot(fill="limegreen", alpha=0.2`

```{r}

# DEFINED BOXPLOT COLOR WITH FILL

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap)) +
  geom_boxplot(fill="limegreen", 
               alpha=0.2) +
  labs(title = "GDP percapita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita") 

```


### `geom_boxplot(fill="cornflowerblue", color= "darkblue", alpha=0.2, notch = TRUE, notchwidth = 0.5, outlier.color = "firebrick1", outlier.fill = "red", outlier.size = 3)`

```{r}

# PLOT: WITH NOTCHS AND OUTLIERS HIGHLIGHTED

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap)) +
  geom_boxplot(fill="cornflowerblue", 
               color= "darkblue",
               alpha=0.2,
               notch = TRUE,
               notchwidth = 0.5,
               outlier.color = "firebrick1",
               outlier.fill = "red",
               outlier.size = 4) +
  labs(title = "GDP percapita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita") 

```




Sheet B
====================================================================================


Column 
-----------------------------------------------------------------------

### `grafico_gdppercap$type=factor(ifelse(grafico_gdppercap$continent=="Europe","A","B"))` | `labs(fill= NULL)`

```{r}

# PLOT WITH ONE CONTINENT HIGHLIGHTED

grafico_gdppercap$type=factor(ifelse(grafico_gdppercap$continent=="Europe","A","B"))

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap, 
                              fill = type)) +
  geom_boxplot() +
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita",
       fill = NULL) 

```


### `scale_fill_manual(values = c("cornflowerblue", "firebrick1")) + scale_alpha_manual(values = c(0.6, 0.6))`  | 

```{r}

# PLOT: COLORS SELECTED MANUALLY

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap, 
                              fill = type,
                              alpha =  type)) +
  geom_boxplot() +
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita") +
  scale_fill_manual(values = c("cornflowerblue", "firebrick1")) + 
  scale_alpha_manual(values = c(0.6, 0.6)) 

```




Column 
-----------------------------------------------------------------------

### `ggplot(data=gapminder_b, aes (x=continent, y=lifeExp, fill=year))`

```{r}

# PLOT: COMPARING DATES (1952, 1977 AND 2007). LEGEND MOVED TO BOTTOM.

gapminder_b<- gapminder%>%
  filter(year == 1952 | year == 1977 | year == 2007) %>%
  mutate(year = factor(year))

ggplot(data=gapminder_b, aes (x=continent, 
                              y=lifeExp, 
                              fill=year)) +
  geom_boxplot()+
  labs(title = "Life expectancy of selected countries by continent",
       x = "",
       y = "Life Expectancy") +
  theme(legend.position = "bottom",
        legend.title = element_blank(),
        plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5))

```


###  `geom_boxplot(varwidth = TRUE, alpha = 0.3)` | `scale_y_continuous(labels = scales::dollar)`

```{r}

# PLOT: X-LABELS INDICATING THE NUMBER OF COUNTRIES IN EACH BOX AND BARS WIDTH INDICATING THE SIZE. X-AXIS INDICATING THE $ SIGN.

x_labels <- paste(levels(grafico_gdppercap$continent),"\n(N=", table(grafico_gdppercap$continent),")",sep="")

ggplot(grafico_gdppercap, aes(x = continent, y = gdpPercap, fill = continent)) +
  geom_boxplot(varwidth = TRUE, alpha = 0.3) +
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita") +
  scale_y_continuous(labels = scales::dollar) +
  scale_x_discrete(labels=x_labels)

```



Column 
-----------------------------------------------------------------------

### `x = reorder(continent, -pop)`

```{r}

# PLOT: REORDER CONTINENTS BY POPULATION (DESCENDING ORDER)

ggplot(grafico_gdppercap, aes(x = reorder(continent, -pop), 
                              y = gdpPercap, 
                              fill = continent)) +
  geom_boxplot() +
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita") 


```


### continuous variable: `aes(group = cut_width(lifeExp, 5))`

```{r}

# PLOT: DEALING WITH CONTINUOS VARIABLES (RANGES CREATED)

ggplot(grafico_gdppercap, aes(x=lifeExp, 
                              y=gdpPercap)) +
  geom_boxplot(fill="orange", aes(group = cut_width(lifeExp, 5))) +
  labs(title = "GDP per capita by ranges of life expectancy",
       subtitle = "year: 2007",
       x = "life Expectancy ranges",
       y = "GDP per capita") +
  scale_x_continuous(breaks = seq(40, 80, 5))

```




Sheet C
=============================================================

Column 
-----------------------------------------------------------------------


### `facet_wrap(~year, ncol=3)`

```{r, fig.height= 8, fig.width= 7}

# PLOT: FACETS AND X-LABELS CHANGED TO SHORT NAMES

ggplot(gapminder, aes(x=continent, 
                      y=lifeExp)) + 
  geom_boxplot() +
  facet_wrap(~year, ncol=3) +
  labs(title = "Life expectancy by continent and year",
       x = NULL,
       y = "Life Expectancy") +
  scale_x_discrete(labels = c("AFR", "AME", "ASI", "EUR", "OCE"))

```


Column 
-----------------------------------------------------------------------

### `facet_wrap(~year, ncol=3, scale="free")`

```{r, fig.height= 7, fig.width= 7}

# PLOT: FACETS WITH SCALE= FREE AND X-LABELS ROTATED

ggplot(gapminder, aes(x=continent, 
                      y=lifeExp)) + 
  geom_boxplot() +
  facet_wrap(~year, 
             ncol=3, 
             scale="free")+
  labs(title = "Life expectancy by continent and year",
       x = NULL,
       y = "Life Expectancy") +
  theme(axis.text.x = element_text(size = rel(0.7),
                                   angle = 45,
                                   hjust = 1),
        axis.text.y = element_text(size = rel(0.7)))

```




Sheet D
=============================================================

Column 
-----------------------------------------------------------------------


### `facet_wrap(~year, ncol=2)` |  `scale_fill_brewer(palette = "OrRd")`

```{r, fig.height= 8, fig.width= 7}

# PLOT: FACETS BY CONTINENT AND YEAR. COLOR PALETTE = OrRd

ggplot(gapminder_b, aes(x=year, 
                        y=lifeExp, 
                        fill=year)) + 
  geom_boxplot() +
  facet_wrap(~continent, 
             ncol=2) +
  labs(title = "Life expectancy by continent and year",
       x = NULL,
       y = "Life Expectancy") +
  scale_fill_brewer(palette = "OrRd")

```


Column 
-----------------------------------------------------------------------

### `facet_wrap(~year, ncol=2, scale="free")` | `scale_fill_brewer(palette = "OrRd")`

```{r, fig.height= 7, fig.width= 7}

# PLOT: FACETS WITH SCALE =FREE. 

ggplot(gapminder_b, aes(x=year, 
                        y=lifeExp, 
                        fill=continent)) + 
  geom_boxplot() +
  facet_wrap(~continent, 
             ncol=2, 
             scale="free") +
  labs(title = "Life expectancy by continent and year",
       x = NULL,
       y = "Life Expectancy") +
  scale_fill_brewer(palette = "OrRd")

```


Sheet E
==============================================================================


Column 
-----------------------------------------------------------------------

### `stat_summary(fun.y = mean, geom = "point", shape = 20, size = 5, color = "brown", fill = "black")`

```{r}

# PLOT WITH STAT_SUMMARY INDICATING THE MEAN OF EACH BOX

ggplot(grafico_gdppercap, aes(x = continent, y = gdpPercap, fill = continent)) +
  geom_boxplot() +
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita") +
  stat_summary(fun.y = mean, geom = "point", shape = 20, size = 5, color = "brown", fill = "black") +
  theme(legend.position = "none")

```


### `geom_point(position = "jitter", color= "grey50", alpha= 0.6)`  

```{r}

# PLOT: ALL COUNTRIES WITH POSITION = JITTER. PANEL BACKGROUND REMOVED

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap, 
                              fill = continent)) +
  geom_boxplot() +
  geom_point(position = "jitter", color= "grey40", alpha= 0.6)+
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita")  +
  theme(legend.position = "none",
        panel.background = element_blank())

```



Column 
-----------------------------------------------------------------------

### `qplot( x=continent , y=gdpPercap , data=grafico_gdppercap , geom=c("boxplot","jitter") , fill=continent)`

```{r}

# PLOT: USING QPLOT 

qplot( x=continent , y=gdpPercap , data=grafico_gdppercap , geom=c("boxplot","jitter") , fill=continent)


```


###  `geom_hline(yintercept= 11680.07, linetype = "dashed", color = "red", size = 1)`

```{r}

# mean(grafico_gdppercap$gdpPercap) = 11680

# PLOT: INDICATING THE TOTAL MEAN WITH GEOM_HLINE

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap, 
                              fill = continent)) +
  geom_boxplot() +
  geom_point(position = "jitter", color= "grey40", alpha= 0.6)+
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita")  +
  theme(legend.position = "none",
        panel.background = element_blank()) +
  geom_hline(yintercept= 11680.07,
             linetype = "dashed",
             color = "red",
             size = 1.2)

```



Column 
-----------------------------------------------------------------------

### `geom_jitter(position = position_jitter(0.2), color= "grey40", alpha= 0.6)`

```{r}

#PLOT: ALL COUNTRIES WITH GEOM_JITTER

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap, 
                              fill = continent)) +
  geom_boxplot() +
  geom_jitter(position = position_jitter(0.2), 
              color= "grey40", 
              alpha= 0.6)+
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita")  +
  theme(legend.position = "none",
        panel.background = element_blank())

```


### `geom_dotplot(binaxis='y', stackdir='center', dotsize=1)`

```{r}

#PLOT: ALL COUNTRIES WITH GEOM_DOTPLOT

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap, 
                              fill = continent)) +
  geom_boxplot() +
  geom_dotplot(binaxis='y', 
               stackdir='center', 
               dotsize=1)+
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita")  +
  theme(legend.position = "none",
        panel.background = element_blank())

ggthemr_reset()

```


Sheet F
===================================================================

Column
-----------------------------------------------------------

### You can customize the graph to your liking using the function `theme()`

```{r, fig.width=8, fig.height=6}

# TEXT FONTS:

library(extrafont)
loadfonts(device = "win")

# PLOT: YOU CAN CUSTOMIZE THE GRAPH TO YOUR LIKING. AN EXAMPLE:

ggplot(grafico_gdppercap, aes(x = continent, 
                              y = gdpPercap, 
                              fill = continent)) +
  geom_boxplot(size = 1) +
  labs(title = "GDP per capita of selected countries by continent",
       subtitle = "year: 2007",
       x = "",
       y = "GDP per capita\n (US Dollars)")  +
  scale_y_continuous(labels = scales::comma) +
  theme(legend.position = "none",
        plot.title=element_text(size = 18, 
                                family= "Modern No. 20",
                                hjust = 0.5),
        plot.subtitle = element_text(size = 14, 
                                     family = "Modern No. 20",
                                     hjust = 0.5),
        text = element_text(size = 16, 
                            family="Modern No. 20"),
        axis.line.x = element_line(size = 1.1, 
                                   colour = "black"),
        axis.line.y = element_line(size = 1.1, 
                                   colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.text.x=element_text(colour="black", 
                                 size = 16),
        axis.text.y=element_text(colour="black", 
                                 size = 10))

```