【问题标题】:ggplot2 setting scales within a facet gridggplot2 在构面网格内设置比例
【发布时间】:2020-11-17 10:28:39
【问题描述】:

我有一个数据集,我用分面网格绘制,我希望每行中的每个图表都具有可比性,所以我想修复每行的 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 轴刻度,使前两行冗余

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

使用scales = "free_y" 我可以释放秤,但每一行不再具有可比性。我想根据分面网格的每一行中出现的最大值来修复 y 轴。 IE。大概第一行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"))

【问题讨论】:

  • 也许它真的很粗糙,但是如果你根据焦点拆分数据,制作 3 个关于性别的图,然后使用 grid.arrange() 或拼凑而成的 wrap_plots 组合?
  • 可以,我希望有一个更优雅的解决方案:(

标签: r ggplot2 dplyr tidyverse


【解决方案1】:

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

我的解决方案是使用facet_grid() 而不是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)))

【讨论】:

  • 太好了,我忘记了 facet grid 命令!
  • 不客气! facet_wrap() 获取一维绘图序列并将它们放在二维上;因此,每行的固定比例相对来说没有意义(对于程序而言)。但是facet_grid() 真正创建了一个二维网格图。此外,您应该考虑到一些面板现在显示在侧面。
猜你喜欢
  • 2018-07-18
  • 2012-04-14
  • 2015-12-11
  • 2013-01-28
  • 2018-11-04
  • 1970-01-01
  • 2016-11-03
  • 1970-01-01
  • 2021-12-04
相关资源
最近更新 更多