Fun with Heatmaps and Plotly

December 29, 2015
By

[This article was first published on Modern Data » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Just because we all like numbers doesn’t mean we can’t have some fun.

Here’s to wishing to everyone a very Happy New Year !

# install.packages("jpeg") 

library(jpeg)
library(plotly)

# Download a jpeg file from imgur
URL <- "http://i.imgur.com/FWsFq6r.jpg"
file <- tempfile()
download.file(URL, file, mode = "wb")

# Read in JPEG file
j <- readJPEG(file)
j <- j[,,1]

# Create an empty matrix
img.mat <-  mat.or.vec(nrow(j), ncol(j))

# Identify elements where there is data
idx <- j > 0

# Add some glitter like effect
img.mat[idx] <-  sample(x = seq(0,1,by = 0.1), size = sum(idx), replace = T)

# Add some glitter to background
idx <-  j == 0
img.mat[idx] <-  sample(seq(0.7,0.9,0.01), size = sum(idx), replace = T)

# Invert the matrix or else it prints upside down
img.mat[nrow(img.mat):1,] <- img.mat[1:nrow(img.mat),]

# Plot !!!
x.axisSettings <- list(
  title = "Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning. -Albert Einstein",
  titlefont = list(
    family = 'Arial, sans-serif',
    size = 12,
    color = 'black'
  ),
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE,
  showgrid = FALSE,
  ticks = ""
)

y.axisSettings <- list(
  title = "",
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE,
  showgrid = FALSE,
  ticks = ""
)


bordercolor = "#ffa64d"
borderwidth = 20

nCol = ncol(img.mat)
nRow = nrow(img.mat)

plot_ly(z = img.mat, colorscale = "Hot", type = "heatmap", showscale = F, hoverinfo = "none") %>%
  layout(xaxis = x.axisSettings,
         yaxis = y.axisSettings,

         # Add a border
         shapes = list(

           # left border
           list(type = 'rect', fillcolor = bordercolor, line = list(color = bordercolor),
                x0 = 0, x1 = borderwidth,
                y0 = 0, y1 = nRow),

           # Right border
           list(type = 'rect', fillcolor = bordercolor, line = list(color = bordercolor),
                x0 = nCol - borderwidth, x1 = nCol,
                y0 = 0, y1 = nRow),

           # Top border
           list(type = 'rect', fillcolor = bordercolor, line = list(color = bordercolor),
                x0 = 0, x1 = nCol,
                y0 = nRow, y1 = nRow - borderwidth),

           # Bottom border
           list(type = 'rect', fillcolor = bordercolor, line = list(color = bordercolor),
                x0 = 0, x1 = nCol,
                y0 = 0, y1 = borderwidth)))

To leave a comment for the author, please follow the link and comment on their blog: Modern Data » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)