plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.56k stars 626 forks source link

plotly heatmap doesn't take a dataframe #329

Open ziyadsaeed opened 8 years ago

ziyadsaeed commented 8 years ago

The figure reference documentation says

plot_ly(df, type="heatmap"[, ...])

the df in the code above indicates a data frame The code below doesn't work

url <- "http://datasets.flowingdata.com/ppg2008.csv"
nba_players <- read.csv(url, row.names = 1)

plot_ly(nba_players, type = "heatmap")

A solution is to use a data.matrix

plot_ly(z = data.matrix(nba_players), 
           x = colnames(nba_players), 
           y = row.names(nba_players), 
           type = "heatmap")
ziyadsaeed commented 8 years ago

what then is the syntax to use a dataframe without converting it to a matrix Also Is there a way to tell it that the integer column x is the x label and integer column y is the y label and z values integer column z of the dataframe

cpsievert commented 8 years ago

I don't think it is currently possible to make a heatmap from a data frame. This will go on the todo list, but it isn't a super high priority right now.

PS. you can adds axis titles like so:

plot_ly(z = volcano, type = "heatmap") %>% layout(xaxis = list(title = "x"), yaxis = list(title = "y"))
ziyadsaeed commented 8 years ago

for the time being this part of the heatmap documentation should be changed. Atleast it should mention that dataframe support is in the works

y (dataframe column, list, vector) Sets the y coordinates. x (dataframe column, list, vector) Sets the x coordinates. z (dataframe column, list, vector) Sets the z data.

simonegabbriellini commented 7 years ago

@ziyadsaeed are you sure about your data.matrix solution? if I run your code, I get:

 plot_ly(z = data.matrix(nba_players), 
+         x = colnames(nba_players), 
+         y = row.names(nba_players), 
+         type = "heatmap")
Error: Variables must be length 1 or 50. 
Problem variables: 'x'

Looks like the axes should have same length, otherwise it complains... I don't understand why though...