ggplot条形图与分类相关的分类顺序 [英] ggplot bar plot with facet-dependent order of categories

查看:196
本文介绍了ggplot条形图与分类相关的分类顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多问题(通常与 ggplot2条形图中的Order Bars 有关如何(重新)订购条形图中的类别。



我所追求的只是一种不同的触摸,但我没有找到一个好的方法:我有一个多方面的柱状图,我想根据另一个变量独立地为每个方面排序x轴(在我的情况下,该变量只是y值本身,即我只是想要酒吧在每个方面增加长度)。

简单的例子,例如在ggplot2条形图中订购酒吧

  df < -  data.frame(name = c('foo','bar','foo','bar'),period = c('old ','old','recent','recent'),val = c(1.23,2.17,4.15,3.65))
p = ggplot(data = df,aes(x = reorder(name,val)), y = val))
p = p + geom_bar(stat ='identity')
p = p + facet_grid(〜period)
p

我们得到如下:

鉴于我想要的是:

解决方案

好的,所有的哲学都抛在一边,如果有人感兴趣,这是一个丑陋的黑客做到这一点。这个想法是使用不同的标签(想想 paste(period,name)),除了我将句号替换为0-空格,1-空格等,以便它们不会显示)。我需要这个阴谋,我不想安排grobs之类的,因为我可能想分享一个共同的传说等。



前面给出的原子例子变成了:

  df < -  data.frame(name = c('foo','bar','foo',' ),
period = c('old','old','recent','recent'),
val = c(1.23,2.17,4.15,3.65),
stringsAsFactors = F)
df $ n = as.numeric(factor(df $ period))
df = ddply(df,。(period,name),transform,x = paste(c '',n-1),name),collapse =''))
df $ x = factor(df $ x,levels = df [order(df $ val),'x'])
p = ggplot(data = df,aes(x = x,y = val))
p = p + geom_bar(stat ='identity')
p = p + facet_grid(〜period,scale ='free_x ')
p

  df < -  ddply(mpg, 。年(制造商),总结,mixmpg =平均值(cty + hwy))
df $制造商= as.character(制造商df $)
df $ n = as.numeric(因子(df $年))
df = ddply(df,。(year,manufacturer),transform,
x = paste(c(rep('',n-1),manufacturer),collapse =''))
df $ x =因子(df $ x,levels = df [order(df $ mixmpg),'x'])
p = ggplot(data = df,aes(x = x,y = mixmpg ))
p = p + geom_bar(stat ='identity')
p = p + facet_grid(〜year,scale ='free_x')
p = p + theme(axis.text.x = element_text(angle = 90,hjust = 1,vjust = .5,color ='gray50'))
p


闭上眼睛,想想帝国,试试享受。

I've seen many questions (often linked to Order Bars in ggplot2 bar graph) about how to (re)order categories in a bar plot.

What I am after is just a touch different, but I haven't found a good way to do it: I have a multi-faceted bar plot, and I want to order the x axis for each facet independently, according to another variable (in my case, that variable is just the y value itself, i.e. I just want the bars to go in increasing length in each facet).

Simple example, following e.g. Order Bars in ggplot2 bar graph:

df <- data.frame(name=c('foo','bar','foo','bar'),period=c('old','old','recent','recent'),val=c(1.23,2.17,4.15,3.65))
p = ggplot(data = df, aes(x = reorder(name, val), y = val))
p = p + geom_bar(stat='identity')
p = p + facet_grid(~period)
p

What we get is the following:

Whereas what I want is:

解决方案

Ok, so all philosophizing aside, and in case anyone is interested, here is an ugly hack to do it. The idea is to use different labels (think paste(period, name) except I replace the period into 0-space, 1-space, etc. so that they don't show). I need this plot and I don't want to arrange grobs and the like, because I might want to share a common legend, etc.

The atomic example given earlier becomes:

df <- data.frame(name=c('foo','bar','foo','bar'),
  period=c('old','old','recent','recent'),
  val=c(1.23,2.17,4.15,3.65),
  stringsAsFactors=F)
df$n = as.numeric(factor(df$period))
df = ddply(df,.(period,name),transform, x=paste(c(rep(' ',n-1), name), collapse=''))
df$x = factor(df$x, levels=df[order(df$val), 'x'])
p = ggplot(data = df, aes(x = x, y = val))
p = p + geom_bar(stat='identity')
p = p + facet_grid(~period, scale='free_x')
p

Another example, still a bit silly but closer to my actual use case, would be:

df <- ddply(mpg, .(year, manufacturer), summarize, mixmpg = mean(cty+hwy))
df$manufacturer = as.character(df$manufacturer)
df$n = as.numeric(factor(df$year))
df = ddply(df, .(year,manufacturer), transform,
     x=paste(c(rep(' ',n-1), manufacturer), collapse=''))
df$x = factor(df$x, levels=df[order(df$mixmpg), 'x'])
p = ggplot(data = df, aes(x = x, y = mixmpg))
p = p + geom_bar(stat='identity')
p = p + facet_grid(~year, scale='free_x')
p = p + theme(axis.text.x=element_text(angle=90,hjust=1,vjust=.5,colour='gray50'))
p

Close your eyes, think of the Empire, and try to enjoy.

这篇关于ggplot条形图与分类相关的分类顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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