[英]ggplot2: geom_pointrange() facet_grid() with coord_flip() and free scales
I am trying to generate a graph with the estimates and confidence intervals from the same regression for a number of countries. 我试图从许多国家的同一回归中生成一个包含估计值和置信区间的图表。 I ran the regressions using
dplyr
's group_by(country)
, and then I aggregated all the results into a data frame with broom
's tidy()
. 我使用
dplyr
的group_by(country)
运行了回归,然后我将所有结果聚合到一个带有broom
tidy()
的数据框中。
When creating the graph from this data frame (called bycountry1
), I run the following code: 从此数据框(由
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")
This is what I want, except that I'd like to have the scales for each box to be different, so that all of them would look more like the religious1
box. 这就是我想要的,除了我想让每个盒子的刻度不同,所以它们看起来更像是
religious1
盒子。 Since that is the one with most variability, it dominates the scale, and then in most of the other boxes you cannot see the variance. 由于这是具有最大可变性的那个,它在尺度上占主导地位,然后在大多数其他框中你看不到方差。 As the code above shows, I did indicate
scales = "free"
in facet_grid()
and I tried all the variants, also with facet_wrap()
, and I cannot get this to work. 正如上面的代码所示,我确实在
facet_grid()
指示了scales = "free"
,我尝试了所有变体,也使用了facet_wrap()
,我无法使其工作。
Following the suggestion of aosmith, I made it work using geom_errorbarh
and removing coord_flip()
. 根据aosmith的建议,我使用
geom_errorbarh
并删除了coord_flip()
。 I also had to set the height
of the geom_errorbarh
to 0 and add a geom_point
for the estimate. 我还必须将
geom_errorbarh
的height
设置为0并为估计添加geom_point
。 Here is the code: 这是代码:
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")
And the resulting image 并由此产生的图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.