在 R 中创建热图?

Creating Heatmap graph in R?

这是我的 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(Recency)、F(Frequency) 和 M(Monetary) 列创建热图,就像下面这张图片一样

我使用了 "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")

谁能解释一下我该怎么办?

您在 plot_ly 调用中的 z 应该是一个矩阵,每个行列对对应于 xy 值。

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")

我从未使用过包 plotly 或其任何功能。这里有一些替代解决方案,包括我认为最有效的解决方案(来自 package lattice)。

原始数据(顺便说一下,如果您提供了这样的测试数据,而不是像上面那样只是打印输出,那将非常有帮助...)

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 函数

heatmap(mm,
        Rowv = NA,
        Colv = NA,
        xlab = "FF",
        ylab = "RR",
        main = "XXX")

您可以使用图例添加键,但这需要一些工作。

选项 2(我最喜欢的):使用 lattice package

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