我绘制了一个小平面图ggplot
,这里是情节
我遇到的问题是,facet(标签)按字母顺序排序(例如:E1,E10,E11,E13,E2,E3,I1,I10,I2),但我需要它们是E1,I1,E2等自定义顺序,I2,E3,E10,I10,E11,E13.
我怎样才能做到这一点 ?
Rei*_*son 46
如果您提供的分组变量不是一个因素,请不要依赖于由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))
Run Code Online (Sandbox Code Playgroud)
如果我想要这个任意顺序怎么办?
set.seed(1)
with(dat, sample(levels(Group)))
Run Code Online (Sandbox Code Playgroud)
为此,请按照您希望的方式设置级别.
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))
Run Code Online (Sandbox Code Playgroud)
现在我们可以使用它来按照我们想要的顺序绘制面板:
require(ggplot2)
p <- ggplot(dat, aes(x = x)) + geom_bar()
p + facet_wrap( ~ Group)
Run Code Online (Sandbox Code Playgroud)
哪个产生:
归档时间: |
|
查看次数: |
19080 次 |
最近记录: |