![](/img/trans.png)
[英]How to Use ggplot2 geom_col with facet_grid and coord_flip
[英]ggplot2: geom_pointrange() facet_grid() with coord_flip() and free scales
我试图从许多国家的同一回归中生成一个包含估计值和置信区间的图表。 我使用dplyr
的group_by(country)
运行了回归,然后我将所有结果聚合到一个带有broom
tidy()
的数据框中。
从此数据框(由bycountry1
调用)创建图形时,我运行以下代码:
ggplot(bycountry1, aes(x = country, y = estimate, ymin = estimate - std.error * 2, ymax = estimate + std.error * 2)) +
geom_hline(yintercept = 0, colour = "black", lty = 2) +
geom_pointrange() +
coord_flip() + facet_grid(. ~ term, scales = "free")
这就是我想要的,除了我想让每个盒子的刻度不同,所以它们看起来更像是religious1
盒子。 由于这是具有最大可变性的那个,它在尺度上占主导地位,然后在大多数其他框中你看不到方差。 正如上面的代码所示,我确实在facet_grid()
指示了scales = "free"
,我尝试了所有变体,也使用了facet_wrap()
,我无法使其工作。
根据aosmith的建议,我使用geom_errorbarh
并删除了coord_flip()
。 我还必须将geom_errorbarh
的height
设置为0并为估计添加geom_point
。 这是代码:
ggplot(bycountry1, aes(y = country, x = estimate, xmin = estimate - std.error * 2, xmax = estimate + std.error * 2)) +
geom_vline(xintercept = 0, colour = "black", lty = 2) +
geom_point() +
geom_errorbarh(height = 0) +
facet_grid(. ~ term, scales = "free")
并由此产生的图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.