---
title: "Data Visualization"
author: "Introduction to R for Public Health Researchers"
output:
  ioslides_presentation:
    css: ../styles.css
    widescreen: yes
---

```{r knit-setup, echo=FALSE, include = FALSE}
library(knitr)
opts_chunk$set(echo = TRUE, 
               message = FALSE, 
               warning = FALSE,
               fig.height = 4,
               fig.width = 7, 
               comment = "")
library(tidyverse)
library(jhur)
```


```{r seed, comment="",echo=FALSE, }
set.seed(3) 
```


## Read in Data

```{r plotEx,  fig.align='center',cache=FALSE}
library(readr)
mort = read_csv(
  "http://johnmuschelli.com/intro_to_r/data/indicatordeadkids35.csv")
mort[1:2, 1:5]
```


## Read in Data: `jhur`

```{r fig.align='center',cache=FALSE}
jhur::read_mortality()
```


```{r}
mort = mort %>% rename(country = X1)
mort[1:2, 1:5]
```

# Data are not Tidy!

## ggplot2

Let's try this out on the childhood mortality data used above. However, let's do some manipulation first, by using `gather` on the data to convert to long. 

```{r}
library(tidyverse)
long = mort
long = long %>% gather(year, morts, -country)
head(long, 2)
```

## ggplot2

Let's also make the year numeric, as we did above in the stand-alone `year` variable. 

```{r}
library(stringr)
library(dplyr)
long$year = long$year %>% str_replace("^X", "") %>% as.numeric
long = long %>% filter(!is.na(morts))
```

## Plot the long data

```{r plot_long_swede, fig.align='center'}
swede_long = long %>% filter(country == "Sweden")
qplot(x = year, y = morts, data = swede_long)
```

## Plot the long data only up to 2012

```{r plot_long_swede_xlim, fig.align='center', warning=FALSE}
qplot(x = year, y = morts, data = swede_long, xlim = c(1760,2012))
```

## ggplot2

`ggplot2` is a package of plotting that is very popular and powerful (using the **g**rammar of **g**raphics). `qplot` ("quick plot"), similar to `plot`


```{r}
library(ggplot2)
qplot(x = year, y = morts, data = swede_long)
```


## ggplot2

The generic plotting function is `ggplot`, which uses **aes**thetics:

```
ggplot(data, aes(args))
```

```{r generic_gg, comment="",  fig.align='center', cache=FALSE}
g = ggplot(data = swede_long, aes(x = year, y = morts))
```

`g` is an object, which you can adapt into multiple plots!


## ggplot2

Common aesthetics:

- **x**
- **y**
- **colour/color**
- **size**
- **fill** 
- **shape**


If you set these in `aes`, you set them to a variable.  If you want to set them for all values, set them in a `geom`.


## ggplot2

You can do this most of the time using `qplot`, but `qplot` will assume a scatterplot if `x` and `y` are specified and histogram if `x` is specified:

```{r gen_q, comment="",  fig.align="center", cache=FALSE}
q = qplot(data = swede_long, x = year, y = morts)
q
```

`g` is an object, which you can adapt into multiple plots!

## ggplot2: what's a `geom`?

`g` on it's own can't be plotted, we have to add `layers`, usually with `geom_` commands:

- `geom_point` - add points
- `geom_line` - add lines
- `geom_density` - add a density plot
- `geom_histogram` - add a histogram
- `geom_smooth` - add a smoother
- `geom_boxplot` - add a boxplots
- `geom_bar` - bar charts
- `geom_tile` - rectangles/heatmaps


## ggplot2: adding a geom and assigning

You "add" things to a plot with a `+` sign (not pipe!).  If you assign a plot to an object, you must call `print` to print it.  

```{r gprint_point}
gpoints = g + geom_point(); print(gpoints) # one line for slides
```

## ggplot2: adding a geom 

Otherwise it prints by default - this time it's a line

```{r geom_line}
g + geom_line()
```

## ggplot2: adding a geom

You can add multiple geoms:

```{r geom_line_point}
g + geom_line() + geom_point()
```

## ggplot2: adding a smoother

Let's add a smoother through the points:

```{r line_smooth}
g + geom_line() + geom_smooth()
```

## ggplot2: grouping - using colour

If we want a plot with new data, call `ggplot` again.  Group plots by `country` using `colour` (piping in the data):

```{r geom_all}
sub = long %>% filter(country %in% c("United States", "United Kingdom", 
    "Sweden", "Afghanistan", "Rwanda"))
g = sub %>% ggplot(aes(x = year, y = morts, colour = country))
g + geom_line()
```


## Coloring manually

There are many `scale_AESTHETICS_*` functions and `scale_AESTHETICS_manual` allows to directly specify the colors:

```{r geom_color}
g + geom_line() + scale_colour_manual(values = 
    c("United States" = "blue", "United Kingdom" = "green", 
      "Sweden" = "black", "Afghanistan" = "red", "Rwanda" = "orange"))
```

## ggplot2: grouping - using colour

Let's remove the legend using the `guide` command:

```{r geom_noguide}
g + geom_line() + guides(colour = FALSE)
```

## Lab Part 1

[Website](http://johnmuschelli.com/intro_to_r/index.html)


## ggplot2: boxplot

```{r geom_box}
ggplot(long, aes(x = year, y = morts)) + geom_boxplot()
```

## ggplot2: boxplot

For different plotting per year - must make it a factor - but x-axis is wrong!
```{r geom_box_fac}
ggplot(long, aes(x = factor(year), y = morts)) + geom_boxplot()
```

## ggplot2: boxplot

```{r geom_box_grouped}
ggplot(long, aes(x = year, y = morts, group = year)) + geom_boxplot()
```

## ggplot2: boxplot with points

- `geom_jitter` plots points "jittered" with noise so not overlapping

```{r geom_box_jitter}
sub_year = long %>% filter( year > 1995 & year <= 2000)
ggplot(sub_year, aes(x = factor(year), y = morts)) + 
  geom_boxplot(outlier.shape = NA) + # don't show outliers - will below
  geom_jitter(height = 0)
```

## facets: plotting multiple panels

A `facet` will make a plot over variables, keeping axes the same (out can change that):

```{r}
sub %>% ggplot(aes(x = year, y = morts)) + 
  geom_line() + 
  facet_wrap(~ country)
```

## facets: plotting multiple panels


```{r}
sub %>% ggplot(aes(x = year, y = morts)) + 
  geom_line() + 
  facet_wrap(~ country, ncol = 1)
```

## facets: plotting multiple panels

You can use `facets` in `qplot`

```{r}
qplot(x = year, y = morts, geom = "line", facets = ~ country, data = sub)
```


## facets: plotting multiple panels

You can also do multiple factors with `+` on the right hand side
```{r, eval = FALSE}
sub %>% ggplot(aes(x = year, y = morts)) + 
  geom_line() + 
  facet_wrap(~ country + x2 + ... )
```


## Lab Part 2

[Website](http://johnmuschelli.com/intro_to_r/index.html)


## Devices

By default, R displays plots in a separate panel. From there, you can export the plot to a variety of image file types, or copy it to the clipboard. 

However, sometimes its very nice to save many plots made at one time to one pdf file, say, for flipping through. Or being more precise with the plot size in the saved file.

R has 5 additional graphics devices: `bmp()`, `jpeg()`, `png()`, `tiff()`, and `pdf()`

## Devices

The syntax is very similar for all of them:

```
pdf("filename.pdf", width=8, height=8) # inches
plot() # plot 1
plot() # plot 2
# etc
dev.off()
```

Basically, you are creating a pdf file, and telling R to write any subsequent plots to that file. Once you are done, you turn the device off. Note that failing to turn the device off will create a pdf file that is corrupt, that you cannot open.


## Saving the output:

```{r labs_save, eval = TRUE}
png("morts_over_time.png")
print(q)
dev.off()
file.exists("morts_over_time.png")
```

```{r labs_del, echo = FALSE, results="hide"}
file.remove("morts_over_time.png")
```

## Saving the output

There's also a `ggsave` function that is useful for saving a single `ggplot` object. 

## Labels and such

- `xlab`/`ylab` - functions to change the labels; `ggtitle` - change the title 

```{r labs, eval = TRUE}
q = qplot(x = year, y = morts, colour = country, data = sub,
          geom = "line") + 
  xlab("Year of Collection") + ylab("morts /100,000") +
  ggtitle("Mortality of Children over the years", subtitle = "not great") 
q
```

## Themes

- see `?theme_bw` - for ggthemes - black and white

```{r theme_bw, eval = TRUE}
q + theme_bw()
```

## Themes: change plot parameters

- `theme` - global or specific elements/increase text size 

```{r theme, eval = TRUE}
q + theme(text = element_text(size = 12), title = element_text(size = 20))
```


## Themes

```{r theme2, eval = TRUE}
q = q + theme(axis.text = element_text(size = 14),
          title = element_text(size = 20),
          axis.title = element_text(size = 16),
          legend.position = c(0.9, 0.8)) + 
  guides(colour = guide_legend(title = "Country"))
q
```

## Code for a transparent legend
```{r translegend}
transparent_legend =  theme(legend.background = element_rect(
    fill = "transparent"),
  legend.key = element_rect(fill = "transparent", 
                            color = "transparent") )
q + transparent_legend
```


## Lab Part 3

[Website](http://johnmuschelli.com/intro_to_r/index.html)


<!-- ## Histograms again -->

<!-- We can do histograms again using `hist`.  Let's do histograms of mort rates over the years: -->

<!-- ```{r hist_mort, comment="", fig.align='center', cache=FALSE} -->
<!-- hist(sub$morts, breaks = 200) -->
<!-- ``` -->

## Histograms again: Changing bins
 
```{r qhist_mort, comment="", fig.align='center', cache=FALSE}
qplot(x = morts, data = sub, bins = 200)
```

## Multiple Histograms

```{r ghist, comment="", fig.align='center', cache = FALSE}
qplot(x = morts, fill = factor(country),
      data = sub, geom = c("histogram"))
```

## Multiple Histograms

Alpha refers to the opacity of the color, less is more opaque
```{r ghist_alpha, comment="", fig.align='center', cache = FALSE}
qplot(x = morts, fill = country, data = sub, 
      geom = c("histogram"), alpha=.7)
```

## Multiple Densities

We cold also do densities:
```{r gdens, comment="", fig.align='center', cache = FALSE}
qplot(x= morts, fill = country, data = sub, 
      geom = c("density"), alpha= .7)  + guides(alpha = FALSE)
```


## Multiple Densities

- using `colour` not `fill`:

```{r gdens_alpha, comment="", fig.align='center', cache = FALSE}
qplot(x = morts, colour = country, data = sub, 
      geom = c("density"), alpha= .7) + guides(alpha = FALSE)
```



## Multiple Densities

You can take off the lines of the bottom like this
```{r gdens_line_alpha_mort, comment="", fig.align='center', cache = FALSE}
ggplot(aes(x = morts, colour = country), data = sub) + 
  geom_line(stat = "density")
```

## ggplot2

```{r geom_line_qplot_nocol, comment="", fig.align='center', cache=FALSE}
qplot(x = year, y = morts, colour = country, 
    data = long, geom = "line") + guides(colour = FALSE)
```

## ggplot2

Let's try to make it different like base R, a bit.  We use `tile` for the geom:

```{r geom_tile_qtile}
qtile = qplot(x = year, y = country, fill = morts, data = sub, 
              geom = "tile") + xlim(1990, 2005) + guides(colour = FALSE)
```
```{r geom_tile_qtile_show, echo = FALSE}
print(qtile)
```

## ggplot2: changing colors

`scale_fill_gradient` let's us change the colors for the fill:

```{r geom_tile_rescale}
qtile + scale_fill_gradient( low = "blue", high = "red")
```

## ggplot2

Let's try categories.

```{r geom_tile_cut}
sub$cat = cut(sub$morts, breaks = c(0, 1, 2, max(sub$morts)))
q2 = qplot(x = year, y = country, fill = cat, data = sub, geom = "tile") + 
  guides(colour = FALSE)
```
```{r geom_tile_qtile2_show, echo = FALSE}
print(q2)
```


## Colors

It's actually pretty hard to make a good color palette. Luckily, smart and artistic people have spent a lot more time thinking about this. The result is the `RColorBrewer` package

`RColorBrewer::display.brewer.all()` will show you all of the palettes available. You can even print it out and keep it next to your monitor for reference.

The help file for `brewer.pal()` gives you an idea how to use the package.

You can also get a "sneak peek" of these palettes at: http://colorbrewer2.org/ . You would provide the number of levels or classes of your data, and then the type of data: sequential, diverging, or qualitative. The names of the `RColorBrewer` palettes are the string after 'pick a color scheme:' 


## ggplot2: changing colors

`scale_fill_brewer` will allow us to use these palettes conveniently

```{r geom_tile_rescale_brewer}
q2 + scale_fill_brewer( type = "div", palette =  "RdBu" )
```


## Bar Plots with a table

```{r barplot2, fig.align='center', cache = FALSE, warn = FALSE}
cars = read_csv(
  "http://johnmuschelli.com/intro_to_r/data/kaggleCarAuction.csv", 
  col_types = cols(VehBCost = col_double()))
counts <- table(cars$IsBadBuy, cars$VehicleAge)
```

## Bar Plots
* Stacked Bar Charts are sometimes wanted to show distributions of data

```{r barplot2_2, fig.align='center', cache = FALSE, warn = FALSE}
barplot(counts, main="Car Distribution by Age and Bad Buy Status", xlab="Vehicle Age", col=c("darkblue","red"), legend = rownames(counts))
```


## Bar Plots
`prop.table` allows you to convert a table to proportions (depends on margin - either row percent or column percent)

```{r barplot2a, fig.align='center', cache = FALSE}
## Use percentages (column percentages)
barplot(prop.table(counts, 2), 
        main = "Car Distribution by Age and Bad Buy Status",
  xlab="Vehicle Age", col=c("darkblue","red"),
    legend = rownames(counts))
```



## Bar Plots

```{r barplot2_stacked_geom, fig.align='center', cache = FALSE}
ggplot(aes(fill = factor(IsBadBuy), x = VehicleAge), 
       data = cars) + geom_bar()
```

## Normalized Stacked Bar charts

- we must calculate percentages on our own

```{r make_perc_data, fig.align='center', cache = FALSE}
perc = cars %>% 
  group_by(IsBadBuy, VehicleAge) %>% 
  tally() %>% ungroup
head(perc)
```

## Each Age adds to 1

```{r norm_bar_1}
perc_is_bad = perc %>% 
  group_by(VehicleAge) %>% mutate(perc = n / sum(n))
ggplot(aes(fill = factor(IsBadBuy), 
           x = VehicleAge, 
           y = perc), 
       data = perc_is_bad) + geom_bar(stat = "identity")
```

## Each Bar adds to 1 for bad buy or not

```{r norm_bad_buy}
perc_yr = perc %>% 
  group_by(IsBadBuy) %>% mutate(perc = n / sum(n))
ggplot(aes(fill = factor(VehicleAge), 
           x = IsBadBuy, 
           y = perc), 
       data = perc_yr) + geom_bar(stat = "identity")
```


## ggplot2 

Useful links:

* http://docs.ggplot2.org/0.9.3/index.html
* http://www.cookbook-r.com/Graphs/


## Website

[Website](http://johnmuschelli.com/intro_to_r/index.html)


# ggplot examples on a second dataset

## Multiple Histograms

```{r ghist_chick, comment="", fig.align='center', cache = FALSE}
qplot(x = weight, 
      fill = factor(Diet),
      data = ChickWeight, 
      geom = c("histogram"))
```

## Multiple Histograms

Alpha refers tot he opacity of the color, less is 
```{r ghist_chick_alpha, comment="", fig.align='center', cache = FALSE}
qplot(x = weight, fill = Diet, data = ChickWeight, 
      geom = c("histogram"), alpha=.7)
```

## Multiple Densities

We cold also do densities
```{r gdens_chick, comment="", fig.align='center', cache = FALSE}
qplot(x= weight, fill = Diet, data = ChickWeight, 
      geom = c("density"), alpha= .7)
```


## Multiple Densities

```{r gdens_chick_alpha, comment="", fig.align='center', cache = FALSE}
qplot(x= weight, colour = Diet, data = ChickWeight, 
      geom = c("density"), alpha=.7)
```


## Multiple Densities

```{r gdens_alpha_gg, comment="", fig.align='center', cache = FALSE}
ggplot(aes(x= weight, colour = Diet), 
  data = ChickWeight) + geom_density(alpha=.7)
```

## Multiple Densities

You can take off the lines of the bottom like this
```{r gdens_line_alpha, comment="", fig.align='center', cache = FALSE}
ggplot(aes(x = weight, colour = Diet), data = ChickWeight) + 
  geom_line(stat = "density")
```

## Spaghetti plot
 
We can make a spaghetti plot by telling ggplot we want a "line", and each line is colored by Chick.

```{r spaghetti, comment="", fig.align='center', cache=FALSE}
qplot(x=Time, y=weight, colour = factor(Chick), 
      data = ChickWeight, geom = "line")
```



## Spaghetti plot: Facets

In ggplot2, if you want separate plots for something, these are referred to as facets.

```{r fac_spag, comment="", fig.align='center', cache=FALSE}
qplot(x = Time, y = weight, colour = factor(Chick), 
      facets = ~Diet, data = ChickWeight, geom = "line")
```


## Spaghetti plot: Facets

We can turn off the legend (referred to a "guide" in ggplot2). (Note - there is different syntax with the `+`)
```{r fac_spag_noleg, comment="", fig.align='center', cache=FALSE}
qplot(x=Time, y=weight, colour = factor(Chick), 
      facets = ~ Diet,  data = ChickWeight, 
        geom = "line") + guides(colour=FALSE)
```

## Spaghetti plot: Facets

```{r fac_spag2, comment="", fig.align='center', cache=FALSE}
ggplot(aes(x = Time, y = weight, colour = factor(Chick)), 
    data = ChickWeight) + geom_line() + 
    facet_wrap(facets = ~Diet) + guides(colour = FALSE)
```


# Base Graphics - explore on your own



## Basic Plots

```{r plot1, comment="",  fig.align='center',cache = FALSE}
library(dplyr)
sweden = mort %>% 
  filter(country == "Sweden") %>% 
  select(-country)
year = as.numeric(colnames(sweden))
plot(as.numeric(sweden) ~ year)
```



## Base Graphics parameters

Set within most plots in the base 'graphics' package:

* pch = point shape, http://voteview.com/symbols_pch.htm
* cex = size/scale
* xlab, ylab = labels for x and y axes
* main = plot title
* lwd = line density
* col = color
* cex.axis, cex.lab, cex.main = scaling/sizing for axes marks, axes labels, and title


## Basic Plots

The y-axis label isn't informative, and we can change the label of the y-axis using `ylab` (`xlab` for x), and `main` for the main title/label.

```{r plotEx2, comment="",  fig.align='center',cache = FALSE}
plot(as.numeric(sweden) ~ year,
      ylab = "# of morts per family", main = "Sweden", type = "l")
```


## Basic Plots

Let's drop any of the projections and keep it to year 2012, and change the points to blue. 
```{r plotEx3, fig.align='center', cache = FALSE}
plot(as.numeric(sweden) ~ year,
      ylab = "# of morts per family", main = "Sweden",
     xlim = c(1760,2012), pch = 19, cex=1.2,col="blue")
```


## Basic Plots
You can also use the `subset` argument in the `plot()` function, only when using formula notation:

```{r plotEx_sub, fig.align='center', cache = FALSE}
plot(as.numeric(sweden) ~ year,
      ylab = "# of morts per family", main = "Sweden",
     subset = year < 2015, pch = 19, cex=1.2,col="blue")
```

## Bar Plots

Using the `beside` argument in `barplot`, you can get side-by-side barplots.
```{r barplot3, fig.align='center', cache = FALSE}
# Stacked Bar Plot with Colors and Legend    
barplot(counts, main="Car Distribution by Age and Bad Buy Status",
  xlab="Vehicle Age", col=c("darkblue","red"),
    legend = rownames(counts), beside=TRUE)
```


## Boxplots, revisited

These are one of my favorite plots. They are way more informative than the barchart + antenna...

```{r boxplots, comment="", fig.align='center', cache=FALSE}
boxplot(weight ~ Diet, data=ChickWeight, outline=FALSE)
points(ChickWeight$weight ~ jitter(as.numeric(ChickWeight$Diet),0.5))
```

## Formulas

Formulas have the format of `y ~ x` and functions taking formulas have a `data` argument where you pass the data.frame.  You don't need to use `$` or referencing when using formulas:

```{r box_ex, eval=FALSE}
boxplot(weight ~ Diet, data=ChickWeight, outline=FALSE)
```


## Colors

R relies on color 'palettes'.

```{r pal, fig.align='center', cache = FALSE}
palette("default")
plot(1:8, 1:8, type="n")
text(1:8, 1:8, lab = palette(), col = 1:8)
```


## Colors

The default color palette is pretty bad, so you can try to make your own. 

```{r pal2, fig.align='center', cache = FALSE}
palette(c("darkred","orange","blue"))
plot(1:3,1:3,col=1:3,pch =19,cex=2)
```




## Colors

```{r pal5, fig.align='center', cache=FALSE}
library(RColorBrewer)
palette(brewer.pal(5,"Dark2"))
plot(weight ~ jitter(Time,amount=0.2),data=ChickWeight,
     pch = 19,  col = Diet,xlab="Time")
```



## Adding legends

The legend() command adds a legend to your plot. There are tons of arguments to pass it. 

x, y=NULL: this just means you can give (x,y) coordinates, or more commonly just give x, as a character string: "top","bottom","topleft","bottomleft","topright","bottomright".

legend: unique character vector, the levels of a factor

pch, lwd: if you want points in the legend, give a pch value. if you want lines, give a lwd value. 

col: give the color for each legend level


## Adding legends


```{r leg1, fig.align='center', cache=FALSE}
palette(brewer.pal(5,"Dark2"))
plot(weight ~ jitter(Time,amount=0.2),data=ChickWeight,
                pch = 19,  col = Diet,xlab="Time")
legend("topleft", paste("Diet",levels(ChickWeight$Diet)), 
        col = 1:length(levels(ChickWeight$Diet)),
       lwd = 3, ncol = 2)
```


## Coloring by variable

```{r circ, comment="", fig.align='center', cache=FALSE}
circ = read_csv("http://johnmuschelli.com/intro_to_r/data/Charm_City_Circulator_Ridership.csv")
palette(brewer.pal(7,"Dark2"))
dd = factor(circ$day)
plot(orangeAverage ~ greenAverage, data=circ, 
     pch=19, col = as.numeric(dd))
legend("bottomright", levels(dd), col=1:length(dd), pch = 19)
```

## Coloring by variable

```{r circ2, comment="", fig.align='center', cache=FALSE}
dd = factor(circ$day, levels=c("Monday","Tuesday","Wednesday",
            "Thursday","Friday","Saturday","Sunday"))
plot(orangeAverage ~ greenAverage, data=circ,
     pch=19, col = as.numeric(dd))
legend("bottomright", levels(dd), col=1:length(dd), pch = 19)
```


