简体   繁体   中英

Avoid converting numbers to dates in plotly

I have a matrix that I want to create a heatmap for in plotly. the row names are assays and the colnames are CASRN and they are in this format "131-55-5"

my matrix looks like this the data matrix for the heatmap

for some reason plotly thinks these are dates and converts them to something like March 2000 and gives me an empty plot.

before i convert my data frame to matrix i checked and all columns are factors. is there any way I can make sure my numbers wont turn into dates when i plot my matrix?

this is the code i am using for my heatmap

plot_ly(x=colnames(dm_new2), y=rownames(dm_new2), z = dm_new2, type = "heatmap") %>%
  layout(margin = list(l=120))

Using some random data to mimic your dataset. Simply put your matrix in a dataframe. Try this:

set.seed(42)

library(plotly)
library(dplyr)
library(tidyr)

dm_new2 <- matrix(runif(12), nrow = 4, dimnames = list(LETTERS[1:4], c("131-55-5", "113-48-4", "1582-09-8")))

# Put matrix in a dataframe
dm_new2 <- as.data.frame(dm_new2) %>% 
  # rownames to column
  mutate(x = row.names(.)) %>%
  # convert to long format
  pivot_longer(-x, names_to = "y", values_to = "value")

dm_new2 %>% 
  plot_ly(x = ~x, y = ~y, z = ~value, type = "heatmap") %>%
  layout(margin = list(l=120))

在此处输入图像描述

Created on 2020-04-08 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM