【问题标题】:Simpler way to order facet_wrap plot by year按年订购 facet_wrap 绘图的更简单方法
【发布时间】:2018-11-11 16:57:40
【问题描述】:

我有 20 年的体重数据,我想用 ggplot2 制作一个 10 行乘 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)

谢谢

【问题讨论】:

    标签: r ggplot2 facet-wrap


    【解决方案1】:

    我认为你需要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 (v0.2.1) 于 2018 年 11 月 11 日创建

    【讨论】:

    • 啊!我怎么能错过这个?
    猜你喜欢
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2011-06-15
    相关资源
    最近更新 更多