如何在r中用plotly修改轴名?

时间:2017-03-02 11:09:03

标签: r heatmap plotly

我的数据框如下所示:

    plot_ly(z = ~df$MonetaryClass, x = ~df$RecencyClass   , y = ~df$FrequencyClass, 
        type = "heatmap") %>% 
  colorbar(limits = c(1,5)) %>%
  layout(title = "RFM Analyse")

结果如下:

enter image description here

但我想修改这个情节。起初我希望隐藏z范围上的不均匀数字。然后我希望在xasis和yasis" 1" a" min"并且在" 5" a" max"是写的。至少改变了xasis和yasis的名称。 结果应如下所示:

enter image description here

对于我的问题的第三部分,我试过这个,但它不起作用:

   plot_ly(z = ~df$MonetaryClass, x = ~df$RecencyClass   , y = ~df$FrequencyClass, 
        type = "heatmap") %>% 
  colorbar(limits = c(1,5)) %>%
  layout(title = "RFM Analyse",
          scene = list(
           x  = list(title = "Recency"), 
           y = list(title = "Frequency"), 
           z = list(title = "Monetary")))

对于前两个问题,我没有解决方法。

它是我的100个数据框的样本:

 structure(list(RecencyClass = structure(c(3L, 1L, 5L, 2L, 4L, 
2L, 1L, 4L, 4L, 2L, 4L, 4L, 4L, 1L, 4L, 3L, 5L, 5L, 5L, 2L, 2L, 
3L, 1L, 5L, 5L, 3L, 5L, 4L, 2L, 2L, 2L, 4L, 1L, 5L, 3L, 3L, 4L, 
5L, 5L, 4L, 2L, 2L, 5L, 1L, 3L, 2L, 2L, 4L, 2L, 4L, 1L, 5L, 2L, 
3L, 1L, 4L, 4L, 1L, 1L, 1L, 2L, 3L, 2L, 2L, 1L, 5L, 5L, 3L, 4L, 
3L, 2L, 4L, 5L, 1L, 2L, 2L, 3L, 1L, 5L, 3L, 1L, 3L, 1L, 1L, 2L, 
5L, 2L, 1L, 4L, 4L, 2L, 3L, 2L, 4L, 3L, 1L, 4L, 5L, 5L, 1L), .Label = c("1", 
"2", "3", "4", "5"), class = "factor"), FrequencyClass = structure(c(3L, 
1L, 3L, 3L, 5L, 4L, 2L, 5L, 2L, 3L, 4L, 5L, 2L, 1L, 4L, 1L, 5L, 
5L, 1L, 3L, 1L, 5L, 1L, 5L, 5L, 2L, 5L, 2L, 2L, 1L, 4L, 2L, 2L, 
5L, 4L, 4L, 5L, 3L, 5L, 5L, 1L, 1L, 5L, 1L, 5L, 1L, 3L, 4L, 3L, 
4L, 4L, 5L, 2L, 1L, 1L, 4L, 4L, 1L, 2L, 2L, 3L, 3L, 3L, 2L, 2L, 
4L, 3L, 1L, 1L, 3L, 3L, 5L, 4L, 1L, 1L, 1L, 3L, 3L, 5L, 3L, 2L, 
2L, 1L, 1L, 3L, 5L, 2L, 2L, 5L, 5L, 3L, 2L, 1L, 4L, 3L, 1L, 5L, 
5L, 4L, 1L), .Label = c("1", "2", "3", "4", "5"), class = "factor"), 
    MonetaryClass = c(3, 1, 4, 3, 5, 5, 2, 5, 3, 3, 5, 5, 4, 
    2, 1, 1, 5, 4, 2, 2, 1, 5, 2, 5, 4, 3, 5, 3, 1, 2, 3, 2, 
    2, 3, 4, 4, 4, 3, 5, 4, 1, 3, 5, 1, 1, 4, 3, 4, 2, 4, 4, 
    5, 2, 3, 1, 3, 3, 2, 2, 4, 3, 4, 3, 3, 2, 2, 3, 3, 2, 5, 
    2, 5, 3, 1, 3, 1, 2, 2, 5, 2, 2, 4, 1, 1, 3, 5, 3, 2, 4, 
    5, 3, 4, 1, 2, 3, 1, 5, 5, 4, 1)), .Names = c("RecencyClass", 
"FrequencyClass", "MonetaryClass"), row.names = c(21905L, 3384L, 
37826L, 15776L, 34715L, 16079L, 6656L, 33920L, 29071L, 14429L, 
32553L, 35034L, 28436L, 1327L, 33075L, 19364L, 44108L, 41976L, 
36220L, 14489L, 10100L, 26309L, 397L, 44059L, 43615L, 21125L, 
43154L, 28203L, 13461L, 9720L, 17104L, 29200L, 6704L, 42277L, 
25339L, 25013L, 35174L, 38928L, 42665L, 34659L, 10300L, 10132L, 
44827L, 3405L, 25523L, 11189L, 15357L, 33415L, 14023L, 33246L, 
8733L, 42276L, 13381L, 18459L, 3390L, 32937L, 31867L, 5118L, 
7311L, 6536L, 13771L, 23489L, 14375L, 11987L, 6584L, 41619L, 
38854L, 18188L, 27675L, 23360L, 14335L, 33970L, 41305L, 5429L, 
9262L, 10655L, 22696L, 8186L, 41769L, 22566L, 6008L, 21076L, 
3123L, 5359L, 14213L, 43481L, 12594L, 7364L, 34879L, 35302L, 
14190L, 20604L, 10801L, 32840L, 23387L, 29L, 34714L, 44049L, 
39741L, 5467L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

Plotly为您提供了大量自定义图表的选项,并了解它们here 要具体执行您的要求,请使用colorbarxaxisyaxis的选项:

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,3,5)) %>%
    layout(title = "RFM Analyse",
           xaxis = list(title = 'Recency Class',
                        tickmode = 'array',
                        tickvals = c(1, 2,3,4,5),
                        ticktext = c('1<br>Min', 2, 3, 4, '5<br>Max')
                        ),
           yaxis = list(title = 'Frequency Class',
                        tickmode = 'array',
                        tickvals = c(1, 2,3,4,5),
                        ticktext = c('1<br>Min', 2, 3, 4, '5<br>Max')
                        )
    )