简体   繁体   English

你如何在 ggplot2 geom_bar 中订购填充颜色

[英]How do you order the fill-colours within ggplot2 geom_bar

I am calling the ggplot function我正在调用 ggplot 函数

ggplot(data,aes(x,y,fill=category)+geom_bar(stat="identity")

The result is a barplot with bars filled by various colours corresponding to category.结果是条形图,条形图由与类别对应的各种颜色填充。 However the ordering of the colours is not consistent from bar to bar.但是,条与条之间的颜色顺序不一致。 Say there is pink, green and blue.假设有粉红色、绿色和蓝色。 Some bars go pink,green,blue from bottom to top and some go green,pink,blue.有些条从下到上变成粉红色、绿色、蓝色,有些则变成绿色、粉红色、蓝色。 I don't see any obvious pattern.我没有看到任何明显的模式。

How are these orderings chosen?这些订单是如何选择的? How can I change it?我怎样才能改变它? At the very least, how can I make ggplot choose a consistent ordering?至少,我怎样才能让 ggplot 选择一致的顺序?

The class of (x,y and category) are (integer,numeric and factor) respectively. (x,y 和 category) 的类分别是 (integer,numeric 和 factor)。 If I make category an ordered factor, it does not change this behavior.如果我将类别作为有序因素,它不会改变这种行为。

Anyone know how to fix this?有人知道怎么修这个东西吗?

Reproducible example:可重现的例子:

dput(data)

structure(list(mon = c(9L, 10L, 11L, 10L, 8L, 7L, 7L, 11L, 9L, 
10L, 12L, 11L, 7L, 12L, 8L, 12L, 9L, 7L, 9L, 10L, 10L, 8L, 12L, 
7L, 11L, 10L, 8L, 7L, 11L, 12L, 12L, 9L, 9L, 7L, 7L, 12L, 12L, 
9L, 9L, 8L), gclass = structure(c(9L, 1L, 8L, 6L, 4L, 4L, 3L, 
6L, 2L, 4L, 1L, 1L, 5L, 7L, 1L, 6L, 8L, 6L, 4L, 7L, 8L, 7L, 9L, 
8L, 3L, 5L, 9L, 2L, 7L, 3L, 5L, 5L, 7L, 7L, 9L, 2L, 4L, 1L, 3L, 
8L), .Label = c("Down-Down", "Down-Stable", "Down-Up", "Stable-Down", 
"Stable-Stable", "Stable-Up", "Up-Down", "Up-Stable", "Up-Up"
), class = c("ordered", "factor")), NG = c(222614.67, 9998.17, 
351162.2, 37357.95, 4140.48, 1878.57, 553.86, 40012.25, 766.52, 
15733.36, 90676.2, 45000.29, 0, 375699.84, 2424.21, 93094.21, 
120547.69, 291.33, 1536.38, 167352.21, 160347.01, 26851.47, 725689.06, 
4500.55, 10644.54, 75132.98, 42676.41, 267.65, 392277.64, 33854.26, 
384754.67, 7195.93, 88974.2, 20665.79, 7185.69, 45059.64, 60576.96, 
3564.53, 1262.39, 9394.15)), .Names = c("mon", "gclass", "NG"
), row.names = c(NA, -40L), class = "data.frame") 

ggplot(data,aes(mon,NG,fill=gclass))+geom_bar(stat="identity")

Starting in ggplot2_2.0.0, the order aesthetic is no longer available.从 ggplot2_2.0.0 开始, order美学不再可用。 To get a graph with the stacks ordered by fill color, you can simply order the dataset by the grouping variable you want to order by.要获得按填充颜色排序的堆栈图,您只需按要排序的分组变量对数据集进行排序。

I often use arrange from dplyr for this.为此,我经常使用dplyr 的arrange Here I'm ordering the dataset by the fill factor within the ggplot call rather than creating an ordered dataset but either will work fine.在这里,我通过ggplot调用中的fill因子对数据集进行排序,而不是创建一个有序的数据集,但两者都可以正常工作。

library(dplyr)

ggplot(arrange(data, gclass), aes(mon, NG, fill = gclass)) +
    geom_bar(stat = "identity")

This is easily done in base R, of course, using the classic order with the extract brackets:这在基础 R 中很容易完成,当然,使用带有提取括号的经典order

ggplot(data[order(data$gclass), ], aes(mon, NG, fill = gclass)) +
    geom_bar(stat = "identity")

With the resulting plot in both cases now in the desired order:现在两种情况下的结果图都按照所需的顺序: 在此处输入图片说明

ggplot2_2.2.0 update ggplot2_2.2.0 更新

In ggplot_2.2.0, fill order is based on the order of the factor levels.在 ggplot_2.2.0 中,填充顺序基于因子水平的顺序。 The default order will plot the first level at the top of the stack instead of the bottom.默认顺序将在堆栈顶部而不是底部绘制第一级。

If you want the first level at the bottom of the stack you can use reverse = TRUE in position_stack .如果您想要堆栈底部的第一级,您可以在position_stack使用reverse = TRUE Note you can also use geom_col as shortcut for geom_bar(stat = "identity") .请注意,您还可以使用geom_col作为geom_bar(stat = "identity")快捷方式。

ggplot(data, aes(mon, NG, fill = gclass)) +
    geom_col(position = position_stack(reverse = TRUE))

You need to specify the order aesthetic as well.您还需要指定order美学。

ggplot(data,aes(mon,NG,fill=gclass,order=gclass))+
    geom_bar(stat="identity")

在此处输入图片说明

This may or may not be a bug .这可能是也可能不是错误

To order, you must use the levels parameter and inform the order.要订购,您必须使用levels参数并通知订单。 Like this:像这样:

data$gclass
(data$gclass2 <- factor(data$gclass,levels=sample(levels(data$gclass)))) # Look the difference in the factors order
ggplot(data,aes(mon,NG,fill=gclass2))+geom_bar(stat="identity")

You can change the colour using the scale_fill_ functions.您可以使用scale_fill_函数更改颜色。 For example:例如:

ggplot(dd,aes(mon,NG,fill=gclass)) + 
  geom_bar(stat="identity") + 
  scale_fill_brewer(palette="blues")

To get consistent ordering in the bars , then you need to order the data frame:要在bars获得一致的排序,则需要对数据框进行排序:

dd = dd[with(dd, order(gclass, -NG)), ]

In order to change the ordering of legend, alter the gclass factor.为了更改图例的顺序,请更改gclass因子。 So something like:所以像:

dd$gclass= factor(dd$gclass,levels=sort(levels(dd$gclass), TRUE))

在此处输入图片说明

Building on @aosmith 's answer, another way to order the bars, that I found slightly more intuitive is:以@aosmith 的回答为基础,另一种排序条形的方法,我发现稍微更直观的是:

ggplot(data, aes(x=mon, y=reorder(NG,gclass), fill = gclass)) +
    geom_bar(stat = "identity")

The beauty of the reorder function from the base stats package is that you can apply it in the reorder(based_on_dimension, y, function) wherein y is ordered based_on_dimension with a function like sum, mean, etc. base stats 包中重新排序函数的美妙之处在于,您可以将它应用到reorder(based_on_dimension, y, function)其中 y 使用 sum、mean 等函数对 based_on_dimension 进行排序。

Since this exchange shows up first for "factor fill order", I will add one more solution, what I believe to be a bit more straight forward, and doesn't require altering your underlying data.由于此交换首先出现在“因子填充订单”中,因此我将添加另一个解决方案,我认为它更直接一些,并且不需要更改您的基础数据。

ggplot(data,aes(x,y,fill=factor(category, levels = c("Down-Down", "Down-Stable", "Down-Up", "Stable-Down", "Stable-Stable", "Stable-Down", "Up-Down", "Up-Stable", "Up-Up"))) + 
geom_col(position = position_stack(reverse = FALSE))

Or as I prefer, I first create a variable vector to simplify coding later and make it more easily editable:或者按照我的喜好,我首先创建一个变量向量以简化以后的编码并使其更易于编辑:

v_factor_levels <- c("Down-Down", "Down-Stable", "Down-Up", "Stable-Down", "Stable-Stable", "Stable-Down", "Up-Down", "Up-Stable", "Up-Up")

ggplot(data,aes(x,y,fill=factor(category, levels = v_factor_levels)) + 
geom_col(position = position_stack(reverse = FALSE))

You don't need the reverse position element within geom_col(), I keep these as a reminder in case I want to reverse, but you could further simplify by eliminating that.你不需要 geom_col() 中的反向位置元素,我保留这些作为提醒,以防我想反向,但你可以通过消除它来进一步简化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM