user4275811
user4275811

Reputation:

Change row and column labels heatmap plot_ly R

So I am building a heatmap using plot_ly and would like the row and column labels to be the row and column names from my data matrix (a). I have tried the following code but it still won't change the labels:

vals <- unique(scales::rescale(c(a)))
o <- order(vals, decreasing = FALSE)
cols <- scales::col_numeric("Blues", domain = NULL)(vals)
colz <- setNames(data.frame(vals[o], cols[o]), NULL)
plot_ly(z = a, colorscale=colz,type = "heatmap") %>%
 layout(xaxis = list(colnames(a)), yaxis = list(rownames(a)))

The layout part doesn't seem to update the x and y axis?

I attached an image of the view I'm currently getting.

graph of heatmap with wrong column and row labels The row names I would like are;

"BILL_AMT1"                "BILL_AMT2"                "BILL_AMT5"               
"ID"                       "LIMIT_BAL"                "PAY_0"                   
"PAY_2"                    "PAY_3"                    "PAY_4"                   
"PAY_5"                    "PAY_6"                    "PAY_AMT2"                
"PAY_AMT4"                 "PAY_AMT6"                 "SEX"                     
"EDUCATION"                "MARRIAGE"                 "AGE"                     
"BILL_AMT3"                "BILL_AMT4"                "BILL_AMT6"               
"PAY_AMT1"                 "PAY_AMT3"                 "PAY_AMT5"                
"avg_bill_amt"             "avg_pay_amt"              "Bill_pay_diff1"          
"Bill_pay_diff2"           "Bill_pay_diff3"           "Bill_pay_diff4"          
"Bill_pay_diff5"           "Bill_pay_diff_prop_limit"

And the column names are;

"UnalteredTree" "CreatedFields" "CreatedSMOTE50.50" "CreatedSMOTE60.40" "CreatedSMOTE70.30"

dput(head(a)):

structure(c(0, 0, 0, 18.1134012050694, 3.29561403508772, 0, 16.8737510298348, 111.988517072994, 238.103575444644, 93.8902946420699, 0, 0, 17.2492245114282, 126.35593664339, 836.870398039609, 476.448288146105, 85.084473204874, 134.652264684817, 50.7197832427989, 70.9693694071751, 593.541951819008, 406.736392972063, 98.0032746143464, 104.028924545005, 37.1605151856135, 93.5877347492431, 409.663438436698, 137.4329664773, 56.9190963166505, 61.7152642490207), .Dim = c(6L, 5L), .Dimnames = list(c("BILL_AMT1", "BILL_AMT2", "BILL_AMT5", "ID", "LIMIT_BAL", "PAY_0"), c("UnalteredTree", "CreatedFields", "CreatedSMOTE50.50", "CreatedSMOTE60.40", "CreatedSMOTE70.30" )))

Upvotes: 0

Views: 1851

Answers (1)

Steven Beaupr&#233;
Steven Beaupr&#233;

Reputation: 21621

Simply provide the x and y arguments with the appropriate reference:

library(plotly)

set.seed(1)
m <- matrix(rnorm(9), nrow = 3, ncol = 3)
dimnames(m) <- list(rownames(m, FALSE, "r"), 
                    colnames(m, FALSE, "c"))

plot_ly(
  x = colnames(m), y = rownames(m),
  z = m, type = "heatmap"
)

Upvotes: 1

Related Questions