ggplot2:geom_pointrange()facet_grid()与coord_flip()和自由标度 [英] ggplot2: geom_pointrange() facet_grid() with coord_flip() and free scales
问题描述
我试图用许多国家的相同回归的估计值和置信区间来生成图表。我使用 dplyr
的 group_by(country)
来运行回归,然后我将所有结果汇总到数据框中与 broom
的 tidy()
。
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()
.
从这个数据框创建图(称为 bycountry1
),我运行以下代码:
When creating the graph from this data frame (called bycountry1
), I run the following code:
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()
,我无法得到这个工作。
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. 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.
推荐答案
遵循aosmith的建议,我使用 geom_errorbarh
并删除 coord_flip()
。我还必须将 geom_errorbarh
的高度
设置为0并添加一个 geom_point
作为估计值。这里是代码:
Following the suggestion of aosmith, I made it work using geom_errorbarh
and removing coord_flip()
. I also had to set the height
of the geom_errorbarh
to 0 and add a geom_point
for the estimate. 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")
并由此产生的图像
这篇关于ggplot2:geom_pointrange()facet_grid()与coord_flip()和自由标度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!