如何对齐直方图和箱线图以便它们共享 x 轴?

问题描述

数据

const char*

对齐直方图和箱线图

我正在尝试使用 class human { const char* name; public: void setName(const char *nameValue); const char* getName(); }; void human::setName(const char *nameValue) { name = nameValue; } const char* human::getName(){ return name; } int main() { human myHuman; const char* name = "walter"; myHuman.setName(name); const char* result3 = myHuman.getName(); cout << "My human has a name of " << result3 << endl; return EXIT_SUCCESS; } 包对齐 wages # A tibble: 1,472 x 3 wage educ exper <dbl> <fct> <dbl> 1 7.78 1 23 2 4.82 1 15 3 10.6 1 31 4 7.04 1 32 5 7.89 1 9 6 8.20 1 15 7 8.21 1 26 8 10.4 1 23 9 11.0 1 13 10 7.21 1 22 # ... with 1,462 more rows 列的直方图和箱线图。我希望它们共享 x 轴,但值有点偏离。请参阅下面的表示:

wage

reprex package (v1.0.0) 于 2021 年 2 月 27 日创建

如何让它们完美共享 x 轴?

解决方法

您不仅应该设置限制,还应该设置扩展范围(为无)和中断:

p1 <- p1 +
  scale_x_continuous(limits= c(0,50),expand = c(0,0),breaks = c(0,10,20,30,40,50))
p2 <- p2+
  scale_x_continuous(limits= c(0,50))

plot_grid(p1,p2,ncol = 1,axis = "l",align = 'v')

enter image description here

,

只需将 xlim(0,50) 添加到每个 ggplot 调用中。