how to include all axis in facet_wrap

时间:2015-11-12 11:43:06

标签: r ggplot2

The data structure is like this

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")

I do the ggplot

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

and I got enter image description here

What I don't like is the lack of x axis in the right panel. I tried scales = "free" but I'd like to keep the same scale for all the the figures

1 个答案:

答案 0 :(得分:5)

You can 'cheat' the scales by firstly setting the x-axis labels manually and then using 'free_x' inside facet_wrap:

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")

enter image description here

相关问题