pluke
pluke

Reputation: 4346

ggplot2 setting scales within a facet grid

I have a dataset which I'm plotting with a facet grid, I would like each graph in each row to be comparable, so I would like to fix the y axis scale for each row. At the moment I appear to have two options:

     ggplot(data=cs_uptake_gender_graph_data, 
                                     aes(x=Year, y=value, colour=Type)) + 
                                geom_line() + 
                                facet_wrap(focus ~ Gender) + 
                                labs(x = "Year", y = "") +
                                theme(legend.position="bottom",
                                      legend.text=element_text(size=rel(0.5)),
                                      legend.title=element_text(size=rel(0.5)))

which fixes the y axis scale based on the highest value of any graph, making the first two rows redundant

enter image description here

     ggplot(data=cs_uptake_gender_graph_data, 
                                     aes(x=Year, y=value, colour=Type)) + 
                                geom_line() + 
                                facet_wrap(focus ~ Gender, scales = "free_y") + 
                                labs(x = "Year", y = "") +
                                theme(legend.position="bottom",
                                      legend.text=element_text(size=rel(0.5)),
                                      legend.title=element_text(size=rel(0.5)))

enter image description here

Using scales = "free_y" I can free the scales, but each row is no longer comparable. I would like to fix the y axis based on the maximum value that appears in each row of the facet grid. I.e. roughly 0-28 for the first row, 0-31 for the second row, 0-92 for the third row.

The dataset is here:

structure(list(Type = c("Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent", "Comprehensive", "Comprehensive", "Comprehensive", 
"Grammar", "Grammar", "Grammar", "Independent", "Independent", 
"Independent"), Gender = c("Boys", "Girls", "Mixed", "Boys", 
"Girls", "Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", 
"Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", 
"Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", 
"Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", 
"Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", 
"Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", 
"Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", 
"Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", 
"Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", 
"Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", 
"Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", 
"Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", 
"Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", 
"Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", 
"Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", 
"Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", 
"Boys", "Girls", "Mixed", "Boys", "Girls", "Mixed", "Boys", "Girls", 
"Mixed", "Boys", "Girls", "Mixed"), Year = c(2015, 2015, 2015, 
2015, 2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016, 
2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2015, 2015, 
2015, 2015, 2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 
2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2018, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2015, 
2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 
2016, 2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2018, 2018, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019
), focus = c("% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of possible students", "% of possible students", 
"% of possible students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "% of all students", "% of all students", 
"% of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students", "reach % of all students", "reach % of all students", 
"reach % of all students"), value = c(20.3, 10, 12.3, 26.4, 17.5, 
20.7, 14.6, 7.7, 12.9, 19.2, 11.5, 15, 25.5, 17.7, 22.1, 12.5, 
12.2, 14, 23.1, 11.4, 15.8, 30.5, 19.9, 23.6, 26.8, 17.7, 16, 
20.3, 10.4, 15.4, 27.2, 19.6, 26.4, 18, 14.7, 14.1, 23.1, 11.4, 
15.8, 30.5, 19.9, 23.6, 26.8, 17.7, 16, 11, 2.7, 5.9, 17, 7.6, 
13.1, 1.4, 1.3, 3.1, 16.3, 6.7, 11.3, 17.4, 11.7, 15.9, 1.8, 
3.7, 4.7, 20.7, 8.7, 13.5, 28, 17.7, 21.8, 7.6, 9, 7.4, 18.8, 
7, 13, 23.8, 15.5, 20.7, 3.8, 6.8, 6, 20.7, 8.7, 13.5, 28, 17.7, 
21.8, 7.6, 9, 7.4, 54.1, 27.4, 48.3, 64.3, 43.5, 63.5, 9.5, 17.2, 
24.3, 85, 58, 75.2, 68.4, 66.3, 72, 14.5, 30.4, 34, 89.5, 76.4, 
85.7, 91.6, 89, 92.2, 28.4, 51, 46.1, 92.4, 67.4, 84.7, 87.4, 
79.1, 78.7, 21.2, 46.4, 42.4, 89.5, 76.4, 85.7, 91.6, 89, 92.2, 
28.4, 51, 46.1)), row.names = c(NA, -135L), class = c("tbl_df", 
"tbl", "data.frame"))

Upvotes: 0

Views: 295

Answers (1)

Érico Patto
Érico Patto

Reputation: 1015

I have this idea; it'll change your layout a little, so maybe it won't be so interesting for you, but it does what you said.

My solution is to use facet_grid() instead od facet_wrap().

ggplot(data=cs_uptake_gender_graph_data, 
       aes(x=Year, y=value, colour=Type)) + 
  geom_line() + 
  facet_grid(rows = vars(focus), cols = vars(Gender), scales = "free_y") + 
  labs(x = "Year", y = "") +
  theme(legend.position="bottom",
        legend.text=element_text(size=rel(0.5)),
        legend.title=element_text(size=rel(0.5)))

enter image description here

Upvotes: 1

Related Questions