【问题标题】:how to include all axis in facet_wrap如何在 facet_wrap 中包含所有轴
【发布时间】:2015-11-12 11:43:05
【问题描述】:

数据结构是这样的

test<- structure(list(yta = c(1101L, 1101L, 1101L, 1101L, 1101L, 1101L, 
1101L, 1101L, 1101L, 1101L, 1101L, 1101L, 1101L, 1101L, 1101L, 
1101L, 1102L, 1102L, 1102L, 1102L, 1102L, 1102L, 1102L, 1102L, 
1102L, 1102L, 1102L, 1102L, 1102L, 1102L, 1102L, 1102L, 1102L, 
1102L, 1102L, 1102L, 1103L, 1103L, 1103L, 1103L, 1103L, 1103L, 
1103L, 1103L, 1103L, 1103L, 1103L, 1103L, 1103L, 1103L, 1103L, 
1103L, 1103L, 1103L, 1103L, 1103L), id = c("1101_3", "1101_3", 
"1101_3", "1101_3", "1101_3", "1101_3", "1101_3", "1101_3", "1101_4", 
"1101_4", "1101_4", "1101_4", "1101_4", "1101_4", "1101_4", "1101_4", 
"1102_3", "1102_3", "1102_3", "1102_3", "1102_3", "1102_3", "1102_3", 
"1102_3", "1102_3", "1102_3", "1102_6", "1102_6", "1102_6", "1102_6", 
"1102_6", "1102_6", "1102_6", "1102_6", "1102_6", "1102_6", "1103_1", 
"1103_1", "1103_1", "1103_1", "1103_1", "1103_1", "1103_1", "1103_1", 
"1103_1", "1103_1", "1103_2", "1103_2", "1103_2", "1103_2", "1103_2", 
"1103_2", "1103_2", "1103_2", "1103_2", "1103_2"), Htgeg = c(23.5, 
20, 26.3, 21.1, 28.8, 21.6, 25.3, 16.3, 29.5, 22.5, 32.2, 23.7, 
34.6, 26, 33.2, 29.5, 23.1, 23.1, 29.5, 22.2, 28.3, 25.4, 32, 
31.5, 34.3, 33.5, 22.7, 22.7, 29.1, 14.6, 19.9, 19.4, 27.3, 27.1, 
29.9, 29.6, 21.3, 21.3, 26.8, 20.3, 25.6, 20, 24.4, 17.5, 20.4, 
19.6, 22.1, 22.1, 28.6, 14.3, 19.6, 18.9, 24.8, 23.1, 25.5, 25.5
), alder = c(31L, 31L, 38L, 38L, 47L, 47L, 53L, 53L, 31L, 31L, 
38L, 38L, 47L, 47L, 53L, 53L, 27L, 27L, 32L, 32L, 39L, 39L, 49L, 
49L, 54L, 54L, 27L, 27L, 32L, 32L, 39L, 39L, 49L, 49L, 54L, 54L, 
27L, 27L, 32L, 32L, 39L, 39L, 49L, 49L, 54L, 54L, 27L, 27L, 32L, 
32L, 39L, 39L, 49L, 49L, 54L, 54L), beh = structure(c(2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L), .Label = c("", "25%", "25%EXT", "25%N", 
"25%SEN", "25%TALL", "25%TID", "50%", "50%EXT", "OGAL"), class = "factor")), .Names = c("yta", 
"id", "Htgeg", "alder", "beh"), row.names = c("24", "24.1", "25", 
"25.1", "26", "26.1", "27", "27.1", "28", "28.1", "29", "29.1", 
"30", "30.1", "31", "31.1", "457", "457.1", "458", "458.1", "459", 
"459.1", "460", "460.1", "461", "461.1", "472", "472.1", "473", 
"473.1", "474", "474.1", "475", "475.1", "476", "476.1", "482", 
"482.1", "483", "483.1", "484", "484.1", "485", "485.1", "486", 
"486.1", "487", "487.1", "488", "488.1", "489", "489.1", "490", 
"490.1", "491", "491.1"), class = "data.frame")

我做ggplot

ggplot(aes(x = alder, y = Htgeg), data = test) + geom_line(aes(lty = id)) + facet_wrap(~yta, ncol = 2)

我得到了

我不喜欢的是右侧面板中缺少x axis。我试过scales = "free",但我想为所有数字保持相同的scale

【问题讨论】:

  • 最快的解决方案是首先强制缩放(使用xlim(c(25,60))),然后在对 facet_wrap 的调用中使用 scales="free_x"。
  • 完美!谢谢@Heroka
  • 不客气。我会把它写下来作为答案[用于存档目的]
  • 还有这个:stackoverflow.com/questions/13297155/…,我用过很多次了。

标签: r ggplot2


【解决方案1】:

您可以先手动设置 x 轴标签,然后在 facet_wrap 中使用“free_x”来“欺骗”比例:

ggplot(aes(x = alder, y = Htgeg), data = test) + 
  geom_line(aes(lty = id)) + xlim(c(25,60))+
  facet_wrap(~yta, ncol = 2,scales = "free_x")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多