gpt4 book ai didi

r - 增加 ggplot2 中的图例项

转载 作者:行者123 更新时间:2023-12-04 05:12:00 25 4
gpt4 key购买 nike

我有这个数据框:

头(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)
    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 增加图例值的数量并为每个图例分配不同的颜色?

最佳答案

我发现使用 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") .

关于r - 增加 ggplot2 中的图例项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14814846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com