增加ggplot2中的图例项

时间:2013-02-11 15:06:31

标签: r ggplot2

我有数据框:

头(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 dput(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")

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

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

1 个答案:

答案 0 :(得分:3)

我发现使用quantiles绘制heatmaps as done here in this blog非常有用。这有助于生成倾斜的颜色集(如博客中所示)。假设数据与你的数据相当(0的数量非常大),那么通过计算适当的分位数,我们可以创建一个偏斜的颜色图,它具有适当的标签,在视觉上非常好并且信息丰富。我已经修改了已经链接到此问题的博客图中的代码,并添加了更多解释。博客文章必须得到理念和实施的全部功劳。

在进入代码之前,我们必须使用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")