2

我有这个数据框:

头(x)

      Date Company   Region Units
1 1/1/2012 Gateway  America     0
2 1/1/2012 Gateway   Europe     0
3 1/1/2012 Gateway  America     0
4 1/1/2012 Gateway Americas     0
5 1/1/2012 Gateway   Europe     0
6 1/1/2012 Gateway  Pacific     0

x 输入(x)

    structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("1/1/2012", 
"1/12/2012", "1/2/2012"), class = "factor"), Company = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("Gateway", "HP", "IBM"), class = "factor"), 
    Region = structure(c(1L, 3L, 1L, 2L, 3L, 4L, 2L, 1L, 3L, 
    1L, 2L, 3L, 4L, 2L, 1L, 3L, 1L, 2L, 3L, 4L, 2L, 1L, 3L, 1L, 
    2L, 3L, 4L, 2L, 1L, 3L, 1L, 2L, 3L, 4L, 2L, 1L, 3L, 1L, 2L, 
    3L, 4L, 2L, 1L, 3L, 1L, 2L, 3L, 4L, 2L, 1L, 3L, 1L, 2L, 3L, 
    4L, 2L, 1L, 3L, 1L, 2L, 3L, 4L, 2L, 1L, 3L, 1L, 2L, 3L, 4L, 
    2L, 1L, 3L, 1L, 2L, 3L, 4L, 2L, 1L, 3L, 1L, 2L, 3L, 4L, 2L
    ), .Label = c("America", "Americas", "Europe", "Pacific"), class = "factor"), 
    Units = c(1L, 3L, 1L, 6L, 20L, 2L, 2L, 10L, 2L, 1L, 2L, 4L, 
    6L, 30L, 2L, 15L, 10L, 3L, 4L, 7L, 9L, 12L, 34L, 50L, 3L, 
    2L, 4L, 3L, 1L, 3L, 3L, 1L, 4L, 0L, 1L, 0L, 0L, 1L, 0L, 4L, 
    0L, 0L, 0L, 0L, 5L, 0L, 8L, 0L, 0L, 0L, 0L, 0L, 9L, 0L, 56L, 
    10L, 0L, 0L, 5L, 7L, 0L, 0L, 8L, 0L, 2L, 0L, 4L, 0L, 5L, 
    7L, 0L, 0L, 8L, 10L, 0L, 6L, 0L, 4L, 4L, 0L, 2L, 0L, 5L, 
    0L)), .Names = c("Date", "Company", "Region", "Units"), class = "data.frame", row.names = c(NA, 
-84L))

我想创建一个热图:

ggplot(x, aes(Date, Company, fill=Units)) + geom_tile(aes(fill=Units)) + facet_grid(~Region) + scale_fill_gradient(low="white", high="red")

此命令有效,但我需要能够使用不同的颜色而不是白色和红色,并增加图例上的比例。现在,默认是,有 5 个图例。我喜欢增加 10。O 将是白色的,而其他人应该与白色明显不同,以便用户会注意到它。

如何使用 ggplot 增加图例值的数量并为每个图例分配不同的颜色?

4

1 回答 1

3

我发现使用此博客中所做quantiles的绘图非常有用。这有助于生成倾斜的颜色集(如博客中所示)。假设数据与您的数据相似(大量的 0),然后通过计算适当的分位数,我们可以创建一个带有适当标签的倾斜颜色图,该颜色图在视觉上非常出色且信息丰富。我已经修改了已针对此问题链接的博客图中的代码,并添加了更多解释。博客文章的想法和实施必须得到所有赞誉。heatmaps

在进入代码之前,我们必须对quantiles您的数据进行一些分析,以查看要使用哪些分位数。通过做:

quantile(x$Units, seq(0, 1, length.out = 25)

#      0% 4.166667% 8.333333%     12.5% 16.66667% 20.83333%       25% 29.16667% 33.33333% 
# 0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000 
#   37.5% 41.66667% 45.83333%       50% 54.16667% 58.33333%     62.5% 66.66667% 70.83333% 
# 1.00000   1.00000   2.00000   2.00000   3.00000   3.00000   4.00000   4.00000   5.00000 
#     75% 79.16667% 83.33333%     87.5% 91.66667% 95.83333%      100% 
# 6.00000   7.00000   8.00000   9.62500  10.16667  25.41667  56.00000 

您会看到0%分位数对应于您的数据Units=0。直到33%33.33%准确地说)。所以,也许我们选择38%作为下一个分位数。然后说 , ,60%最后以 . 结束。现在,我们有足够多的您想要的级别,并且它们的级别对您的数据有意义。75%90%100%

我们需要zoo包来完成这个。现在让我们构建数据:

require(zoo) # for rollapply
# the quantiles we just decided to categorise the data into classes.
qtiles    <- quantile(x$Units, probs = c(0, 38, 60, 75, 90, 100)/100)
# a color palette
c_pal     <- colorRampPalette(c("#3794bf", "#FFFFFF", 
                         "#df8640"))(length(qtiles)-1)
# since we are using quantile classes for fill levels, 
# we'll have to generate the appropriate labels
labels    <- rollapply(round(qtiles, 2), width = 2, by = 1, 
                      FUN = function(i) paste(i, collapse = " : "))
# added the quantile interval in which the data falls, 
# which will be used for fill
x$q.units <- findInterval(x$Units, qtiles, all.inside = TRUE)

# Now plot
library(ggplot2)
p <- ggplot(data = x, aes(x = Date, y = Company, fill = factor(q.units)))
p <- p + geom_tile(color = "black")
p <- p + scale_fill_manual(values = c_pal, name = "", labels = labels)
p <- p + facet_grid( ~ Region)
p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 1))
p

你得到这个: ggplot2_heatmap_skewed

希望这可以帮助。

编辑:您还可以访问colorbrewer2.org以获得漂亮的调色板并自己设置颜色。例如:

# try out these colors:
c_pal     <- c("#EDF8FB", "#B3CDE3", "#8C96C6", "#8856A7", "#810F7C")
c_pal     <- c("#FFFFB2", "#FECC5C", "#FD8D3C", "#F03B20", "#BD0026")

另外,尝试alpha在代码中设置geom_tile(color = "black", alpha = 0.5")

于 2013-02-11T16:28:44.587 回答