实现平滑的色带

Hei*_*ler 5 r colors heatmap ggplot2

我在 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") 
Run Code Online (Sandbox Code Playgroud)

如何应用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")
Run Code Online (Sandbox Code Playgroud)

cle*_*ens 2

您可以使用该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")
Run Code Online (Sandbox Code Playgroud)

加载包:

library(tableHTML)
Run Code Online (Sandbox Code Playgroud)

重塑data.frame以反映您拥有的结构:

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   
Run Code Online (Sandbox Code Playgroud)

然后,您可以创建一个tableHTML对象并向其应用 css 来调整样式:步骤如下:

  1. 创建tableHTML具有第二个标题和标题的对象
  2. 为标题添加背景颜色和边框
  3. 更改第二个标题的背景颜色"Mon."
  4. 使用 RColorbrewer 调色板向rec和列添加颜色等级freq"Blues"
  5. 确保缺失值(即"")为白色
  6. RAG(红色、琥珀色、绿色)颜色等级应用于Mon.
  7. 对下面的标题应用不同深浅的蓝色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)
Run Code Online (Sandbox Code Playgroud)

结果如下:

输出