r - 实现平滑的色带

标签 r ggplot2 colors heatmap

我在 Excel 中有热图,我试图在 R 中重新创建它。它的基本数据用于 RFM 分割,在 excel 中,颜色范围很棒,但我正在努力在 R 中获得如此漂亮的平滑颜色渐变,并尝试了多种方法但无法实现相同的平滑渐变。

我的 Excel 热图如下所示:

enter image description here

我在 R 中的热图如下所示:

enter image description here

我的 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)
    

    结果如下所示:

    output

    关于r - 实现平滑的色带,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51818989/

    相关文章:

    r - 在 rsample 中结合滚动原点预测重采样和 Group V-Fold 交叉验证

    R Shiny downloadHandler 返回应用程序 html 而不是绘图或数据

    r - 将分组的 geom_vline 添加到多个方面

    r - 单个变量前面的波浪号(〜)是什么意思(facet_wrap)?

    C++ Pixel Grabber 错误的 RGB 值

    android - WebView 如何继承当前 Android 主题的颜色?

    r - 定义中未定义的插槽类?

    r - grepl期间为“。”在R?

    r - xtable 输出中的列对齐

    r - 将颜色分成均匀的部分