簡體   English   中英

在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(新近度),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")

任何人都可以解釋我該怎么辦?

我從來沒有使用包plotly或者其任何職能。 以下是替代解決方案,包括最適合我的解決方案(來自封裝點陣)。

原始數據(順便說一下,如果你提供了這樣的測試數據而不僅僅是像上面那樣的打印輸出,那將非常有用......)

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(我最喜歡的):使用格子

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我不打算編寫代碼,但你可以在這里看到它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM