|
| 1 | +--- |
| 2 | +title: "Flex Dashboard" |
| 3 | +output: |
| 4 | + flexdashboard::flex_dashboard: |
| 5 | + orientation: rows |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +```{r setup, include=FALSE} |
| 10 | +library(plotly) |
| 11 | +library(maps) |
| 12 | +knitr::opts_chunk$set(message = FALSE) |
| 13 | +``` |
| 14 | + |
| 15 | +Rows {data-height:600} |
| 16 | +------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +### Chart A |
| 19 | + |
| 20 | +```{r} |
| 21 | +# This example modifies code from Hadley Wickham -- https://gist.github.com/hadley/233134 |
| 22 | +# It also uses data from Nathan Yau's flowingdata site -- http://flowingdata.com/ |
| 23 | +unemp <- read.csv("http://datasets.flowingdata.com/unemployment09.csv") |
| 24 | +names(unemp) <- c("id", "state_fips", "county_fips", "name", "year", |
| 25 | + "?", "?", "?", "rate") |
| 26 | +unemp$county <- tolower(gsub(" County, [A-Z]{2}", "", unemp$name)) |
| 27 | +unemp$state <- gsub("^.*([A-Z]{2}).*$", "\\1", unemp$name) |
| 28 | +county_df <- map_data("county") |
| 29 | +names(county_df) <- c("long", "lat", "group", "order", "state_name", "county") |
| 30 | +county_df$state <- state.abb[match(county_df$state_name, tolower(state.name))] |
| 31 | +county_df$state_name <- NULL |
| 32 | +state_df <- map_data("state") |
| 33 | +choropleth <- merge(county_df, unemp, by = c("state", "county")) |
| 34 | +choropleth <- choropleth[order(choropleth$order), ] |
| 35 | +choropleth$rate_d <- cut(choropleth$rate, breaks = c(seq(0, 10, by = 2), 35)) |
| 36 | +
|
| 37 | +# provide a custom tooltip to plotly with the county name and actual rate |
| 38 | +choropleth$text <- with(choropleth, paste0("County: ", name, "<br>Rate: ", rate)) |
| 39 | +p <- ggplot(choropleth, aes(long, lat, group = group)) + |
| 40 | + geom_polygon(aes(fill = rate_d, text = text), |
| 41 | + colour = alpha("white", 1/2), size = 0.2) + |
| 42 | + geom_polygon(data = state_df, colour = "white", fill = NA) + |
| 43 | + scale_fill_brewer(palette = "PuRd") + theme_void() |
| 44 | +# just show the text aesthetic in the tooltip |
| 45 | +ggplotly(p, tooltip = "text") |
| 46 | +``` |
| 47 | + |
| 48 | +### Chart B |
| 49 | + |
| 50 | +```{r} |
| 51 | +crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests) |
| 52 | +crimesm <- tidyr::gather(crimes, variable, value, -state) |
| 53 | +states_map <- map_data("state") |
| 54 | +g <- ggplot(crimesm, aes(map_id = state)) + |
| 55 | + geom_map(aes(fill = value), map = states_map) + |
| 56 | + expand_limits(x = states_map$long, y = states_map$lat) + |
| 57 | + facet_wrap( ~ variable) |
| 58 | +ggplotly(g) |
| 59 | +``` |
| 60 | + |
| 61 | +Rows {data-height:400} |
| 62 | +------------------------------------------------------------------------------ |
| 63 | + |
| 64 | + |
| 65 | +### Chart C |
| 66 | + |
| 67 | +```{r} |
| 68 | +m <- ggplot(faithful, aes(x = eruptions, y = waiting)) + |
| 69 | + stat_density_2d() + xlim(0.5, 6) + ylim(40, 110) |
| 70 | +ggplotly(m) |
| 71 | +``` |
| 72 | + |
| 73 | +### Chart D |
| 74 | + |
| 75 | +```{r} |
| 76 | +m <- ggplot(faithful, aes(x = eruptions, y = waiting)) + |
| 77 | + stat_density_2d(aes(fill = ..level..), geom = "polygon") + |
| 78 | + xlim(0.5, 6) + ylim(40, 110) |
| 79 | +ggplotly(m) |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +### Chart E |
| 84 | + |
| 85 | +```{r} |
| 86 | +m <- ggplot(faithful, aes(x = eruptions, y = waiting)) + geom_hex() |
| 87 | +ggplotly(m) |
| 88 | +``` |
0 commit comments