Ggplot2: geom_pointrange () facet_grid () with cop_flip () and free scales

I am trying to create a graph with estimates and confidence intervals from the same regression for a number of countries. I performed the regression using dplyr group_by(country) and then I collected all the results in a data frame using broom tidy() .

When creating a 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") 

here is the graph I get

This is what I want, except that I would like the scales for each window to be different, so that they will all look more like religious1 field. Since this is the same one with more variability, it dominates the scale, and then in most other boxes you can not see the variance. As the above code shows, I specified scales = "free" in facet_grid() , and I tried all the options, also with facet_wrap() , and I can't get this to work.

+5
source share
1 answer

Following the aosmith suggestion, I did this using geom_errorbarh and removing coord_flip() . I also had to set the height of geom_errorbarh to 0 and add a geom_point score for the score. 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

enter image description here

+3
source

Source: https://habr.com/ru/post/1240659/


All Articles