控制ggplot2中facet_grid / facet_wrap的顺序? [英] controlling order of facet_grid/facet_wrap in ggplot2?

查看:1918
本文介绍了控制ggplot2中facet_grid / facet_wrap的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ggplot中使用 facet_wrap facet_grid 来绘制事物,如:

  ggplot(iris)+ geom_histogram(aes(iris $ Petal.Width))+ facet_grid(Species〜。)

是否可以控制图中 Species 面板排序的顺序?这可以在不更改 iris 数据框或创建新数据框的情况下完成吗?这里的默认显示setosa,versicolor,virginica,但我想要一个不同的顺序。谢谢。

解决方案

我不认为我真的可以满足您的没有制定新的数据框架要求,但您可以创建新的数据框:

  ggplot(transform(iris,
Species = factor(Species,levels = c(virginica,setosa,versicolor))))+
geom_histogram(aes(Petal.Width))+ facet_grid(Species〜。)

我同意如果有另一种方法来控制它,但是 ggplot 已经很好一个非常强大的(和复杂的)引擎...



请注意,(1)数据集行的顺序独立于(2)因子的等级的顺序。 #2是什么因素(...,levels = ...)变化,以及 ggplot 看的是什么确定方面的顺序。做#1(按照指定的顺序排列数据框的行)是一个有趣的挑战。我想我会先通过#2做到这一点,然后使用 order() arrange()来实现按照因子的数值进行排序:

  neworder <-c(virginica,setosa,versicolor )
library(plyr)##或dplyr(transform - > mutate)
iris2 < - arrange(transform(iris,
Species = factor(Species,levels = neworder)) ,物种)

我无法立即看到快速的方式来执行此操作改变因子水平的顺序(你可以这样做,然后重新设置因子水平的顺序)。

通常,R中的函数取决于分类变量的级别顺序基于因子级别顺序,而不是数据集中行的顺序:上面的答案更一般地适用。


I am plotting things using facet_wrap and facet_grid in ggplot, like:

ggplot(iris) + geom_histogram(aes(iris$Petal.Width)) + facet_grid(Species ~ .)

Is it possible to control the order in which the Species panels are ordered in the plot? Can this be done without changing the iris dataframe or making a new one? The default here shows setosa, versicolor, virginica but I'd like a different order. thanks.

解决方案

I don't think I can really satisfy your "without making a new data frame" requirement, but you can create the new data frame on the fly:

ggplot(transform(iris,
      Species=factor(Species,levels=c("virginica","setosa","versicolor")))) + 
    geom_histogram(aes(Petal.Width))+ facet_grid(Species~.)

I agree it would be nice if there were another way to control this, but ggplot is already a pretty powerful (and complicated) engine ...

Note that the order of (1) the rows in the data set is independent of the order of (2) the levels of the factor. #2 is what factor(...,levels=...) changes, and what ggplot looks at to determine the order of the facets. Doing #1 (sorting the rows of the data frame in a specified order) is an interesting challenge. I think I would actually achieve this by doing #2 first, and then using order() or arrange() to sort according to the numeric values of the factor:

neworder <- c("virginica","setosa","versicolor")
library(plyr)  ## or dplyr (transform -> mutate)
iris2 <- arrange(transform(iris,
             Species=factor(Species,levels=neworder)),Species)

I can't immediately see a quick way to do this without changing the order of the factor levels (you could do it and then reset the order of the factor levels accordingly).

In general, functions in R that depend on the order of levels of a categorical variable are based on factor level order, not the order of the rows in the dataset: the answer above applies more generally.

这篇关于控制ggplot2中facet_grid / facet_wrap的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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