问题描述
我有一个data.frame
,如下所示:
set.seed(100)
df <- data.frame(year = rep(2011:2014, 3),
class = rep(c("high", "middle", "low"), each = 4),
age_group = rep(1:3, each = 4),
value = sample(1:2, 12, rep = TRUE))
并且我希望通过facet
-ing(通过变量age_group
)产生三个与以下代码产生的图相似的图:
and I am looking to produce, by facet
-ing (by the variable age_group
) three plots which look similar to those produced by the following code:
library(ggplot2)
blue <- c("#bdc9e1", "#74a9cf", "#0570b0")
ggplot(df) + geom_bar(aes(x = year, y = value,
fill = factor(class, levels = c("high", "middle", "low"))),
stat = "identity") +
scale_fill_manual(values = c(blue)) +
guides(fill = FALSE)
但是,每个方面都有不同的配色方案,其中所有颜色都是由我自己指定的.
however, where each facet has a different colour scheme where all the colours are specified by myself.
我似乎想要这里发生的事情的更具体版本:
I appear to want a more specific version of what is going on here: ggplot2: Change color for each facet in bar chart
因此,根据我提供的数据,我希望获得三个多面图,并按age_group
进行划分,其中每个图中的填充度均按class
的级别指定,并且所有颜色(共9种) )由我自己手动指定.
So, using the data I have provided, I am looking to get three facet-ed plots, split by age_group
where the fill is given in each plot by the level of class
, and all colours (9 total) would be specified manually by myself.
为澄清起见,以下代码的确提供了我想最后介绍的方面:
For clarification, the facet that I would like to end up with is indeed provided by the following code:
ggplot(df) + geom_bar(aes(x = year, y = value,
fill = factor(class, levels = c("high", "middle", "low"))),
stat = "identity") +
scale_fill_manual(values = c(blue)) +
guides(fill = FALSE) +
facet_wrap(~ age_group)
通过class
变量增加了对颜色子集的控制级别.
with the added level of control of colour subset by the class
variable.
推荐答案
我不确定为什么要这么做,所以很难知道我是否要这么做.提出了您的实际用例的地址.
I'm not entirely sure why you want to do this, so it is a little hard to know whether or not what I came up with addresses your actual use case.
首先,我生成了一个不同的数据集,该数据集实际上在每个age_group中都有每个类:
First, I generated a different data set that actually has each class in each age_group:
set.seed(100)
df <- data.frame(year = rep(2011:2014, 3),
class = rep(c("high", "middle", "low"), each = 12),
age_group = rep(1:3, each = 4),
value = sample(1:2, 36, rep = TRUE))
如果要在每个age_group
中寻找类似的明暗梯度,则可以直接使用alpha
完成此操作,而不必担心添加额外的数据列:
If you are looking for a similar dark-to-light gradient within each age_group
you can accomplish this directly using alpha
and not worry about adding extra data columns:
ggplot(df) +
geom_bar(aes(x = year, y = value,
fill = factor(age_group)
, alpha = class ),
stat = "identity") +
facet_wrap(~age_group) +
scale_alpha_discrete(range = c(0.4,1)) +
scale_fill_brewer(palette = "Set1"
, name = "age_group")
在这里,我将alpha的范围设置为可以给出合理可见的颜色,并且只是从RColorBrewer中选择了一个默认调色板来显示该想法.这给出了:
Here, I set the range of the alpha to give reasonably visible colors, and just chose a default palette from RColorBrewer to show the idea. This gives:
它也提供了一个相对可用的图例作为起点,尽管您可以进一步对其进行修改(这是我对另一个问题的类似图例回答:)
It also gives a relatively usable legend as a starting point, though you could modify it further (here is a similar legend answer I gave to a different question: https://stackoverflow.com/a/39046977/2966222 )
或者,如果您真的要自己指定颜色,则可以在数据中添加一列,然后根据该颜色作为基础颜色:
Alternatively, if you really, really want to specify the colors yourself, you can add a column to the data, and base the color off of that:
df$forColor <-
factor(paste0(df$class, " (", df$age_group , ")")
, levels = paste0(rep(c("high", "middle", "low"), times = 3)
, " ("
, rep(1:3, each = 3)
, ")") )
然后,将其用作填充.请注意,这里我使用RColorBrewer
brewer.pal
来选择颜色.我发现第一种颜色太浅,无法显示出类似这样的条形,因此我将其排除在外.
Then, use that as your fill. Note here that I am using the RColorBrewer
brewer.pal
to pick colors. I find that the first color is too light to show up for bars like this, so I excluded it.
ggplot(df) +
geom_bar(aes(x = year, y = value,
fill = forColor),
stat = "identity") +
scale_fill_manual(values = c(brewer.pal(4, "Blues")[-1]
, brewer.pal(4, "Reds")[-1]
, brewer.pal(4, "Purples")[-1]
)
, name = "Class (age_group)") +
facet_wrap(~age_group)
给予:
图例非常繁忙,但可以修改为类似于我链接到的其他答案.这样一来,您就可以设置所需的9种颜色(或更多,用于不同的用例).
The legend is rather busy, but could be modified similar to the other answer I linked to. This would then allow you to set whatever 9 (or more, for different use cases) colors you wanted.
这篇关于在构面之间更改配色方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!