Search code examples
rggplot2geom-bar

How to plot geom_errorbar with custom values (meta-analysis in R or maybe forest?)


Can anyone help me a bit with plotting a geom_errorbar in R, when my data looks:

> Country Sex Correlation Number  Lower Upper
1  Brazil  Men      -0.108    301 -0.218 0.005
2 Bulgaria Men      -0.012     63 -0.258 0.236
3   Canada Men        0.07     25 -0.334 0.452
4   Brazil Women    -0.074     47 -0.353 0.217
5 Bulgaria Women    -0.042    300 -0.154 0.071
6  Canada  Women     0.092     51 -0.188 0.358

I want to visualize differences in correlations in countries, with respect to sex (filled/coloured sex). I have a mean (Correlation), lower confidence interval for that mean (Lower), and upper (Upper). On the left there should be countries and... basically that's it. Somehow I can't get to it.

When searching through Stackoverflow I wondered if maybe I should rather use some forest functions, as it is perhaps closer to what I imagined.

What I managed to do so far is looking rather poor: link

Thanks in advance!


Solution

  • It sounds like you're looking for something like this:

    ggplot(df, aes(x = Correlation, y = Country, color = Sex)) +
      geom_point(position = position_dodge(width = 0.75)) +
      geom_errorbarh(aes(xmin = Lower, xmax = Upper),
                     position = position_dodge(width = 0.75))
    

    enter image description here

    Data

    df <- structure(list(Country = structure(c(1L, 2L, 3L, 1L, 2L, 3L), 
        .Label = c("Brazil", "Bulgaria", "Canada"), class = "factor"), 
        Sex = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Men", "Women"), 
        class = "factor"), 
        Correlation = c(-0.108, -0.012, 0.07, -0.074, -0.042, 0.092
        ), Number = c(301L, 63L, 25L, 47L, 300L, 51L), Lower = c(-0.218, 
        -0.258, -0.334, -0.353, -0.154, -0.188), Upper = c(0.005, 
        0.236, 0.452, 0.217, 0.071, 0.358)), class = "data.frame", 
        row.names = c("1", "2", "3", "4", "5", "6"))