Show Sidebar Hide Sidebar

geom_tile in ggplot2

Use geom_tile to create heatmaps in R using ggplot2

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')
## [1] '4.5.6.9000'

Matrix

library(plotly)
library(reshape2)

p <- volcano %>%
  melt() %>% 
  ggplot(aes(Var1, Var2, fill = value)) + geom_tile()

p <- ggplotly(p)

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

Inspired by ggplot2 documentation

Hierarchial Clustering

library(plotly)
library(reshape2)

# Do hierarchical clustering

clust <- volcano %>% 
  dist() %>% 
  hclust()

# Get order
ord <- clust$order

# Re-arrange based on order
df <- volcano[ord,]

# Plot
p <- df %>%
  melt() %>% 
  ggplot(aes(Var1, Var2, fill = value)) + geom_tile()

p <- ggplotly(p)

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

Inspired by Stack Overflow

Continuous Colorscale

library(plotly)
library(reshape2)

p <- volcano %>%
  melt() %>% 
  ggplot(aes(Var1, Var2, fill = value)) + geom_tile()

p <- plotly_build(p)

# Modify the plotly abject to add colorscale
p$data[[1]]$colorscale = list(c(0, "#ffe6cc"), c(1, "#ff8400"))

p <- ggplotly(p)

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

Inspired by ggplot2 documentation

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.