Skip to content

Commit 267a84e

Browse files
authored
Merge pull request plotly#1454 from plotly/geom_raster
geom_raster
2 parents 6d2736f + af6fedc commit 267a84e

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
title: geom_raster | Examples | Plotly
3+
name: geom_raster
4+
permalink: ggplot2/geom_raster/
5+
description: How to make a 2-dimensional heatmap in ggplot2 using geom_raster.
6+
layout: base
7+
thumbnail: thumbnail/geom_raster.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: basic
12+
order: 7
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 2d Heatmap
42+
geom\_raster creates a coloured heatmap, with two variables acting as the x- and y-coordinates and a third variable mapping onto a colour. (It is coded similarly to geom\_tile and is generated more quickly.) This uses the volcano dataset that comes pre-loaded with R.
43+
44+
```{r, results='hide'}
45+
library(reshape2)
46+
library(plotly)
47+
48+
df <- melt(volcano)
49+
50+
p <- ggplot(df, aes(Var1, Var2)) +
51+
geom_raster(aes(fill=value)) +
52+
labs(x="West to East",
53+
y="North to South",
54+
title = "Elevation map of Maunga Whau")
55+
ggplotly(p)
56+
57+
# Create a shareable link to your chart
58+
# Set up API credentials: https://plot.ly/r/getting-started
59+
chart_link = api_create(p, filename="geom_raster/basic-chart")
60+
chart_link
61+
```
62+
63+
```{r echo=FALSE}
64+
chart_link
65+
```
66+
67+
### Customized 2d Heatmap
68+
This uses the Spectral palette from [ColorBrewer](https://ggplot2.tidyverse.org/reference/scale_brewer.html); a full list of palettes is here.
69+
70+
```{r, results='hide'}
71+
library(reshape2)
72+
library(plotly)
73+
74+
df <- melt(volcano)
75+
76+
p <- ggplot(df, aes(Var1, Var2)) +
77+
geom_raster(aes(fill=value)) +
78+
scale_fill_distiller(palette = "Spectral", direction = -1) +
79+
labs(x="West to East",
80+
y="North to South",
81+
title = "Elevation map of Maunga Whau",
82+
fill = "Elevation") +
83+
theme(text = element_text(family = 'Fira Sans'),
84+
plot.title = element_text(hjust = 0.5))
85+
ggplotly(p)
86+
87+
# Create a shareable link to your chart
88+
# Set up API credentials: https://plot.ly/r/getting-started
89+
chart_link = api_create(p, filename="geom_raster/colour-scales")
90+
chart_link
91+
```
92+
93+
```{r echo=FALSE}
94+
chart_link
95+
```
96+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: geom_raster | Examples | Plotly
3+
name: geom_raster
4+
permalink: ggplot2/geom_raster/
5+
description: How to make a 2-dimensional heatmap in ggplot2 using geom_raster.
6+
layout: base
7+
thumbnail: thumbnail/geom_raster.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: basic
12+
order: 7
13+
output:
14+
html_document:
15+
keep_md: true
16+
---
17+
18+
19+
20+
### New to Plotly?
21+
22+
Plotly's R library is free and open source!<br>
23+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
24+
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>
25+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
26+
27+
### Version Check
28+
29+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
30+
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.
31+
32+
33+
```r
34+
library(plotly)
35+
packageVersion('plotly')
36+
```
37+
38+
```
39+
## [1] '4.8.0.9000'
40+
```
41+
42+
### Basic 2d Heatmap
43+
geom\_raster creates a coloured heatmap, with two variables acting as the x- and y-coordinates and a third variable mapping onto a colour. (It is coded similarly to geom\_tile and is generated more quickly.) This uses the volcano dataset that comes pre-loaded with R.
44+
45+
46+
```r
47+
library(reshape2)
48+
library(plotly)
49+
50+
df <- melt(volcano)
51+
52+
p <- ggplot(df, aes(Var1, Var2)) +
53+
geom_raster(aes(fill=value)) +
54+
labs(x="West to East",
55+
y="North to South",
56+
title = "Elevation map of Maunga Whau")
57+
ggplotly(p)
58+
59+
# Create a shareable link to your chart
60+
# Set up API credentials: https://plot.ly/r/getting-started
61+
chart_link = api_create(p, filename="geom_raster/basic-chart")
62+
chart_link
63+
```
64+
65+
<iframe src="https://plot.ly/~RPlotBot/5850.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
66+
67+
### Customized 2d Heatmap
68+
This uses the Spectral palette from [ColorBrewer](https://ggplot2.tidyverse.org/reference/scale_brewer.html); a full list of palettes is here.
69+
70+
71+
```r
72+
library(reshape2)
73+
library(plotly)
74+
75+
df <- melt(volcano)
76+
77+
p <- ggplot(df, aes(Var1, Var2)) +
78+
geom_raster(aes(fill=value)) +
79+
scale_fill_distiller(palette = "Spectral", direction = -1) +
80+
labs(x="West to East",
81+
y="North to South",
82+
title = "Elevation map of Maunga Whau",
83+
fill = "Elevation") +
84+
theme(text = element_text(family = 'Fira Sans'),
85+
plot.title = element_text(hjust = 0.5))
86+
ggplotly(p)
87+
88+
# Create a shareable link to your chart
89+
# Set up API credentials: https://plot.ly/r/getting-started
90+
chart_link = api_create(p, filename="geom_raster/colour-scales")
91+
chart_link
92+
```
93+
94+
<iframe src="https://plot.ly/~RPlotBot/5852.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
95+

0 commit comments

Comments
 (0)