Skip to content

Commit d5d0200

Browse files
authored
Merge pull request #1448 from plotly/geom_contour
geom_contour
2 parents 381a9ec + 276434b commit d5d0200

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
title: geom_contour | Examples | Plotly
3+
name: geom_contour
4+
permalink: ggplot2/geom_contour/
5+
description: How to make a contour in ggplot2 using geom_contour.
6+
layout: base
7+
thumbnail: thumbnail/geom_contour.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: basic
12+
order: 3
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\_contour plot
42+
geom\_contour produces a similar output to geom\_density\_2d, except it uses a third variable for the values rather than frequency. The volcano dataset comes pre-loaded on R.
43+
44+
```{r, results='hide'}
45+
library(plotly)
46+
library(reshape2)
47+
df <- melt(volcano)
48+
49+
p <- ggplot(df, aes(Var1, Var2, z= value)) +
50+
geom_contour() +
51+
scale_fill_distiller(palette = "Spectral", direction = -1)
52+
p <- ggplotly(p)
53+
54+
# Create a shareable link to your chart
55+
# Set up API credentials: https://plot.ly/r/getting-started
56+
chart_link = api_create(p, filename="geom_contour/basic-plot")
57+
chart_link
58+
```
59+
60+
```{r echo=FALSE}
61+
chart_link
62+
```
63+
64+
### Coloured Plot
65+
[See here](https://ggplot2.tidyverse.org/reference/scale_brewer.html) for a list of colour palettes that come with the brewer (discrete) and distiller (continuous) packages.
66+
67+
```{r, results='hide'}
68+
library(plotly)
69+
library(reshape2)
70+
df <- melt(volcano)
71+
72+
p <- ggplot(df, aes(Var1, Var2, z= value, colour=stat(level))) +
73+
geom_contour() +
74+
scale_colour_distiller(palette = "YlGn", direction = 1)
75+
ggplotly(p)
76+
77+
# Create a shareable link to your chart
78+
# Set up API credentials: https://plot.ly/r/getting-started
79+
chart_link = api_create(p, filename="geom_contour/coloured-plot")
80+
chart_link
81+
```
82+
83+
```{r echo=FALSE}
84+
chart_link
85+
```
86+
87+
### Filled Plot
88+
It's possible to colour in each of the layers, by changing geom\_contour to stat\_contour as below. As the edges of the graph indicate, filled contour plots only work when each layer is an enclosed shape rather than an open line; a geom more suited to this functionality would be geom\_tile or geom\_raster.
89+
90+
```{r, results='hide'}
91+
library(plotly)
92+
library(reshape2)
93+
df <- melt(volcano)
94+
95+
p <- ggplot(df, aes(Var1, Var2, z= value)) +
96+
stat_contour(geom="polygon",aes(fill=stat(level))) +
97+
scale_fill_distiller(palette = "Spectral", direction = -1)
98+
ggplotly(p)
99+
100+
# Create a shareable link to your chart
101+
# Set up API credentials: https://plot.ly/r/getting-started
102+
chart_link = api_create(p, filename="geom_contour/filled-plot")
103+
chart_link
104+
```
105+
106+
```{r echo=FALSE}
107+
chart_link
108+
```
109+
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: geom_contour | Examples | Plotly
3+
name: geom_contour
4+
permalink: ggplot2/geom_contour/
5+
description: How to make a contour in ggplot2 using geom_contour.
6+
layout: base
7+
thumbnail: thumbnail/geom_contour.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: basic
12+
order: 3
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 geom\_contour plot
43+
geom\_contour produces a similar output to geom\_density\_2d, except it uses a third variable for the values rather than frequency. The volcano dataset comes pre-loaded on R.
44+
45+
46+
```r
47+
library(plotly)
48+
library(reshape2)
49+
df <- melt(volcano)
50+
51+
p <- ggplot(df, aes(Var1, Var2, z= value)) +
52+
geom_contour() +
53+
scale_fill_distiller(palette = "Spectral", direction = -1)
54+
p <- 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_contour/basic-plot")
59+
chart_link
60+
```
61+
62+
<iframe src="https://plot.ly/~RPlotBot/5832.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
63+
64+
### Coloured Plot
65+
[See here](https://ggplot2.tidyverse.org/reference/scale_brewer.html) for a list of colour palettes that come with the brewer (discrete) and distiller (continuous) packages.
66+
67+
68+
```r
69+
library(plotly)
70+
library(reshape2)
71+
df <- melt(volcano)
72+
73+
p <- ggplot(df, aes(Var1, Var2, z= value, colour=stat(level))) +
74+
geom_contour() +
75+
scale_colour_distiller(palette = "YlGn", direction = 1)
76+
ggplotly(p)
77+
78+
# Create a shareable link to your chart
79+
# Set up API credentials: https://plot.ly/r/getting-started
80+
chart_link = api_create(p, filename="geom_contour/coloured-plot")
81+
chart_link
82+
```
83+
84+
<iframe src="https://plot.ly/~RPlotBot/5834.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
85+
86+
### Filled Plot
87+
It's possible to colour in each of the layers, by changing geom\_contour to stat\_contour as below. As the edges of the graph indicate, filled contour plots only work when each layer is an enclosed shape rather than an open line; a geom more suited to this functionality would be geom\_tile or geom\_raster.
88+
89+
90+
```r
91+
library(plotly)
92+
library(reshape2)
93+
df <- melt(volcano)
94+
95+
p <- ggplot(df, aes(Var1, Var2, z= value)) +
96+
stat_contour(geom="polygon",aes(fill=stat(level))) +
97+
scale_fill_distiller(palette = "Spectral", direction = -1)
98+
ggplotly(p)
99+
100+
# Create a shareable link to your chart
101+
# Set up API credentials: https://plot.ly/r/getting-started
102+
chart_link = api_create(p, filename="geom_contour/filled-plot")
103+
chart_link
104+
```
105+
106+
<iframe src="https://plot.ly/~RPlotBot/5830.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
107+

0 commit comments

Comments
 (0)