我有 20 年的体重数据,我想用 ggplot2 制作一个 10 行 x 2 列的facet_wrap 图。问题是我想重新排序各个方面,使前十年在左列中,最后十年在右列中。
我可以按照其他帖子中的建议手动重新排序级别,但这相当痛苦。另外,如果我想在不同的年份做同样的事情,我将不得不再次重新排序级别。
有更简单的方法吗?这是一个例子来说明我正在谈论的内容:
sub.data$year = factor(sub.data$year,
levels = c(2000,2005,2001,2006,2002,2007,2003,2008,2004,2009...))
ggplot(data = sub.data, aes(x = sub.data$weight)) +
geom_histogram() + facet_wrap(~ year, ncol = 2)
谢谢
最佳答案
我认为你需要dir = "v"
。
library(tidyverse)
df <- data.frame(year = rep(2001:2020, 5), x = runif(100), y = runif(100))
ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(~year, ncol = 2, dir = "v")
由reprex package于2018年11月11日创建(v0.2.1)
关于r - 按年份排序facet_wrap图的更简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53251046/