实现平滑的色阶

时间:2018-08-13 09:16:23

标签: 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")

2 个答案:

答案 0 :(得分: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")

加载程序包:

library(tableHTML)

重塑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   

然后可以创建一个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)

结果如下:

output

答案 1 :(得分:0)

主要问题是gradientn()将产生线性色标。查看在Excel中完成的示例,值1显示为红色,200显示为黄色,2000显示为绿色。我不知道Excel如何缩放比例(我想是百分位数吗?),但是它绝对不是线性的。

如果线性值很重要,并且转换此数据不合适,则Excel中的色标会产生误导。看起来值的分布范围很广,但实际上,您的大多数值都是相似的,因此很低,如ggplot2色标所示。

如果对数值进行对数转换是合理或适当的,则请执行此操作。这样可以使您获得与Excel相似的缩放比例,但是对于查看器来说更清晰。

这里是一个例子:

library(ggplot2)
library(RColorBrewer)

set.seed(123) ; rn = rnorm(25, mean = 5, sd = 2)

df = data.frame(monetary = rep(seq(5),5),
                frequency = sort(rep(seq(5),5)),
                val = 10^rn)

pal = brewer.pal(9, "RdYlGn")

# mostly red, a few green (very high) values
ggplot(df, aes(monetary, frequency)) +
  geom_tile(aes(fill = val)) +
  scale_fill_gradientn(colors = pal)

# log transforming evens out scale
ggplot(df, aes(monetary, frequency)) +
  geom_tile(aes(fill = log10(val))) +
  scale_fill_gradientn(colors = pal)