3

I plot the estimates of six countries using facet_wrap. I would like to add one layer and divide the countries into two groups a and b while keeping the facet_wrap option. This means that i would like to see the plots separated by each country in both groups a and b.

Here is the plot when all countries are together:

df %>%
  ggplot(aes(x= estimate, y=term)) +
  geom_point(mapping=aes(x=estimate, y=term)) +
  facet_wrap( ~ cntry,  strip.position = "bottom") 

enter image description here

I try to separate them using ggforce. As we can see, it does in fact separate them but in fact all countries become mixed in one plot. Is there a way to keep each country plotted alone while being in two different groups?

Here is the code and the plot:

df %>%
  ggplot(aes(x= estimate, y=term)) +
  geom_point(mapping=aes(x=estimate, y=term)) +
  facet_wrap( ~ cntry,  strip.position = "bottom") +
  ggforce::facet_row(vars(cat_f), scales = 'free_x', strip.position = 'bottom')

enter image description here

Here is the data:

structure(
    list(
        cntry = 
            structure(
                c("Austria", "Belgium", "Italy", "Switzerland", "Spain", "Austria","Belgium", "Italy", "Switzerland", "Spain", "Denmark"),
                format.stata = "%15s"
            ), 
        estimate = 
            structure(
                c(1, 1.5, 2, 1.20000004768372, 1.70000004768372, 4, 5.5, 6, 3, 3.09999990463257, 4.30000019073486), 
                format.stata = "%8.0g"
            ), 
        cat_f = 
            structure(c("Widening gap", 
                "Widening gap", "Widening gap", "Widening gap", "Widening gap", 
                "No change", "No change", "No change", "No change", "No change", 
                "No change"), 
                format.stata = "%13s"
            ), 
        term = 
            structure(c("2009", "2009", "2009", "2009", "2009", "2008", "2008", "2008", "2008", "2008", "2008"), 
                format.stata = "%15s"
            )
    ),
    row.names = c(NA, -11L), 
    class = "data.frame"
)
rafagarci
  • 97
  • 9
Jack
  • 813
  • 4
  • 17
  • 2
    How is this different from your [previous question](https://stackoverflow.com/questions/67870187/how-to-order-facet-wrap-that-has-two-factor-variables)? – Limey Jun 08 '21 at 06:27
  • I tried to make its framing more clear than the previous question, luckily i got an answer... – Jack Jun 08 '21 at 07:07

1 Answers1

2

I don't understand the intended output; is this what you are trying to do?

library(tidyverse)
df <- structure(list(cntry = structure(c("Austria", "Belgium", "Italy", 
                                   "Switzerland", "Spain", "Austria", "Belgium", "Italy", "Switzerland", 
                                   "Spain", "Denmark"), format.stata = "%15s"), estimate = structure(c(1, 
                                                                                                       1.5, 2, 1.20000004768372, 1.70000004768372, 4, 5.5, 6, 3, 3.09999990463257, 
                                                                                                       4.30000019073486), format.stata = "%8.0g"), cat_f = structure(c("Widening gap", 
                                                                                                                                                                       "Widening gap", "Widening gap", "Widening gap", "Widening gap", 
                                                                                                                                                                       "No change", "No change", "No change", "No change", "No change", 
                                                                                                                                                                       "No change"), format.stata = "%13s"), term = structure(c("2009", 
                                                                                                                                                                                                                                "2009", "2009", "2009", "2009", "2008", "2008", "2008", "2008", 
                                                                                                                                                                                                                                "2008", "2008"), format.stata = "%15s")), row.names = c(NA, -11L
                                                                                                                                                                                                                                ), class = "data.frame")
df %>%
  ggplot(aes(x= estimate, y=term)) +
  geom_point() +
  facet_grid(cols = vars(cat_f), rows = vars(cntry))

example_1.png

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46