我用ggplot
绘制了一个刻面图,下面是图
我的问题是,刻面(标签)是按字母顺序排序的(例如:E1、E10、E11、E13、E2、E3、I1、I10、I2),但我需要它们是像E1、I1、E2、I2、E3、E10、I10、E11、E13这样的自定义顺序.
我该怎么做?
我用ggplot
绘制了一个刻面图,下面是图
我的问题是,刻面(标签)是按字母顺序排序的(例如:E1、E10、E11、E13、E2、E3、I1、I10、I2),但我需要它们是像E1、I1、E2、I2、E3、E10、I10、E11、E13这样的自定义顺序.
我该怎么做?
如果您提供的分组变量是not a因子,则不要依赖默认的factor()
或ggplot
级别排序.自己明确地设置级别.
dat <- data.frame(x = runif(100), y = runif(100),
Group = gl(5, 20, labels = LETTERS[1:5]))
head(dat)
with(dat, levels(Group))
如果我想让它们按这种任意顺序排列呢?
set.seed(1)
with(dat, sample(levels(Group)))
要做到这一点,请按照您想要的方式设置级别.
set.seed(1) # reset the seed so I get the random order form above
dat <- within(dat, Group <- factor(Group, levels = sample(levels(Group))))
with(dat, levels(Group))
现在,我们可以使用它来按照we的顺序绘制面板:
require(ggplot2)
p <- ggplot(dat, aes(x = x)) + geom_bar()
p + facet_wrap( ~ Group)
它产生: