|
| 1 | +--- |
| 2 | +title: geom_tile | Examples | Plotly |
| 3 | +name: geom_tile |
| 4 | +permalink: ggplot2/geom_tile/ |
| 5 | +description: How to make a 2-dimensional heatmap in ggplot2 using geom_tile. |
| 6 | +layout: base |
| 7 | +thumbnail: thumbnail/geom_tile.jpg |
| 8 | +language: ggplot2 |
| 9 | +page_type: example_index |
| 10 | +has_thumbnail: true |
| 11 | +display_as: basic |
| 12 | +order: 8 |
| 13 | +output: |
| 14 | + html_document: |
| 15 | + keep_md: true |
| 16 | +--- |
| 17 | + |
| 18 | +```{r, echo = FALSE, message=FALSE} |
| 19 | +knitr::opts_chunk$set(message = FALSE, warning=FALSE) |
| 20 | +Sys.setenv("plotly_username"="RPlotBot") |
| 21 | +Sys.setenv("plotly_api_key"="q0lz6r5efr") |
| 22 | +``` |
| 23 | + |
| 24 | +### New to Plotly? |
| 25 | + |
| 26 | +Plotly's R library is free and open source!<br> |
| 27 | +[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br> |
| 28 | +You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.<br> |
| 29 | +We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! |
| 30 | + |
| 31 | +### Version Check |
| 32 | + |
| 33 | +Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br> |
| 34 | +Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version. |
| 35 | + |
| 36 | +```{r} |
| 37 | +library(plotly) |
| 38 | +packageVersion('plotly') |
| 39 | +``` |
| 40 | + |
| 41 | +### Basic geom\_tile graph |
| 42 | +This graph, compiled by [Jeff Zimmerman](https://docs.google.com/spreadsheets/d/1HI-dikWN64clxSGJu2QV8C64R9Bkzt8K-jFaeHj4X7k/edit#gid=0), shows how often hitters swing and miss at fastballs, based on their velocity and spin rate. Colour schemes are from ColorBrewer; a complete list of palettes is available [here](https://ggplot2.tidyverse.org/reference/scale_brewer.html). |
| 43 | + |
| 44 | +```{r, results='hide'} |
| 45 | +library(plotly) |
| 46 | +spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv", |
| 47 | + stringsAsFactors = FALSE) |
| 48 | +
|
| 49 | +p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + |
| 50 | + geom_tile(aes(fill = swing_miss)) + |
| 51 | + scale_fill_distiller(palette = "YlGnBu") + |
| 52 | + labs(title = "Likelihood of swinging and missing on a fastball", |
| 53 | + y = "spin rate (rpm)") |
| 54 | +ggplotly(p) |
| 55 | +
|
| 56 | +# Create a shareable link to your chart |
| 57 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 58 | +chart_link = api_create(p, filename="geom_tile/distilled-colour") |
| 59 | +chart_link |
| 60 | +``` |
| 61 | + |
| 62 | +```{r echo=FALSE} |
| 63 | +chart_link |
| 64 | +``` |
| 65 | + |
| 66 | +### Adjusting appearance |
| 67 | +The *direction* option sets which side of the colour scheme maps onto the low values and which side maps onto the high; it defaults to -1 but could be adjusted to 1. |
| 68 | + |
| 69 | +Also [adjusted the theme](https://ggplot2.tidyverse.org/reference/ggtheme.html). |
| 70 | + |
| 71 | +```{r, results='hide'} |
| 72 | +library(plotly) |
| 73 | +spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv", |
| 74 | + stringsAsFactors = FALSE) |
| 75 | +
|
| 76 | +p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + |
| 77 | + geom_tile(aes(fill = swing_miss)) + |
| 78 | + scale_fill_distiller(palette = "YlGnBu", direction = 1) + |
| 79 | + theme_light() + |
| 80 | + labs(title = "Likelihood of swinging and missing on a fastball", |
| 81 | + y = "spin rate (rpm)") |
| 82 | +ggplotly(p) |
| 83 | +
|
| 84 | +# Create a shareable link to your chart |
| 85 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 86 | +chart_link = api_create(p, filename="geom_tile/customize-theme") |
| 87 | +chart_link |
| 88 | +``` |
| 89 | + |
| 90 | +```{r echo=FALSE} |
| 91 | +chart_link |
| 92 | +``` |
| 93 | + |
| 94 | +### geom\_tile with viridis colour scheme |
| 95 | +[Viridis colour schemes](https://ggplot2.tidyverse.org/reference/scale_brewer.html) are uniform in both colour and black-and-white, as well as for those with colour-blindness. There are five colour schemes: "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"), "viridis" (or "D", the default option) and "cividis" (or "E"). |
| 96 | + |
| 97 | +```{r, results='hide'} |
| 98 | +library(plotly) |
| 99 | +spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv", |
| 100 | + stringsAsFactors = FALSE) |
| 101 | +
|
| 102 | +p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + |
| 103 | + geom_tile(aes(fill = swing_miss)) + |
| 104 | + scale_fill_viridis_c(option = "B", direction = -1) + |
| 105 | + labs(title = "Likelihood of swinging and missing on a fastball", |
| 106 | + y = "spin rate (rpm)") + |
| 107 | + theme_light() |
| 108 | +ggplotly(p) |
| 109 | +
|
| 110 | +# Create a shareable link to your chart |
| 111 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 112 | +chart_link = api_create(p, filename="geom_tile/viridis") |
| 113 | +chart_link |
| 114 | +``` |
| 115 | + |
| 116 | +```{r echo=FALSE} |
| 117 | +chart_link |
| 118 | +``` |
| 119 | + |
0 commit comments