Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

Is there a way of plotting the facet strip names (switched y) outside the y-axis name in a facet_grid plot?

$
0
0

I made a facetted plot (using ggplot2::facet_grid) where I switched the y-axis, so the y-strip text is plotted on the left side of the plot and already used theme(strip.placement = "outside") to place the strip text outside the y-axis values. However, the strip titles are plotted between the y-axis name and the y-axis values. I would like to plot the strip text outside the y-axis name, so that the y-axis name is plotted right next to the y-axis values. Is that possible?

Here is part of my data:

df<-structure(list(place = c("Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1", 
                             "Site1", "Site1", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2", 
                             "Site2", "Site2", "Site2", "Site2"), year = c(2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
                                                                           2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
                                                                           2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L), month = c(1L, 
                                                                                                                                       1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 
                                                                                                                                       4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 
                                                                                                                                       6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 
                                                                                                                                       8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 
                                                                                                                                       10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 
                                                                                                                                       12L, 12L, 12L, 12L, 12L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
                                                                                                                                       2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
                                                                                                                                       4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 
                                                                                                                                       6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
                                                                                                                                       7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
                                                                                                                                       9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 
                                                                                                                                       10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
                                                                                                                                       11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 
                                                                                                                                       1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
                                                                                                                                       4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 
                                                                                                                                       7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 
                                                                                                                                       9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 
                                                                                                                                       11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
                                                                                                                                       1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
                                                                                                                                       3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
                                                                                                                                       5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 
                                                                                                                                       7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
                                                                                                                                       9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 
                                                                                                                                       10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
                                                                                                                                       11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
                                                                                                                                       12L, 12L, 12L, 12L, 12L), perc = c(0.01, 0.16, 0.06, 0.02, 0, 
                                                                                                                                                                          0, 0.01, 0.05, 0.17, 0.01, 0, 0.02, 0.17, 0.05, 0.02, 0, 0, 0.07, 
                                                                                                                                                                          0.15, 0.01, 0, 0, 0, 0.04, 0.03, 0.14, 0.02, 0.01, 0, 0, 0.01, 
                                                                                                                                                                          0.03, 0.14, 0.06, 0, 0, 0, 0, 0.02, 0.12, 0.03, 0.03, 0, 0, 0, 
                                                                                                                                                                          0, 0.02, 0.11, 0.05, 0.01, 0, 0, 0.02, 0.03, 0.13, 0.01, 0.01, 
                                                                                                                                                                          0, 0, 0, 0, 0.05, 0.08, 0.05, 0.01, 0, 0, 0, 0.04, 0.08, 0.05, 
                                                                                                                                                                          0.02, 0, 0, 0, 0.08, 0.09, 0.01, 0.01, 0, 0, 0, 0, 0.03, 0.14, 
                                                                                                                                                                          0.04, 0.03, 0.01, 0.01, 0, 0, 0.1, 0.1, 0.05, 0, 0, 0, 0, 0.11, 
                                                                                                                                                                          0.11, 0.02, 0.01, 0, 0, 0, 0, 0.03, 0.14, 0.07, 0.02, 0, 0, 0, 
                                                                                                                                                                          0.05, 0.02, 0.16, 0, 0.01, 0, 0, 0, 0, 0.08, 0.1, 0.06, 0.01, 
                                                                                                                                                                          0, 0, 0, 0.01, 0.04, 0.02, 0.03, 0.08, 0.02, 0.01, 0, 0, 0, 0, 
                                                                                                                                                                          0, 0, 0, 0, 0.02, 0.01, 0.05, 0.06, 0.04, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                          0, 0.02, 0.06, 0.09, 0.02, 0, 0, 0, 0, 0, 0, 0.01, 0.02, 0.05, 
                                                                                                                                                                          0.06, 0.04, 0, 0, 0, 0.01, 0.01, 0.07, 0.07, 0.04, 0.01, 0, 0, 
                                                                                                                                                                          0, 0, 0.01, 0.04, 0.07, 0.04, 0.03, 0, 0, 0, 0, 0, 0.16, 0.03, 
                                                                                                                                                                          0.06, 0, 0, 0.15, 0.01, 0.08, 0.01, 0.11, 0.07, 0.07, 0, 0.09, 
                                                                                                                                                                          0.11, 0.04, 0, 0, 0.04, 0.11, 0.08, 0.02, 0, 0, 0, 0.06, 0.12, 
                                                                                                                                                                          0.05, 0.02, 0.01, 0, 0, 0.05, 0.08, 0.05, 0.02, 0.01, 0, 0.08, 
                                                                                                                                                                          0.07, 0.04, 0.01, 0, 0, 0, 0.04, 0.09, 0.05, 0.02, 0, 0, 0, 0.02, 
                                                                                                                                                                          0.06, 0.07, 0.03, 0.01, 0, 0, 0.01, 0.05, 0.07, 0.05, 0.02, 0, 
                                                                                                                                                                          0, 0, 0.01, 0.07, 0.05, 0.05, 0.01, 0, 0, 0, 0.01, 0.07, 0.08, 
                                                                                                                                                                          0.06, 0.03, 0, 0, 0, 0.04, 0.07, 0.06, 0.06, 0.02, 0, 0, 0.03, 
                                                                                                                                                                          0.09, 0.08, 0.03, 0.01, 0, 0, 0.04, 0.09, 0.08, 0.03, 0, 0, 0, 
                                                                                                                                                                          0.06, 0.08, 0.07, 0.03, 0.01, 0, 0, 0.03, 0.08, 0.07, 0.05, 0.01, 
                                                                                                                                                                          0, 0, 0, 0.03, 0.06, 0.05, 0.03, 0.02, 0.01, 0, 0, 0, 0, 0.02, 
                                                                                                                                                                          0.06, 0.05, 0.04, 0.02, 0.01, 0, 0, 0, 0, 0.03, 0.08, 0.05, 0.02, 
                                                                                                                                                                          0.01, 0, 0, 0, 0, 0.02, 0.05, 0.05, 0.03, 0.03, 0.02, 0.01, 0, 
                                                                                                                                                                          0, 0, 0, 0, 0, 0.02, 0.04, 0.05, 0.04, 0.02, 0.01, 0.01, 0, 0, 
                                                                                                                                                                          0, 0, 0.01, 0.03, 0.05, 0.05, 0.03, 0.02, 0, 0, 0, 0, 0, 0), 
                   category = c("0", "1", "2", "3", "4", "0", "1", "2", "3", 
                                "4", "5", "0", "1", "2", "3", "4", "0", "1", "2", "3", "4", 
                                "5", "6", "0", "1", "2", "3", "4", "5", "0", "1", "2", "3", 
                                "4", "5", "6", "7 - 14", "0", "1", "2", "3", "4", "5", "6", 
                                "0", "1", "2", "3", "4", "5", "6", "7 - 14", "0", "1", "2", 
                                "3", "4", "5", "6", "0", "1", "2", "3", "4", "5", "6", "7 - 14", 
                                "0", "1", "2", "3", "4", "5", "6", "0", "1", "2", "3", "4", 
                                "5", "6", "7 - 14", "0", "1", "2", "3", "4", "5", "6", "7 - 14", 
                                "0", "1", "2", "3", "4", "5", "6", "0", "1", "2", "3", "4", 
                                "5", "6", "0", "1", "2", "3", "4", "5", "6", "7 - 14", "0", 
                                "1", "2", "3", "4", "5", "6", "7 - 14", "7 - 14", "0", "1", 
                                "2", "3", "4", "5", "6", "0", "1", "2", "3", "4", "5", "6", 
                                "7 - 14", "7 - 14", "7 - 14", "7 - 14", "7 - 14", "7 - 14", 
                                "7 - 14", "0", "1", "2", "3", "4", "5", "6", "7 - 14", "7 - 14", 
                                "7 - 14", "7 - 14", "7 - 14", "7 - 14", "0", "1", "2", "3", 
                                "4", "5", "6", "7 - 14", "7 - 14", "7 - 14", "7 - 14", "1", 
                                "2", "3", "4", "5", "6", "7 - 14", "7 - 14", "7 - 14", "7 - 14", 
                                "1", "2", "3", "4", "5", "6", "7 - 14", "7 - 14", "1", "2", 
                                "3", "4", "5", "6", "7 - 14", "7 - 14", "7 - 14", "7 - 14", 
                                "7 - 14", "0", "1", "2", "3", "4", "0", "1", "2", "3", "0", 
                                "1", "2", "3", "0", "1", "2", "3", "4", "0", "1", "2", "3", 
                                "4", "5", "6", "0", "1", "2", "3", "4", "5", "6", "0", "1", 
                                "2", "3", "4", "5", "0", "1", "2", "3", "4", "5", "6", "0", 
                                "1", "2", "3", "4", "5", "6", "0", "1", "2", "3", "4", "5", 
                                "6", "0", "1", "2", "3", "4", "5", "6", "7 - 14", "0", "1", 
                                "2", "3", "4", "5", "6", "7 - 14", "0", "1", "2", "3", "4", 
                                "5", "6", "7 - 14", "0", "1", "2", "3", "4", "5", "6", "0", 
                                "1", "2", "3", "4", "5", "6", "0", "1", "2", "3", "4", "5", 
                                "6", "0", "1", "2", "3", "4", "5", "6", "0", "1", "2", "3", 
                                "4", "5", "6", "7 - 14", "0", "1", "2", "3", "4", "5", "6", 
                                "7 - 14", "7 - 14", "7 - 14", "0", "1", "2", "3", "4", "5", 
                                "6", "7 - 14", "7 - 14", "7 - 14", "0", "1", "2", "3", "4", 
                                "5", "6", "7 - 14", "7 - 14", "0", "1", "2", "3", "4", "5", 
                                "6", "7 - 14", "7 - 14", "7 - 14", "7 - 14", "0", "1", "2", 
                                "3", "4", "5", "6", "7 - 14", "7 - 14", "7 - 14", "7 - 14", 
                                "7 - 14", "7 - 14", "0", "1", "2", "3", "4", "5", "6", "7 - 14", 
                                "7 - 14", "7 - 14", "7 - 14", "7 - 14")), row.names = c(NA, 
                                                                                        -379L), class = c("tbl_df", "tbl", "data.frame"))

Here is the code I used to build the plot.

library(ggplot2)
library(lemon)

ggplot(df,aes(x = month, 
                   y = perc,
                   fill = category)) +
  geom_col(position = "fill") +
  facet_rep_grid(place ~ year , 
                 repeat.tick.labels = T,
                 switch = "y") +
  theme_minimal() + 
  theme(strip.text.y = element_text(angle = 180),
        strip.placement = "outside")

And the resulting the plot:

plotwrong

Finally, this is the desired plot:

enter image description here


Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>