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