r - 按年份排序facet_wrap图的更简单方法

标签 r ggplot2 facet-wrap

我有 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/

相关文章:

r - 使用 facet_grid 时,我可以为 x 和 y 以外的美学设置自由比例(例如尺寸)吗?

rCharts 在 Shiny 的 : width with 2 charts

r - 更改 igraph 图中子图的颜色

r - 对日期轴有效地使用 lubridate 和 ggplot2

r - R 中的 qplot 和抗锯齿

r - ggplot2 - 用 position = dodge 注释(标记)geom_boxplot

r - 计算并绘制 R 中股票市场每小时/天/月的平均波动率?

c# - 当我运行刺激胃口的应用程序时,R.NET 崩溃了

r - ggplot2:facet_wrap 基于数据集中变量的 strip 颜色

r - 贴标机不使用 facet_wrap 应用标签并返回 NA