如何订购ggplot2 geom_bar中的填充颜色 [英] How do you order the fill-colours within ggplot2 geom_bar

查看:207
本文介绍了如何订购ggplot2 geom_bar中的填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用ggplot函数

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

结果是一个条形图,条上填有各种对应于类别的颜色。然而,从酒吧到酒吧的颜色排序并不一致,假设有粉红色,绿色和蓝色,有些酒吧从底部到顶部变成粉红色,绿色,蓝色,有些变成绿色,粉色,蓝色,我没有看到任何明显的模式。



这些排序是如何选择的?我该如何改变它?至少,我怎样才能让ggplot选择一致的排序呢?



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

任何人都知道如何解决这个问题?

可复制的例子:

 dput(data)

结构(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 ,稳定,上下,上升,上升
),等级= c(有序,因素)),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, ),.Names = c(mon,gclass,NG
),row.names = c(NA,-40L),cl (data,aes(mon,NG,fill = gclass))+ geom_bar(stat =identity)
$ / code >


解决方案

您需要指定顺序

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


这可能是也可能不是 bug


I am calling the ggplot function

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?

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.

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

解决方案

You need to specify the order aesthetic as well.

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

This may or may not be a bug.

这篇关于如何订购ggplot2 geom_bar中的填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆