rggplot2axis-labelsfacet-grid

Can the x axis in facet grid be grouped or filtered?


I have data from a survey of damage on a standing structure that is broken into different sections and units that create each structure. This dataset contains the year the survey was done (Var1), the section type (Var2), unit type (Var3), type of damage (Var4), frequency and percentage.

    Var1       Var2              Var3                    Var4 Freq     Percent
1   2019       Bent       Column Head     Broken off Concrete    3  0.06274838
2   2019      Flume   Expansion Joint     Broken off Concrete    1  0.02091613
3   2019 Cross Beam       Hauch Dowel     Broken off Concrete    1  0.02091613
4   2015      Flume Longitudinal Beam     Broken off Concrete    3  0.15321757
5   2019      Flume Longitudinal Beam     Broken off Concrete    1  0.02091613
6   2015       Bent          Pedestal     Broken off Concrete    2  0.10214505
7   2015       Bent            Column              Cold Joint  209 10.67415730
8   2019       Bent            Column              Cold Joint  102  2.13344489
9   2015       Bent        Cross Beam              Cold Joint    2  0.10214505
10  2015       Bent       Hauch Dowel              Cold Joint   31  1.58324821
11  2015 Cross Beam       Hauch Dowel              Cold Joint  135  6.89479060
12  2019 Cross Beam       Hauch Dowel              Cold Joint    1  0.02091613
13  2019      Inlet        Inlet Base              Cold Joint    1  0.02091613
14  2019      Flume Longitudinal Beam              Cold Joint    4  0.08366451
15  2019      Inlet        North Face              Cold Joint    1  0.02091613
16  2019      Inlet        South Face              Cold Joint    1  0.02091613
17  2019     Outlet        South Wall              Cold Joint    1  0.02091613
18  2019       Bent            Column Construction Deficiency  159  3.32566409
19  2019       Bent       Column Head Construction Deficiency  103  2.15436101
20  2015       Bent        Cross Beam Construction Deficiency    2  0.10214505
21  2019       Bent        Cross Beam Construction Deficiency  150  3.13741895
22  2015 Cross Beam        Cross Beam Construction Deficiency    7  0.35750766
23  2019 Cross Beam        Cross Beam Construction Deficiency   60  1.25496758
24  2019      Flume       Cross Strut Construction Deficiency    5  0.10458063
25  2019      Flume       Flume Shell Construction Deficiency    4  0.08366451
26  2015       Bent       Hauch Dowel Construction Deficiency    1  0.05107252
27  2019       Bent       Hauch Dowel Construction Deficiency   33  0.69023217
28  2015 Cross Beam       Hauch Dowel Construction Deficiency    2  0.10214505
29  2019 Cross Beam       Hauch Dowel Construction Deficiency   67  1.40138046
30  2015      Flume Longitudinal Beam Construction Deficiency    1  0.05107252
31  2019      Flume Longitudinal Beam Construction Deficiency   47  0.98305794
32  2019      Inlet        North Face Construction Deficiency    1  0.02091613
33  2019     Outlet        North Wall Construction Deficiency    2  0.04183225
34  2019       Bent          Pedestal Construction Deficiency    3  0.06274838
35  2019      Inlet        South Face Construction Deficiency    1  0.02091613
36  2015       Bent            Column                Cracking  384 19.61184883
37  2019       Bent            Column                Cracking  884 18.48985568
38  2015       Bent       Column Head                Cracking  295 15.06639428
39  2019       Bent       Column Head                Cracking  159  3.32566409
40  2015       Bent        Cross Beam                Cracking   22  1.12359551
41  2019       Bent        Cross Beam                Cracking  243  5.08261870
42  2015 Cross Beam        Cross Beam                Cracking   32  1.63432074
43  2019 Cross Beam        Cross Beam                Cracking  120  2.50993516
44  2019     Outlet       Cross Brace                Cracking    5  0.10458063
45  2019      Flume       Cross Strut                Cracking   20  0.41832253
46  2015      Flume   Expansion Joint                Cracking    2  0.10214505
47  2019     Siphon          Fin Wall                Cracking   20  0.41832253
48  2015      Flume       Flume Shell                Cracking    6  0.30643514
49  2019      Flume       Flume Shell                Cracking   14  0.29282577
50  2015       Bent       Hauch Dowel                Cracking    7  0.35750766
51  2019       Bent       Hauch Dowel                Cracking  341  7.13239908
52  2015 Cross Beam       Hauch Dowel                Cracking  107  5.46475996
53  2019 Cross Beam       Hauch Dowel                Cracking  201  4.20414139
54  2019      Inlet        Inlet Base                Cracking    9  0.18824514
55  2015      Flume Longitudinal Beam                Cracking   29  1.48110317
56  2019      Flume Longitudinal Beam                Cracking   28  0.58565154
57  2019      Inlet        North Face                Cracking   17  0.35557415
58  2019     Outlet        North Wall                Cracking   23  0.48107091
59  2019     Outlet   Outlet Buttress                Cracking    4  0.08366451
60  2015       Bent          Pedestal                Cracking    9  0.45965271
61  2019       Bent          Pedestal                Cracking  242  5.06170257
62  2019     Siphon   Siphon Buttress                Cracking   22  0.46015478
63  2019     Siphon      Siphon Inlet                Cracking    6  0.12549676
64  2019     Siphon     Siphon Outlet                Cracking    6  0.12549676
65  2019      Inlet        South Face                Cracking   14  0.29282577
66  2019     Outlet        South Wall                Cracking   11  0.23007739
67  2019      Inlet          Spillway                Cracking    2  0.04183225
68  2015       Bent            Column            Delamination    4  0.20429009
69  2015       Bent        Cross Beam            Delamination    2  0.10214505
70  2019      Flume   Expansion Joint            Delamination    1  0.02091613
71  2015      Flume       Flume Shell            Delamination   31  1.58324821
72  2019      Flume       Flume Shell            Delamination   40  0.83664505
73  2019 Cross Beam       Hauch Dowel            Delamination    1  0.02091613
74  2019      Flume Longitudinal Beam            Delamination    1  0.02091613
75  2019     Siphon      Siphon Inlet            Delamination    3  0.06274838
76  2019     Siphon     Siphon Outlet            Delamination    2  0.04183225
77  2019       Bent        Cross Beam          Discolouration   56  1.17130307
78  2019      Flume   Expansion Joint          Discolouration    1  0.02091613
79  2019      Flume       Flume Shell          Discolouration    3  0.06274838
80  2019 Cross Beam       Hauch Dowel          Discolouration    1  0.02091613
81  2019       Bent          Pedestal          Discolouration   12  0.25099352
82  2019       Bent            Column                Graffiti   14  0.29282577
83  2019      Flume       Cross Strut                Graffiti    2  0.04183225
84  2019     Siphon          Fin Wall                Graffiti    4  0.08366451
85  2019      Flume       Flume Shell                Graffiti    5  0.10458063
86  2019      Flume Longitudinal Beam                Graffiti    3  0.06274838
87  2019     Outlet   Outlet Buttress                Graffiti    1  0.02091613
88  2019     Siphon   Siphon Buttress                Graffiti    4  0.08366451
89  2019     Siphon     Siphon Outlet                Graffiti    2  0.04183225
90  2019      Inlet        South Face                Graffiti    1  0.02091613
91  2019     Outlet    Strainer Block                Graffiti    1  0.02091613
92  2015       Bent            Column           Honey Combing   12  0.61287028
93  2019       Bent            Column           Honey Combing  144  3.01192219
94  2019       Bent       Column Head           Honey Combing   11  0.23007739
95  2019       Bent        Cross Beam           Honey Combing   52  1.08763857
96  2019 Cross Beam        Cross Beam           Honey Combing   12  0.25099352
97  2019     Siphon          Fin Wall           Honey Combing    2  0.04183225
98  2019      Flume       Flume Shell           Honey Combing    1  0.02091613
99  2019       Bent       Hauch Dowel           Honey Combing   99  2.07069651
100 2015 Cross Beam       Hauch Dowel           Honey Combing    1  0.05107252
101 2019 Cross Beam       Hauch Dowel           Honey Combing   83  1.73603849
102 2019      Inlet        Inlet Base           Honey Combing    1  0.02091613
103 2019      Flume Longitudinal Beam           Honey Combing    7  0.14641288
104 2019       Bent          Pedestal           Honey Combing   30  0.62748379
105 2019     Siphon   Siphon Buttress           Honey Combing    1  0.02091613
106 2019      Inlet        South Face           Honey Combing    1  0.02091613
107 2019     Outlet        South Wall           Honey Combing    2  0.04183225
108 2019      Inlet          Spillway           Honey Combing    3  0.06274838
109 2019       Bent            Column            Out of Plumb    2  0.04183225
110 2019       Bent       Column Head            Out of Plumb    1  0.02091613
111 2019     Outlet        North Wall            Out of Plumb    1  0.02091613
112 2015       Bent            Column     Scaling of Concrete   21  1.07252298
113 2019       Bent            Column     Scaling of Concrete    6  0.12549676
114 2015       Bent       Column Head     Scaling of Concrete    6  0.30643514
115 2019       Bent       Column Head     Scaling of Concrete    4  0.08366451
116 2015       Bent        Cross Beam     Scaling of Concrete    5  0.25536261
117 2019       Bent        Cross Beam     Scaling of Concrete   26  0.54381928
118 2015 Cross Beam        Cross Beam     Scaling of Concrete   18  0.91930541
119 2019 Cross Beam        Cross Beam     Scaling of Concrete    5  0.10458063
120 2019     Outlet       Cross Brace     Scaling of Concrete    1  0.02091613
121 2019      Flume       Flume Shell     Scaling of Concrete    3  0.06274838
122 2015       Bent       Hauch Dowel     Scaling of Concrete    1  0.05107252
123 2019       Bent       Hauch Dowel     Scaling of Concrete    7  0.14641288
124 2015 Cross Beam       Hauch Dowel     Scaling of Concrete    3  0.15321757
125 2019 Cross Beam       Hauch Dowel     Scaling of Concrete    8  0.16732901
126 2019      Inlet        Inlet Base     Scaling of Concrete    1  0.02091613
127 2015      Flume Longitudinal Beam     Scaling of Concrete    4  0.20429009
128 2019       Bent          Pedestal     Scaling of Concrete    3  0.06274838
129 2015       Bent            Column    Spalling of Concrete   99  5.05617978
130 2019       Bent            Column    Spalling of Concrete  303  6.33758628
131 2015       Bent       Column Head    Spalling of Concrete  139  7.09908069
132 2019       Bent       Column Head    Spalling of Concrete  167  3.49299310
133 2015       Bent        Cross Beam    Spalling of Concrete   48  2.45148110
134 2019       Bent        Cross Beam    Spalling of Concrete   57  1.19221920
135 2015 Cross Beam        Cross Beam    Spalling of Concrete   17  0.86823289
136 2019 Cross Beam        Cross Beam    Spalling of Concrete   32  0.66931604
137 2019     Outlet       Cross Brace    Spalling of Concrete    1  0.02091613
138 2019      Flume       Cross Strut    Spalling of Concrete    1  0.02091613
139 2015      Flume   Expansion Joint    Spalling of Concrete    3  0.15321757
140 2019      Flume   Expansion Joint    Spalling of Concrete    8  0.16732901
141 2019     Siphon          Fin Wall    Spalling of Concrete    6  0.12549676
142 2015      Flume       Flume Shell    Spalling of Concrete    5  0.25536261
143 2019      Flume       Flume Shell    Spalling of Concrete   21  0.43923865
144 2015       Bent       Hauch Dowel    Spalling of Concrete   38  1.94075587
145 2019       Bent       Hauch Dowel    Spalling of Concrete   43  0.89939343
146 2015 Cross Beam       Hauch Dowel    Spalling of Concrete   77  3.93258427
147 2019 Cross Beam       Hauch Dowel    Spalling of Concrete   42  0.87847731
148 2019      Inlet        Inlet Base    Spalling of Concrete    3  0.06274838
149 2015      Flume Longitudinal Beam    Spalling of Concrete  111  5.66905005
150 2019      Flume Longitudinal Beam    Spalling of Concrete   66  1.38046434
151 2015      Inlet        North Face    Spalling of Concrete    1  0.05107252
152 2019      Inlet        North Face    Spalling of Concrete   11  0.23007739
153 2019     Outlet        North Wall    Spalling of Concrete    7  0.14641288
154 2019     Outlet       Outlet Base    Spalling of Concrete    2  0.04183225
155 2019     Outlet   Outlet Buttress    Spalling of Concrete    5  0.10458063
156 2015       Bent          Pedestal    Spalling of Concrete   18  0.91930541
157 2019       Bent          Pedestal    Spalling of Concrete   74  1.54779335
158 2015     Siphon   Siphon Buttress    Spalling of Concrete    2  0.10214505
159 2019     Siphon   Siphon Buttress    Spalling of Concrete    4  0.08366451
160 2019     Siphon      Siphon Inlet    Spalling of Concrete    2  0.04183225
161 2019     Siphon     Siphon Outlet    Spalling of Concrete    4  0.08366451
162 2019      Inlet        South Face    Spalling of Concrete    7  0.14641288
163 2019     Outlet        South Wall    Spalling of Concrete    3  0.06274838
164 2019      Inlet          Spillway    Spalling of Concrete    1  0.02091613
165 2019     Outlet    Strainer Block    Spalling of Concrete   41  0.85756118
166 2019       Bent            Column              Vegetation    4  0.08366451

I have successfully created a plot that combines the year of the survey (Var1), the section type (Var2), the unit type (Var3), and damage type (var4) with the facet gird function.

CUDT_2015_2019_Percent_plot <- CUDT_2015_2019_Percent_Filter %>% ggplot(aes(Var3, Percent, fill = Var4))+geom_col()+facet_grid(CUDT_2015_2019_Percent_Filter$Var1~CUDT_2015_2019_Percent_Filter$Var2)+theme(axis.text.x = element_text(angle = 90, size = 8, hjust = 1, vjust = 0))+labs(fill = "Damage Type", x = "Unit Type")+scale_fill_manual(values = DColour)

However, on the x axis all unit types are represented across all the section types, but not all unit types (Var3) are associated with all section types (Var2).

enter image description here

What I want to do to make the plot more readable is to only have the units that are associated with each of the section types. For example Bents only have Column Head, Column, Pedestal, Cross Beam, and Hauch Dowel. The bent grid does not need to have the flume shell etc. Is there a way of filtering or grouping the x axis?

I have been looking for similar examples within the stack exchange and elsewhere online but have had no success finding an answer. I don't use R enough even to know where to start to try and tackle this.


Solution

  • You could use scales="free_x" in facet_grid to show only categories present in each panel and use `space="free_x"´ to still have bars of equal width:

    library(ggplot2)
    
    CUDT_2015_2019_Percent_Filter |>
      ggplot(aes(Var3, Percent, fill = Var4)) +
      geom_col() +
      facet_grid(
        Var1 ~ Var2,
        scales = "free_x", space = "free_x"
      ) +
      theme(axis.text.x = element_text(
        angle = 90, size = 8,
        hjust = 1, vjust = 0
      )) +
      labs(fill = "Damage Type", x = "Unit Type")
    

    DATA

    CUDT_2015_2019_Percent_Filter <-
      df <- data.frame(
        Var1 = c(
          2019, 2019, 2019, 2015, 2019, 2015, 2015, 2019, 2015, 2015,
          2015, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
          2019, 2015, 2019, 2019, 2015, 2019, 2015, 2019, 2015, 2019,
          2015, 2019, 2019, 2019, 2019
        ),
        Var2 = c(
          "Bent", "Flume", "Cross Beam", "Flume", "Flume", "Bent", "Bent",
          "Bent", "Bent", "Bent", "Cross Beam", "Inlet",
          "Flume", "Inlet", "Inlet", "Outlet", "Bent", "Bent", "Bent",
          "Bent", "Bent", "Bent", "Flume", "Flume", "Bent", "Bent",
          "Cross Beam", "Flume", "Flume", "Bent", "Flume", "Inlet",
          "Outlet", "Bent", "Inlet"
        ),
        Var3 = c(
          "Column Head", "Expansion Joint", "Hauch Dowel", "Longitudinal Beam",
          "Longitudinal Beam", "Pedestal", "Column", "Column", "Cross Beam",
          "Hauch Dowel", "Hauch Dowel", "Hauch Dowel", "Inlet Base",
          "Longitudinal Beam", "North Face", "South Face", "South Wall",
          "Column", "Column Head",
          "Cross Beam", "Cross Beam",
          "Cross Beam", "Cross Beam",
          "Cross Strut", "Flume Shell",
          "Hauch Dowel", "Hauch Dowel",
          "Hauch Dowel", "Hauch Dowel",
          "Longitudinal Beam", "Longitudinal Beam",
          "North Face", "North Wall",
          "Pedestal", "South Face"
        ),
        Var4 = c(
          "Broken off Concrete", "Broken off Concrete", "Broken off Concrete",
          "Broken off Concrete", "Broken off Concrete", "Broken off Concrete",
          "Cold Joint", "Cold Joint", "Cold Joint", "Cold Joint", "Cold Joint",
          "Cold Joint", "Cold Joint", "Cold Joint", "Cold Joint", "Cold Joint",
          "Cold Joint", "Construction Deficiency", "Construction Deficiency",
          "Construction Deficiency", "Construction Deficiency",
          "Construction Deficiency", "Construction Deficiency",
          "Construction Deficiency", "Construction Deficiency",
          "Construction Deficiency", "Construction Deficiency",
          "Construction Deficiency", "Construction Deficiency",
          "Construction Deficiency", "Construction Deficiency",
          "Construction Deficiency", "Construction Deficiency",
          "Construction Deficiency", "Construction Deficiency"
        ),
        Freq = c(
          3, 1, 1, 3, 1, 2, 209, 102, 2, 31, 135, 1, 1, 4, 1, 1, 1, 159,
          103, 2, 150, 7, 60, 5, 4, 1, 33, 2, 67, 1, 47, 1, 2, 3, 1
        ),
        Percent = c(
          0.06274838, 0.02091613, 0.02091613, 0.15321757, 0.02091613,
          0.10214505, 10.67415730, 2.13344489, 0.10214505, 1.58324821,
          6.89479060, 0.02091613, 0.02091613, 0.08366451, 0.02091613,
          0.02091613, 0.02091613, 3.32566409, 2.15436101, 0.10214505,
          3.13741895, 0.35750766, 1.25496758, 0.10458063, 0.08366451,
          0.05107252, 0.69023217, 0.10214505, 1.40138046, 0.05107252,
          0.98305794, 0.02091613, 0.04183225, 0.06274838, 0.02091613
        )
      )