2017-03-02 136 views
2

你好,我製作plotly的熱圖。的代碼是:如何將文字寫入r中的劇情熱圖中?

 plot_ly(z = ~df$MonetaryClass, 
     x = ~df$RecencyClass, 
     y = ~df$FrequencyClass, 
     type = "heatmap") %>% 
    colorbar(title = 'Monetary Class', 
      limits = c(1,5), 
      tickmode = 'array', 
      tickvals = c(1,2,3,4,5)) %>% 
    layout(title = "RFM Analyse", 
     xaxis = list(title = 'Recency Class', 
         tickmode = 'array', 
         tickvals = c(1, 2,3,4,5), 
         ticktext = c(1, 2, 3, 4, 5) 
     ), 
     yaxis = list(title = 'Frequency Class', 
         tickmode = 'array', 
         tickvals = c(1, 2,3,4,5), 
         ticktext = c(1, 2, 3, 4, 5) 
     ) 
) 

這裏是我的數據幀的100的樣品,並且dput()的信息:

 structure(list(RecencyClass = c(3, 4, 4, 3, 4, 4, 1, 4, 3, 4, 
4, 2, 1, 3, 5, 5, 2, 1, 4, 1, 3, 1, 4, 2, 2, 1, 4, 5, 2, 4, 5, 
1, 1, 1, 3, 3, 3, 4, 1, 3, 1, 3, 1, 3, 2, 5, 2, 1, 4, 1, 5, 2, 
4, 4, 2, 2, 5, 3, 1, 3, 3, 5, 2, 2, 4, 4, 2, 2, 5, 2, 2, 2, 5, 
3, 2, 1, 1, 2, 5, 2, 1, 1, 2, 1, 4, 2, 3, 2, 2, 1, 1, 2, 2, 4, 
5, 3, 5, 5, 5, 5), FrequencyClass = c(4, 5, 1, 3, 5, 5, 3, 1, 
5, 3, 1, 3, 4, 1, 3, 2, 1, 2, 5, 1, 1, 4, 3, 2, 3, 1, 4, 4, 2, 
4, 4, 5, 1, 1, 3, 4, 4, 3, 2, 4, 1, 2, 1, 2, 1, 4, 1, 1, 2, 1, 
3, 1, 3, 2, 2, 4, 2, 5, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 5, 2, 1, 
4, 5, 1, 1, 3, 1, 1, 5, 4, 4, 2, 5, 1, 4, 1, 5, 2, 1, 1, 1, 3, 
5, 5, 4, 4, 4, 3, 4, 2), MonetaryClass = c(5, 5, 3, 4, 5, 3, 
1, 2, 3, 3, 1, 2, 3, 1, 4, 3, 2, 3, 5, 2, 1, 4, 1, 3, 2, 3, 5, 
5, 3, 3, 4, 5, 1, 1, 3, 3, 5, 2, 2, 4, 4, 3, 1, 2, 1, 3, 1, 1, 
2, 4, 3, 1, 2, 4, 2, 5, 1, 5, 1, 1, 1, 3, 3, 2, 1, 5, 2, 4, 5, 
2, 1, 4, 2, 1, 3, 5, 2, 1, 5, 5, 5, 2, 4, 1, 5, 1, 5, 1, 4, 1, 
2, 1, 1, 5, 3, 5, 2, 2, 4, 3)), .Names = c("RecencyClass", "FrequencyClass", 
"MonetaryClass"), row.names = c(24908L, 34164L, 27970L, 23648L, 
35944L, 35581L, 7825L, 27857L, 26043L, 31093L, 27026L, 13607L, 
8689L, 18196L, 39464L, 37304L, 9820L, 6867L, 35361L, 4767L, 18345L, 
8840L, 29486L, 11414L, 13718L, 60L, 31892L, 41103L, 13347L, 33299L, 
41563L, 8950L, 16L, 568L, 21384L, 25256L, 24005L, 30861L, 6025L, 
25098L, 5800L, 20559L, 2362L, 20797L, 11256L, 40490L, 9163L, 
3282L, 29174L, 5965L, 38331L, 11068L, 31018L, 28535L, 11793L, 
16171L, 37278L, 25937L, 5976L, 19566L, 18754L, 37055L, 11249L, 
13414L, 27966L, 31049L, 11150L, 15701L, 42887L, 11334L, 10880L, 
16532L, 44559L, 18162L, 9545L, 7810L, 3715L, 10128L, 42163L, 
16488L, 8926L, 6416L, 17789L, 4352L, 32966L, 10045L, 26614L, 
12601L, 10906L, 3122L, 4696L, 15936L, 18048L, 34864L, 41047L, 
24615L, 41413L, 39479L, 40317L, 37389L), class = "data.frame") 

它看起來像這樣:

enter image description here

作爲你可以看到我有25個盒子可以洞察我的矩陣。是否可以在每個盒子裏寫一個自選文本?例如,對於框「1」頻率和「1」新近程度,左下角的藍黑色就像「我喜歡啤酒」。或在「5」和「5」上右上角的黃色部分,如「我喜歡CSGO」。 我試過text,但它不適合我。

+0

我試圖讓一個5x5矩陣所要求的句子,但我不知道如何將這些新矩陣與我的情節相匹配? – Marre

回答

1

您可以通過add_annotations添加文本。

  • (經由xref = 'x'yref = 'y'在相對於x和y軸下面的例子中)指定x和y座標
  • 經由text
  • 添加文本與刪除默認箭頭showarrow = FALSE

enter image description here

anno_x <- c(1, 5) 
anno_y <- c(1, 5) 
anno_text <- c('I like beer', 'I like CSGO') 

p <- plot_ly(
    x = c(1:5), 
    y = c(1:5), 
    z = matrix(rnorm(9), nrow = 5, ncol = 5), 
    type='heatmap') %>% add_annotations(x = anno_x, y = anno_y, text = anno_text, xref = 'x', yref = 'y', showarrow = FALSE, font=list(color='black')) 

p 
+1

非常感謝你們倆。 – Marre

1

首先定義5x5文本矩陣,然後將其添加到數據幀,然後使用add_annotations

txt <- matrix(LETTERS[1:25], nrow=5) # define your 5x5 text matrix 
df$txt <- txt[as.matrix(df[,1:2])] 
library(plotly) 
plot_ly(z = ~df$MonetaryClass, 
     x = ~df$RecencyClass, 
     y = ~df$FrequencyClass, 
     type = "heatmap") %>% 
    colorbar(title = 'Monetary Class', 
      limits = c(1,5), 
      tickmode = 'array', 
      tickvals = c(1,2,3,4,5)) %>% 
    layout(title = "RFM Analyse", 
     xaxis = list(title = 'Recency Class', 
         tickmode = 'array', 
         tickvals = c(1, 2,3,4,5), 
         ticktext = c(1, 2, 3, 4, 5) 
     ), 
     yaxis = list(title = 'Frequency Class', 
         tickmode = 'array', 
         tickvals = c(1, 2,3,4,5), 
         ticktext = c(1, 2, 3, 4, 5) 
     ) 
) %>% 
    add_annotations(x = df$RecencyClass, 
        y = df$FrequencyClass, 
        text = df$txt, 
        showarrow = FALSE, 
        ax = 20, 
        ay = -20) 

enter image description here