改变x轴g​​gplot2 facet wrap的顺序

时间:2017-09-27 14:02:54

标签: r ggplot2 axis facet

之前我曾问过类似的问题,但无法查询我的问题。这是一个完全可重复的例子。数据如下:

Fact<-structure(list(Code = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
17L), .Label = c("i", "m", "R", "T", "TA", "TB", "TS", "TU", 
"U", "UJ", "UK", "UO", "UY", "UZ", "w", "X", "XHH", "XSW"), class     = "factor"), 
Initials = structure(c(7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("AA", "FF", "HH", 
"II", "KJ", "KK", "LD", "LL", "TT", "WY"), class = "factor"), 
FactorName = structure(c(3L, 11L, 1L, 9L, 8L, 2L, 10L, 7L, 
5L, 6L, 4L, 9L, 10L, 11L, 2L, 1L, 8L, 7L, 3L, 6L, 5L, 4L, 
1L, 3L, 4L, 2L, 6L, 8L, 5L, 11L, 7L, 9L, 10L, 6L, 9L, 3L, 
8L, 7L, 5L, 4L, 2L, 1L, 10L, 11L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 1L, 9L, 1L, 4L, 10L, 11L, 2L, 3L, 5L, 6L, 
7L, 8L, 9L, 7L, 1L, 4L, 8L, 6L, 5L, 3L, 10L, 2L, 11L, 11L, 
10L, 5L, 1L, 2L, 6L, 9L, 4L, 8L, 3L, 7L, 2L, 1L, 4L, 5L, 
3L, 8L, 10L, 11L, 7L, 6L, 9L, 6L, 10L, 11L, 5L, 4L, 9L, 1L, 
7L, 8L, 3L, 2L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 3L, 2L, 6L, 5L, 4L, 1L, 11L, 8L, 10L, 7L, 9L, 9L, 2L, 
3L, 6L, 7L, 8L, 11L, 4L, 1L, 5L, 10L, 4L, 7L, 8L, 9L, 1L, 
2L, 5L, 6L, 10L, 11L, 3L, 11L, 10L, 8L, 3L, 6L, 7L, 9L, 5L, 
2L, 1L, 4L, 4L, 5L, 3L, 6L, 1L, 2L, 10L, 8L, 11L, 7L, 9L, 
9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 6L, 3L, 4L, 
5L, 11L, 7L, 9L, 10L, 1L, 2L, 8L, 4L, 9L, 1L, 5L, 8L, 6L, 
10L, 11L, 2L, 3L, 7L), .Label = c("Exchange Rate Sensitivity", 
"Growth", "Investment Trusts", "Leverage", "Liquidity", "Market Sensitivity", 
"Medium-Term Momentum", "Short-Term Momentum", "Size", "Value", 
"Volatility"), class = "factor"), Rating = c(0.982, 0.471, 
0.532, 0.49, 0.791, 0.235, 0.0159, 0.425, 0.437, 0.642, 0.937, 
0.229, 0.715, 0.537, 0.881, 0.857, 0.687, 0.409, 0.363, 0.567, 
0.328, 0.645, 0.305, 0.826, 0.538, 0.381, 0.726, 0.0473, 
0.884, 0.847, 0.063, 0.278, 0.452, 0.473, 0.981, 0.4, 0.774, 
0.805, 0.982, 0.889, 0.281, 0.288, 0.765, 0.51, 0.784, 0.00634, 
0.293, 0.0331, 0.874, 0.0806, 0.253, 0.295, 0.11, 0.775, 
0.807, 0.164, 0.695, 0.792, 1, 0.57, 0.691, 0.432, 0.252, 
0.318, 0.287, 0.249, 0.997, 0.486, 0.794, 0.228, 0.0345, 
0.295, 0.342, 0.684, 0.346, 0.557, 0.929, 0.89, 0.356, 0.507, 
0.85, 0.353, 0.171, 0.968, 0.915, 0.564, 0.89, 0.00313, 0.39, 
0.274, 0.97, 0.213, 0.0792, 0.549, 0.916, 0.528, 0.248, 0.525, 
0.631, 0.27, 0.0294, 0.221, 0.627, 0.628, 0.666, 0.0401, 
0.784, 0.605, 0.66, 0.602, 0.094, 0.445, 0.389, 0.494, 0.104, 
0.612, 0.834, 0.182, 0.298, 0.464, 0.338, 0.269, 0.843, 0.755, 
0.238, 0.794, 0.266, 0.587, 0.653, 0.873, 0.354, 0.54, 0.451, 
0.863, 0.611, 0.00506, 0.767, 0.477, 0.56, 0.722, 0.125, 
0.667, 0.626, 0.139, 0.364, 0.943, 0.266, 0.223, 0.361, 0.473, 
0.624, 0.167, 0.449, 0.148, 0.334, 0.523, 0.666, 0.503, 0.287, 
0.193, 0.992, 0.468, 0.678, 0.235, 0.221, 0.566, 0.612, 0.00794, 
0.249, 0.329, 0.695, 0.714, 0.236, 0.69, 0.187, 0.721, 0.173, 
0.413, 0.833, 0.984, 0.604, 0.0594, 0.798, 0.684, 0.793, 
0.186, 0.728, 0.923, 0.911, 0.608, 0.634, 0.73, 0.361, 0.0534, 
0.251, 0.871, 0.948, 0.306, 0.483, 0.562, 0.205, 0.0798, 
0.0288, 0.618, 0.784, 0.0358, 0.949, 0.134, 0.141)), .Names = c("Code", 
"Initials", "FactorName", "Rating"), class = "data.frame", row.names = c(NA, 
-209L))

我的情节代码。

Exp<-ggplot(Fact)+ aes(x = Code, y = Rating, fill = Initials) + geom_col()
Exp<- Exp+facet_wrap(~FactorName, ncol = 3, drop = TRUE) + theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) + labs(y="Active Rating") + labs(x="")    
Exp 

现在每个方面的x轴顺序是按字母顺序排列的。相反,我想将首字母组合在一起,以便为每个因子名称图组合条形颜色。即所有红色彼此相邻,蓝色彼此相邻等等。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

你可以尝试

library(tidyverse)
as.tbl(Fact) %>% 
  arrange(Initials) %>%
  mutate(Code2=factor(Code, levels=unique(Code))) %>% 
    ggplot(aes(x = Code2, y = Rating, fill = Initials)) + geom_col()+
        facet_wrap(~FactorName, ncol = 3, drop = TRUE) 

enter image description here

我们的想法是根据Initials进行排列,然后使用unique创建新的因子级别,以适应数据框中的出现。