forked from Vancouver-Datajam/project_7
-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.Rmd
90 lines (75 loc) · 2.87 KB
/
example.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
title: "ggplotly: various examples"
author: "Carson Sievert"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
---
```{r setup, include=FALSE}
# https://beta.rstudioconnect.com/jjallaire/htmlwidgets-ggplotly-examples/htmlwidgets-ggplotly-examples.html
library(plotly)
library(maps)
knitr::opts_chunk$set(message = FALSE)
```
Row {data-height=600}
------------------------------------------------------------------------------
### Unemployment
```{r}
# This example modifies code from Hadley Wickham (https://gist.github.com/hadley/233134)
# It also uses data from Nathan Yau's flowingdata site (http://flowingdata.com/)
unemp <- read.csv("http://datasets.flowingdata.com/unemployment09.csv")
names(unemp) <- c("id", "state_fips", "county_fips", "name", "year",
"?", "?", "?", "rate")
unemp$county <- tolower(gsub(" County, [A-Z]{2}", "", unemp$name))
unemp$state <- gsub("^.*([A-Z]{2}).*$", "\\1", unemp$name)
county_df <- map_data("county")
names(county_df) <- c("long", "lat", "group", "order", "state_name", "county")
county_df$state <- state.abb[match(county_df$state_name, tolower(state.name))]
county_df$state_name <- NULL
state_df <- map_data("state")
choropleth <- merge(county_df, unemp, by = c("state", "county"))
choropleth <- choropleth[order(choropleth$order), ]
choropleth$rate_d <- cut(choropleth$rate, breaks = c(seq(0, 10, by = 2), 35))
# provide a custom tooltip to plotly with the county name and actual rate
choropleth$text <- with(choropleth, paste0("County: ", name, "Rate: ", rate))
p <- ggplot(choropleth, aes(long, lat, group = group)) +
geom_polygon(aes(fill = rate_d, text = text),
colour = alpha("white", 1/2), size = 0.2) +
geom_polygon(data = state_df, colour = "white", fill = NA) +
scale_fill_brewer(palette = "PuRd") + theme_void()
# just show the text aesthetic in the tooltip
ggplotly(p, tooltip = "text")
```
### Crimes
```{r}
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
crimesm <- tidyr::gather(crimes, variable, value, -state)
states_map <- map_data("state")
g <- ggplot(crimesm, aes(map_id = state)) +
geom_map(aes(fill = value), map = states_map) +
expand_limits(x = states_map$long, y = states_map$lat) +
facet_wrap( ~ variable) + theme_void()
ggplotly(g)
```
Row {data-height=400}
------------------------------------------------------------------------------
### Faithful Eruptions
```{r}
m <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
stat_density_2d() + xlim(0.5, 6) + ylim(40, 110)
ggplotly(m)
```
### Faithful Eruptions (polygon)
```{r}
m <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
stat_density_2d(aes(fill = ..level..), geom = "polygon") +
xlim(0.5, 6) + ylim(40, 110)
ggplotly(m)
```
### Faithful Eruptions (hex)
```{r, error=TRUE}
m <- ggplot(faithful, aes(x = eruptions, y = waiting)) + geom_hex()
ggplotly(m)
```