|
| 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 | + |
0 commit comments