【发布时间】:2019-01-19 23:40:52
【问题描述】:
我在 Excel 中有热图,我试图在 R 中重新创建它。它基本上是用于 RFM 分割的数据,在 excel 中颜色范围很好,但我很难在 R 中获得如此漂亮的平滑颜色渐变,并尝试了多种方法但无法实现相同的平滑渐变。
我的 Excel 热图如下所示:
我在 R 中的热图如下所示:
我的 R 代码是:
cols <- brewer.pal(9, 'RdYlGn')
ggplot(xxx)+
geom_tile(aes(x= mon, y = reorder(freq, desc(freq)), fill = n)) +
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) +
theme(legend.position = "none")
我如何应用ColorRampPalette 来实现与 Excel 中相同的平滑颜色渐变或任何其他可以让我获得更平滑渐变的方法? R中的渐变不是很好。
我无法在此处发布我的数据集,因为它有 30,000 条记录。我使用 dput(head(df)) 将我的数据集的头部转储如下:
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")
【问题讨论】:
-
请注意,对于您的用例,DT package 可能更合适。