-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-visualize.Rmd
49 lines (40 loc) · 2.58 KB
/
10-visualize.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
---
title: "Data visulization with ggplot2"
output:
html_document:
toc: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Overview
ggplot2 is a grammar of graphics, a coherent system for describing and building graphs.
- data (data frame): `ggplot(dataset)`
- aesthetic mapping (coordination system, color, size, line type, position etc): `aes()`
- geometries: `geom_point`, `geom_line`, `geom_bar`, ...
- stat (summarization/transformation of data): `stat_smooth`,
- scale (map data values into computer values): `scale_y_continuous()`, `scale_x_log10`, ...
- facet: `facet_wrap`, `facet_grid`
- fine tuning coordinatae, theme, labels: `coordinate_*`, `theme_bw`, `labs(x=..., y=...)`
## Tips
- Keep your data tidy (3 rules)
- Reshape your data (tall & thin often better for visualization & analysis)
- [Use factors (for reorder & better labeling)](https://blog.rstudio.com/2016/08/31/forcats-0-1-0/)
# Lesson
1. [Data visulization with ggplot2](http://cities.github.io/datascience/data-visulization-with-ggplot2.html)
1. [ggfortify - plot some popular R packages with ggplot2](https://github.com/sinhrks/ggfortify)
1. [geom_map](http://ggplot2.tidyverse.org/reference/geom_map.html)
1. [geom_sf - ggplot2 working with simple feature](http://ggplot2.tidyverse.org/reference/geom_map.html) and the [simple feature package](https://github.com/r-spatial/sf) for geo-spatial analysis
1. [ggmap - raster map tiles from popular online mapping services](https://github.com/dkahle/ggmap)
1. [interactive maps and charts with R](http://www.htmlwidgets.org/showcase_leaflet.html)
# Exercise
1. Plot the daily bike counts with ggplot2; Experiement with different options (bar, line, etc) and select the one works best for you;
1. Plot weekly, monthly, and annual bike counts for the three bridges;
1. Now think about and try out different ways to plot weather information along with your daily bike counts (separately or together);
1. You can combine time series stats and ggplot2/ggfortify to plot seasonal variation, trend and noise separately. See [here](http://rpubs.com/sinhrks/plot_tsstats) for an example of how to do this.
[Sample code](code/visualize_bikecounts.R)
# Resources:
1. [ggplot2 tutorial by Dr. Jenny Bryan](https://github.com/jennybc/ggplot2-tutorial/blob/master/ggplot2-tutorial-slides/ggplot2-tutorial-slides.pdf)
1. [R4DS Graphics for communication](http://r4ds.had.co.nz/graphics-for-communication.html)
1. [The ggplot2 Cheat Sheet - RStudio](https://www.rstudio.com/wp-content/uploads/2016/11/ggplot2-cheatsheet-2.1.pdf)
1. [R Graphics Cookbook](http://www.cookbook-r.com/Graphs/)