这是我的rfm_table数据帧:
UserID R F M Rsegment Fsegment Msegment Total_Score
1 10609 984 3 318.78 2 4 4 244
2 10922 648 1 184.26 5 2 3 523
3 11300 1022 1 91.02 2 2 2 222
4 11400 864 5 851.73 3 5 5 355
5 11487 797 1 147.22 3 2 3 323
6 11762 1042 1 32.31 1 2 1 121
我想使用R(新近度),F(频率)和M(货币)列来制作热图,就像下面下面的图像一样
我用过" plotly"打包并编写此代码,但收到此错误
# minimal value for n is 3, returning requested palette with 3 different levels
plot_ly(z = rfm_table[,c(5,6,4)]
x = c("1","2","3","4","5"), y =c("1","2","3","4","5"),
type = "heatmap")
任何人都可以解释我该怎么做?
答案 0 :(得分:3)
我从未使用过包 情节 或其任何功能。以下是替代解决方案,包括最适合我的解决方案(来自封装晶格)。
原始数据(顺便说一句,如果你提供了这样的测试数据而不仅仅是像上面那样的打印输出,那将非常有用......)
d1 <- data.frame(UserID = factor(rep(paste("id", 1:10), 50, sep = "")),
RR = factor(rep(1:5, 100)),
FF = factor(rep(1:5, each = 100)),
MM = round(1000*rnorm(500), 2))
table(d1$R, d1$F)
# Means of groups, long format
d.l <- aggregate(x = d1$M,
by = list(RR = d1$RR,
FF = d1$FF),
FUN = mean)
# Same data, wide format
d.w <- reshape(data = d.l,
idvar = "RR",
timevar = "FF",
direction = "wide")
mm <- as.matrix(d.w[, -1])
colnames(mm) <- 1:5
使用颜色键绘制热图 选项1:使用默认的 热图 功能
heatmap(mm,
Rowv = NA,
Colv = NA,
xlab = "FF",
ylab = "RR",
main = "XXX")
您可以使用图例添加键,但需要一些工作。
选项2(我最喜欢的):使用 lattice 包
require(lattice)
levelplot(mm,
xlab = "FF",
ylab = "RR",
main = "XXX",
col.regions = heat.colors)
选项3:使用包 gplots
require(gplots)
heatmap.2(mm,
Rowv = F,
Colv = F,
density.info = "none",
trace = "none",
xlab = "FF",
ylab = "RR",
main = "XXX")
选项4:使用 ggplots 我不打算编写代码,但你可以看到它here。
答案 1 :(得分:2)
z
来电中的plot_ly
应该是一个矩阵,每个行 - 列对都对应x
和y
值。
require(plotly)
rfm_table <- read.table(text="
UserID R F M Rsegment Fsegment Msegment Total_Score
10609 984 3 318.78 2 4 4 244
10922 648 1 184.26 5 2 3 523
11300 1022 1 91.02 2 2 2 222
11400 864 5 851.73 3 5 5 355
11487 797 1 147.22 3 2 3 323
11762 1042 1 32.31 1 2 1 121",
header = TRUE)
df <- expand.grid(lapply(rfm_table[, 5:6], function(x) sort(unique(x))))
df <- merge(df, rfm_table[, 4:6], by = c("Rsegment", "Fsegment"), all.x = TRUE)
df <- df[order(df$Rsegment, df$Fsegment), ]
z <- matrix(df$M, nrow = length(unique(df$Rsegment)))
plot_ly(z = z, x = sort(unique(df[[1]])), y = sort(unique(df[[2]])),
type = "heatmap")