我在 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")
最佳答案
您可以使用 tableHTML
包裹:
这是我正在使用的数据:
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")
加载包:
library(tableHTML)
reshape
data.frame
反射(reflect)您拥有的结构:df <- data.table::dcast(df,
rec + freq ~ mon,
value.var = "rfm_score",
fill = "")
rec freq 1 2 3 4 5
1 1 1 111 112 113 114 115
2 1 2 121
然后您可以创建一个
tableHTML
对象并对其应用 css 以调整样式:步骤如下:
tableHTML
带有第二个标题和标题的对象 "Mon."
rec
添加颜色等级和 freq
列使用 RColorbrewer 调色板 "Blues"
""
)是白色的 RAG
(红色、琥珀色、绿色)颜色等级为 Mon.
栏目 Mon.
\
df %>%
tableHTML(rownames = FALSE,
second_headers = list(c(2, 5),
c("", "Mon.")),
caption = "<br>RFM Segmentation <br> Count of Cust in each Segment",
widths = c(rep(80, 2), rep(100, 5))) %>%
add_css_caption(css = list(c("background-color", "border"),
c("#F9E9DC", "1px solid black"))) %>%
add_css_second_header(css = list("background-color",
"lightgray"),
second_headers = 2) %>%
add_css_conditional_column(conditional = "colour_rank",
colour_rank_css = make_css_colour_rank_theme(list(rec = df$rec),
RColorBrewer::brewer.pal(5, "Blues")),
columns = 1) %>%
add_css_conditional_column(conditional = "colour_rank",
colour_rank_css = make_css_colour_rank_theme(list(freq = df$freq),
RColorBrewer::brewer.pal(5, "Blues")),
columns = 2) %>%
add_css_conditional_column(conditional = "==",
value = "",
css = list(c("background-color", "color"),
c("white", "white")),
columns = 3:7) %>%
add_css_conditional_column(conditional = "colour_rank",
colour_rank_theme = "RAG",
columns = 3:7,
decreasing = TRUE) %>%
add_css_header(css = list("background-color",
"#EFF3FF"),
header = 3) %>%
add_css_header(css = list("background-color",
"#BDD7E7"),
header = 4) %>%
add_css_header(css = list("background-color",
"#6BAED6"),
header = 5) %>%
add_css_header(css = list("background-color",
"#3182BD"),
header = 6) %>%
add_css_header(css = list("background-color",
"#08519C"),
header = 7)
结果如下所示:
关于r - 实现平滑的色带,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51818989/