Creating a Graph Graph in R?

This is my rfm_table data frame:

 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

I want to split the heatmap using the columns R (Recency), F (Frequency) and M (monetary), as shown below enter image description here

I used the "plotly" package and wrote the following codes, but getting this error

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

Can anyone explain what should I do?

+4
source share
2 answers

I have never used the plotly package or any of its functions. Here are alternative solutions, including the one that works best for me (from the package lattice).

Raw data (by the way, it would be very useful if you provided test data like this, and not just a printout, as you have above).

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

enter image description here

, , .

2 ( ):

require(lattice)
levelplot(mm,
          xlab = "FF",
          ylab = "RR",
          main = "XXX",
          col.regions = heat.colors)

enter image description here

3: gplots

require(gplots)
heatmap.2(mm,
          Rowv = F,
          Colv = F,
          density.info = "none",
          trace = "none",
          xlab = "FF",
          ylab = "RR",
          main = "XXX")

enter image description here

4: ggplots , .

+3

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

Source: https://habr.com/ru/post/1620216/


All Articles