coord_flip()中ggplot2条形图的图例条目顺序 [英] Order of legend entries in ggplot2 barplots with coord_flip()

查看:3585
本文介绍了coord_flip()中ggplot2条形图的图例条目顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设我有一个数据框,比如:

  set.seed(1234)
my_df< - data.frame(matrix(0,8,4))
名称(my_df)< -c(year,variable,value,vartype)
my_df $ year< - rep(2006:2007)
my_df $ variable< -c(rep(VX,2),rep(VB,2),rep(VZ,2),rep(VD,2))
my_df $ value< - runif (8,5,10)
my_df $ vartype< - c(rep(TA,4),rep(TB,4))

产生下表:

 年变量值vartype 
1 2006 VX 5.568517 TA
2 2007 VX 8.111497 TA
3 2006 VB 8.046374 TA
4 2007 VB 8.116897 TA
5 2006 VZ 9.304577 TB
6 2007 VZ 8.201553 TB
7 2006 VD 5.047479 TB
8 2007 VD 6.162753 TB

有四个变量(VX,VB ,VZ和VD),属于两组变量类型(TA和TB)。

我想将这些值绘制为y轴上的水平条,垂直首先按变量组排序,然后按变量名排序,按年份排列,x轴上的值和变量组对应的填充颜色。
(即在这个简单的例子中,顺序应该是从上至下,VB,VX,VD,VZ)

1)我的第一次尝试是尝试以下操作:

  ggplot(my_df,
aes(x = variable,y = value,fill = vartype ,order = vartype))+
#添加或删除美学order = vartype不会改变任何内容
geom_bar()+
facet_grid(。〜year)+
coord_flip()

但是,这些变量按照反向字母顺序列出,但不是按 vartype order = vartype 审美被忽略。





2)根据我昨天发布的一个类似问题的答案,我尝试了以下内容,依据的是的原始答案 ,也给出了与上面2相同的平台

  my_df < -  <(my_df,
vartype<因子(vartype,
levels = names(sort(table(vartype),
decrease = TRUE)))

我对这样一个事实感到困惑,即尽管有几种方法,但审美 order = vartype 被忽略。尽管如此,它似乎工作在一个无关的问题: http://learnr.wordpress.com/2010/03/23/ggplot2-changing-the-default-order-of-legend-labels-and-stacking- of-data /



我希望问题清楚,欢迎提供任何建议。

Matteo



昨天我发布了一个类似的问题,但不幸的是,在描述问题并提供可重复的示例时,我犯了几个错误。
我听了几个建议,并且在我所知的最好的解决方案的基础上,彻底搜索了stakoverflow以获得类似的问题和应用,无济于事。
我再次发布这个问题,希望能够解决我的问题,并希望能够对其他人有所帮助。

这与 ggplot 无关,而是一个关于生成变量排序以用于重新排列因子级别的问题。这里是你的数据,使用各种函数来实现效果更好:

  set.seed(1234)
df2< ; - data.frame(year = rep(2006:2007),
variable = rep(c(VX,VB,VZ,VD),each = 2),
值= runif(8,5,10),
vartype = rep(c(TA,TB),each = 4))
变量和 vartype 是因素。如果它们不是因素,那么 ggplot()会强制它们,然后按字母顺序排列。我以前曾经这样说过,而且毫无疑问会再说一遍。在您开始绘制/执行数据分析之前,请将您的数据转换为第一 的正确格式。



您需要以下订购:

 > (df2,order(vartype,variable))
[1] 3 4 1 2 7 8 5 6

请注意,我们首先按 vartype 进行排序,然后才由 variable vartype 的级别。如果我们用这个来重新排列变量的等级,我们得到:

  > (df2,reorder(variable,order(vartype,variable)))
[1] VX VX VB VB VZ VZ VD VD
attr(,scores)
VB VD VX VZ
1.5 5.5 3.5 7.5
级别:VB VX VD VZ

(忽略 attr(,scores)位并关注关卡)。这有正确的顺序,但是 ggplot()会自下而上绘制它们,并且您希望从上到下。我不太熟悉 ggplot()来知道这是否可以被控制,所以我们还需要使用递减= TRUE 在 order()



的调用中放在一起,我们有:

  ##在`vartype`内对`variable`变量`variable'
df3< - transform(df2,变量= reorder(变量,order(vartype,变量,
递减= TRUE)))

使用您的绘图代码时:

  ggplot(df3,aes(x = variable,y = value,fill = vartype ))+ 
geom_bar()+
facet_grid(。〜year)+
coord_flip()

产生这样的结果:


I'm struggling get the right ordering of variables in a graph I made with ggplot2 in R.

Suppose I have a dataframe such as:

set.seed(1234)
my_df<- data.frame(matrix(0,8,4))
names(my_df) <- c("year", "variable", "value", "vartype")
my_df$year <- rep(2006:2007)
my_df$variable <- c(rep("VX",2),rep("VB",2),rep("VZ",2),rep("VD",2))
my_df$value <- runif(8, 5,10) 
my_df$vartype<- c(rep("TA",4), rep("TB",4))

which yields the following table:

  year variable    value vartype
1 2006       VX 5.568517      TA
2 2007       VX 8.111497      TA
3 2006       VB 8.046374      TA
4 2007       VB 8.116897      TA
5 2006       VZ 9.304577      TB
6 2007       VZ 8.201553      TB
7 2006       VD 5.047479      TB
8 2007       VD 6.162753      TB

There are four variables (VX, VB, VZ and VD), belonging to two groups of variable types, (TA and TB).

I would like to plot the values as horizontal bars on the y axis, ordered vertically first by variable groups and then by variable names, faceted by year, with values on the x axis and fill colour corresponding to variable group. (i.e. in this simplified example, the order should be, top to bottom, VB, VX, VD, VZ)

1) My first attempt has been to try the following:

ggplot(my_df,        
    aes(x=variable, y=value, fill=vartype, order=vartype)) +
       # adding or removing the aesthetic "order=vartype" doesn't change anything
     geom_bar() + 
     facet_grid(. ~ year) + 
     coord_flip()

However, the variables are listed in reverse alphabetical order, but not by vartype : the order=vartype aesthetic is ignored.

2) Following an answer to a similar question I posted yesterday, i tried the following, based on the post Order Bars in ggplot2 bar graph :

my_df$variable <- factor(
  my_df$variable, 
  levels=rev(sort(unique(my_df$variable))), 
  ordered=TRUE
)

This approach does gets the variables in vertical alphabetical order in the plot, but ignores the fact that the variables should be ordered first by variable goups (with TA-variables on top and TB-variables below).

3) The following gives the same as 2 (above):

my_df$vartype <- factor(
  my_df$vartype, 
  levels=sort(unique(my_df$vartype)), 
  ordered=TRUE
)

... which has the same issues as the first approach (variables listed in reverse alphabetical order, groups ignored)

4) another approach, based on the original answer to Order Bars in ggplot2 bar graph , also gives the same plat as 2, above

my_df <- within(my_df, 
                vartype <- factor(vartype, 
                levels=names(sort(table(vartype),
                decreasing=TRUE)))
                ) 

I'm puzzled by the fact that, despite several approaches, the aesthetic order=vartype is ignored. Still, it seems to work in an unrelated problem: http://learnr.wordpress.com/2010/03/23/ggplot2-changing-the-default-order-of-legend-labels-and-stacking-of-data/

I hope that the problem is clear and welcome any suggestions.

Matteo

I posted a similar question yesterday, but, unfortunately I made several mistakes when descrbing the problem and providing a reproducible example. I've listened to several suggestions since, and thoroughly searched stakoverflow for similar question and applied, to the best of my knowledge, every suggested combination of solutions, to no avail. I'm posting the question again hoping to be able to solve my issue and, hopefully, be helpful to others.

解决方案

This has little to do with ggplot, but is instead a question about generating an ordering of variables to use to reorder the levels of a factor. Here is your data, implemented using the various functions to better effect:

set.seed(1234)
df2 <- data.frame(year = rep(2006:2007), 
                  variable = rep(c("VX","VB","VZ","VD"), each = 2),
                  value = runif(8, 5,10),
                  vartype = rep(c("TA","TB"), each = 4))

Note that this way variable and vartype are factors. If they aren't factors, ggplot() will coerce them and then you get left with alphabetical ordering. I have said this before and will no doubt say it again; get your data into the correct format first before you start plotting / doing data analysis.

You want the following ordering:

> with(df2, order(vartype, variable))
[1] 3 4 1 2 7 8 5 6

where you should note that we get the ordering by vartype first and only then by variable within the levels of vartype. If we use this to reorder the levels of variable we get:

> with(df2, reorder(variable, order(vartype, variable)))
[1] VX VX VB VB VZ VZ VD VD
attr(,"scores")
 VB  VD  VX  VZ 
1.5 5.5 3.5 7.5 
Levels: VB VX VD VZ

(ignore the attr(,"scores") bit and focus on the Levels). This has the right ordering, but ggplot() will draw them bottom to top and you wanted top to bottom. I'm not sufficiently familiar with ggplot() to know if this can be controlled, so we will also need to reverse the ordering using decreasing = TRUE in the call to order().

Putting this all together we have:

## reorder `variable` on `variable` within `vartype`
df3 <- transform(df2, variable = reorder(variable, order(vartype, variable,
                                                         decreasing = TRUE)))

Which when used with your plotting code:

ggplot(df3, aes(x=variable, y=value, fill=vartype)) +
       geom_bar() + 
       facet_grid(. ~ year) + 
       coord_flip()

produces this:

这篇关于coord_flip()中ggplot2条形图的图例条目顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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