我是R的新手,一直在疯狂地挣扎,无法在R中使用geom_tile将3路表可视化为热图。我已经看过使用Mosaics了,但这不是我想要的,并且我找到了数百种两向表的示例,但是似乎没有三向表的示例。
我希望输出看起来像这样:
我的数据集如下:(它是30,000条记录的小快照):
xxx <- structure(list(rfm_score = c(111, 112, 113, 114, 115, 121), n = c(2624L,
160L, 270L, 23L, 5L, 650L), rec = structure(c(1L, 1L, 1L, 1L,
1L, 1L), .Label = c("1", "2", "3", "4", "5"), class = "factor"),
freq = structure(c(1L, 1L, 1L, 1L, 1L, 2L), .Label = c("1",
"2", "3", "4", "5"), class = "factor"), mon = structure(c(1L,
2L, 3L, 4L, 5L, 1L), .Label = c("1", "2", "3", "4", "5"), class = "factor")), row.names = c(NA,
6L), class = "data.frame")
本质上,这是对客户购物行为(新近度,频率和货币)的RFM分析。输出的热图(我想要的)应该是每个RFM细分中的客户数。在我提供的热图中,您会看到左侧有两个变量(例如R =新近度(五分位数范围1至5)和F =频率(五分位范围1至5),并且在热图的顶部是M =货币变量(五分之一至5),例如,细分RFM = 555拥有2511个客户。
我已经尝试了以下代码及其变体,但会遇到错误
library(ggplot2)
library(RColorBrewer)
library(dplyr)
cols <- rev(brewer.pal(11, 'RdYlBu'))
ols <- brewer.pal(9, 'RdYlGn')
ggplot(xxx)+ geom_tile(aes(x= mon, y = reorder(freq, desc(freq)), fill = n)) +
theme_change +
facet_grid(rec~.) +
# geom_text(aes(label=n)) +
# scale_fill_gradient2(midpoint = (max(xxx$n)/2), low = "red", mid = "yellow", high = "darkgreen") +
# scale_fill_gradient(low = "red", high = "blue") + scale_fill_gradientn(colours = cols) +
# scale_fill_brewer() +
labs(x = "monetary", y= "frequency") +
scale_x_discrete(expand = c(0,0)) + scale_y_discrete(expand = c(0,0)) +
coord_fixed(ratio= 0.5)
我不知道如何在R中创建此热图。任何人都可以帮助我。.
亲切的问候
Heinrich