如何在R中将文本写入绘图热图? [英] How to write a text into plotly heat map in r?
问题描述
你好,我用plotly
制作了一个热图.代码是:
Hello I make a heat map with plotly
. The code is:
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()
的信息:
Here is a sample of 100 of my data frame, and the information of 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")
它看起来像这样:
如您所见,我有25个框可用于分析矩阵.是否可以在每个框中写一个自选文本?例如,在框"1"的频率和"1"的新近度上,左下角的蓝黑色为我喜欢啤酒"之类的东西.或者在右上角的"5"和"5"上显示黄色,类似于我喜欢CSGO".
我用text
尝试过,但是对我不起作用.
As you can see I have 25 boxes insight my matrix. Is it possible to write a self chosen text in every box ? For example on box "1" Frequency and "1" Recency the blue-black one on the bottom left corner something like " I like beer". Or on "5" and "5" the yellow one in the upper right corner,something like "I like CSGO".
I tried it with text
but it don't work for me.
推荐答案
首先定义5x5
文本矩阵,然后将其添加到数据框中,然后使用add_annotations
:
First define the 5x5
text matrix, then add it to the data frame and then use 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)
这篇关于如何在R中将文本写入绘图热图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!