您如何在ggplot2风水中订购填充颜色?

时间:2020-12-16 15:02:37

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. If I make category an ordered factor, it does not change this behavior.

类(x、y和类别)分别为(整数、数字和因子)。如果我把category作为一个有序的因子,它不会改变这种行为。

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")

4 个解决方案

#1


21  

You need to specify the order aesthetic as well.

您还需要指定订单审美观。

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

您如何在ggplot2风水中订购填充颜色?

This may or may not be a bug.

这可能是一个错误,也可能不是。

#2


39  

Starting in ggplot2_2.0.0, the order aesthetic is no longer available. 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.

从ggplot2_2.0.0开始,订单的美观不再可用。要获得由填充色排序的堆栈的图形,您可以简单地通过您想要排序的分组变量来订购数据集。

I often use arrange from dplyr for this. 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.

我经常用dplyr来做这个。这里,我在ggplot调用中按填充因子排序数据集,而不是创建一个有序的数据集,但是它们都可以正常工作。

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中完成,当然,使用经典的顺序和提取括号:

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风水中订购填充颜色?

在这两种情况下的结果都是按照期望的顺序:

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. The default order will plot the first level at the top of the stack instead of the bottom.

在ggplot_2.2.0中,填充顺序是基于因子级别的顺序的。默认顺序将在堆栈顶部而不是底部绘制第一个级别。

If you want the first level at the bottom of the stack you can use reverse = TRUE in position_stack. Note you can also use geom_col as shortcut for geom_bar(stat = "identity").

如果您想要堆栈底部的第一个级别,您可以在position_stack中使用reverse = TRUE。注意,您也可以使用“风水”作为“风水”的快捷方式(stat =“identity”)。

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

#3


4  

You can change the colour using the scale_fill_ functions. For example:

您可以使用scale_fill_函数更改颜色。例如:

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:

为了在栏中获得一致的顺序,您需要订购数据框:

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

In order to change the ordering of legend, alter the gclass factor. So something like:

为了改变传说的顺序,改变gclass因子。所以类似:

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

您如何在ggplot2风水中订购填充颜色?

#4


2  

To order, you must use the levels parameter and inform the order. 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")

#1


21  

You need to specify the order aesthetic as well.

您还需要指定订单审美观。

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

您如何在ggplot2风水中订购填充颜色?

This may or may not be a bug.

这可能是一个错误,也可能不是。

#2


39  

Starting in ggplot2_2.0.0, the order aesthetic is no longer available. 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.

从ggplot2_2.0.0开始,订单的美观不再可用。要获得由填充色排序的堆栈的图形,您可以简单地通过您想要排序的分组变量来订购数据集。

I often use arrange from dplyr for this. 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.

我经常用dplyr来做这个。这里,我在ggplot调用中按填充因子排序数据集,而不是创建一个有序的数据集,但是它们都可以正常工作。

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中完成,当然,使用经典的顺序和提取括号:

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风水中订购填充颜色?

在这两种情况下的结果都是按照期望的顺序:

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. The default order will plot the first level at the top of the stack instead of the bottom.

在ggplot_2.2.0中,填充顺序是基于因子级别的顺序的。默认顺序将在堆栈顶部而不是底部绘制第一个级别。

If you want the first level at the bottom of the stack you can use reverse = TRUE in position_stack. Note you can also use geom_col as shortcut for geom_bar(stat = "identity").

如果您想要堆栈底部的第一个级别,您可以在position_stack中使用reverse = TRUE。注意,您也可以使用“风水”作为“风水”的快捷方式(stat =“identity”)。

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

#3


4  

You can change the colour using the scale_fill_ functions. For example:

您可以使用scale_fill_函数更改颜色。例如:

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:

为了在栏中获得一致的顺序,您需要订购数据框:

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

In order to change the ordering of legend, alter the gclass factor. So something like:

为了改变传说的顺序,改变gclass因子。所以类似:

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

您如何在ggplot2风水中订购填充颜色?

#4


2  

To order, you must use the levels parameter and inform the order. 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")