Thanks to visit codestin.com
Credit goes to github.com

Skip to content

new version of geom_tile #1453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 0 additions & 128 deletions _posts/ggplot2/2016-11-29-geom_tile.Rmd

This file was deleted.

126 changes: 0 additions & 126 deletions _posts/ggplot2/2016-11-29-geom_tile.md

This file was deleted.

119 changes: 119 additions & 0 deletions _posts/ggplot2/2019-08-12-geom_tile.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---
title: geom_tile | Examples | Plotly
name: geom_tile
permalink: ggplot2/geom_tile/
description: How to make a 2-dimensional heatmap in ggplot2 using geom_tile.
layout: base
thumbnail: thumbnail/geom_tile.jpg
language: ggplot2
page_type: example_index
has_thumbnail: true
display_as: basic
order: 8
output:
html_document:
keep_md: true
---

```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
Sys.setenv("plotly_username"="RPlotBot")
Sys.setenv("plotly_api_key"="q0lz6r5efr")
```

### New to Plotly?

Plotly's R library is free and open source!<br>
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
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>
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!

### Version Check

Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
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.

```{r}
library(plotly)
packageVersion('plotly')
```

### Basic geom\_tile graph
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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIce Dataset!


```{r, results='hide'}
library(plotly)
spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv",
stringsAsFactors = FALSE)

p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) +
geom_tile(aes(fill = swing_miss)) +
scale_fill_distiller(palette = "YlGnBu") +
labs(title = "Likelihood of swinging and missing on a fastball",
y = "spin rate (rpm)")
ggplotly(p)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="geom_tile/distilled-colour")
chart_link
```

```{r echo=FALSE}
chart_link
```

### Adjusting appearance
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.

Also [adjusted the theme](https://ggplot2.tidyverse.org/reference/ggtheme.html).

```{r, results='hide'}
library(plotly)
spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv",
stringsAsFactors = FALSE)

p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) +
geom_tile(aes(fill = swing_miss)) +
scale_fill_distiller(palette = "YlGnBu", direction = 1) +
theme_light() +
labs(title = "Likelihood of swinging and missing on a fastball",
y = "spin rate (rpm)")
ggplotly(p)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="geom_tile/customize-theme")
chart_link
```

```{r echo=FALSE}
chart_link
```

### geom\_tile with viridis colour scheme
[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").

```{r, results='hide'}
library(plotly)
spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv",
stringsAsFactors = FALSE)

p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) +
geom_tile(aes(fill = swing_miss)) +
scale_fill_viridis_c(option = "B", direction = -1) +
labs(title = "Likelihood of swinging and missing on a fastball",
y = "spin rate (rpm)") +
theme_light()
ggplotly(p)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="geom_tile/viridis")
chart_link
```

```{r echo=FALSE}
chart_link
```

Loading