R ggplot2设置镶嵌面栅格内的比例

R ggplot2设置镶嵌面栅格内的比例,r,ggplot2,dplyr,tidyverse,R,Ggplot2,Dplyr,Tidyverse,我有一个用刻面网格绘制的数据集,我希望每一行中的每个图形都是可比较的,所以我想为每一行固定y轴比例。目前,我似乎有两个选择: ggplot(data=cs_uptake_gender_graph_data, aes(x=Year, y=value, colour=Type)) + geom_line() +

我有一个用刻面网格绘制的数据集,我希望每一行中的每个图形都是可比较的,所以我想为每一行固定y轴比例。目前,我似乎有两个选择:

     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)))
它根据任何图形的最高值固定y轴比例,使前两行冗余

使用
scales=“free_y”
我可以释放刻度,但每行不再具有可比性。我想根据镶嵌面栅格每行中出现的最大值来修复y轴。即第一行大约为0-28,第二行大约为0-31,第三行大约为0-92

数据集如下所示:

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

我有这个想法;它会稍微改变你的布局,所以对你来说可能不会那么有趣,但它会照你说的做

我的解决方案是使用
facet\u grid()
而不是od
facet\u wrap()


也许这真的很粗糙,但是如果你根据焦点分割数据,在性别上绘制3个平面图,然后使用grid.arrange()或wrap_patchwork进行组合?可以,我希望有一个更优雅的解决方案:(太完美了,我忘记了facet grid命令!不客气!
facet_wrap()将一个1D的情节序列映射到2D上;因此,每一行的固定标尺相对来说是没有意义的(对于程序而言)。但是<代码> FAGETYGRID()/Cuth>确实创建了一个2D网格图。
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"))
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)))