0
library(ggplot)
library(ggforce)

I am plotting some estimates for 23 countries. As you can see in the plot below, the countries are plotted randomly. I would like to order them starting with the countries of the widening gap group, followed with no change and Contracting gap.

I tried to follow this advice here: controlling order of facet_grid/facet_wrap in ggplot2?, but for some reasons it did not work.

Furthermore, if it is possible i would like to have one word for all countries in each group instead of showing i.e. "widening gap" for each country again and again. I try to do it using ggforce, but countries are not being plotted.

Could someone help please?

Here is the code:

 df %>%
  ggplot(aes(x= estimate, y=term)) +
  geom_point(mapping=aes(x=estimate, y=term), size=2.3, shape=21, fill="black") +
  geom_vline(xintercept=0, color = "black", linetype= "dotted") +
  facet_wrap(vars(cntry,cat_f), scales = "free_x", strip.position = "bottom")

Here is my plot:

enter image description here

Here is an example where i try to divide it into three categories by using ggforce. However, it is not plotting countries once i divide to three categories

df %>%
  ggplot(aes(x= estimate, y=term)) +
  geom_point(mapping=aes(x=estimate, y=term), size=2.3, shape=21, fill="black") +
  geom_vline(xintercept=0, color = "black", linetype= "dotted") +
  facet_wrap( ~ cat_f + cntry,  scales = "free_x", 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(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 
12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 
18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L), .Label = c("Austria", 
"Belgium", "Switzerland", "Czech Republic", "Germany", "Denmark", 
"Estonia", "Greece", "Spain", "Finland", "France", "Hungary", 
"Ireland", "Iceland", "Italy", "Luxembourg", "Netherlands", "Norway", 
"Poland", "Portugal", "Sweden", "Slovakia", "United Kingdom"), class = "factor"), 
    estimate = c(-0.0913140419553834, -0.118306201172409, -0.0239667799007915, 
    -0.0443156002183645, -0.0104216581052402, -0.0548359509121889, 
    0.0131305888416785, 0.0315223511324752, -0.0190273624021191, 
    -0.0186414728272051, 0.0411967940816404, 0.0231043131714301, 
    -0.0171116200795716, -0.0154689392721703, 0.030292299601443, 
    0.0169741956257371, -0.105814083412474, -0.140455990414314, 
    0.0647774293035408, 0.0563998778231824, -0.0235327514882885, 
    0.00143103206722573, 0.000657080813472903, -0.00615188723709141, 
    -0.0262666239250381, 0.0217722749404904, 0.0056080759613924, 
    -0.0717368274077352, 0.00814944419371703, -0.0994925665252663, 
    0.0036407509645274, 0.0265557284220869, -0.0158617373908501, 
    -0.0238087514417458, 0.0167937459496994, -0.00390159593739075, 
    0.0535891490419863, 0.0127836449750844, 0.0594230533290278, 
    0.0292983217374143, -0.0240741202575357, -0.0210028856455069, 
    0.0442436113476246, 0.0569644358417757, 0.0137935174123489, 
    0.00173110517002347), cat_f = structure(c(1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 
    1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 
    1L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 2L, 2L), .Label = c("Widening gap", 
    "No change", "Contracting gap"), class = "factor"), term = c("year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3")), row.names = c(NA, 
-46L), class = "data.frame")
Jack
  • 813
  • 4
  • 17

1 Answers1

1

for ordering them, try changing the facet_wrap like this:

facet_wrap( ~ cat_f + cntry, scales = "free_x", strip.position = "bottom")

To remove the labels, you can remove both using strip.text.x and then use geom_text to add the country name to the plots:

theme(strip.text.x = element_blank()) + geom_text(aes(label = cntry, x = Inf, y = Inf), vjust = 2, hjust = 2)

You will have to adjust the arguments x, y, vjust and hjust to according to the final size of your plot. enter image description here


My suggestion is to present the information on the gap as colours and shapes, as the plot below:

ggplot(dt, aes(x= estimate, y=term)) +
  geom_point(mapping=aes(x=estimate, y=term, shape = cat_f, color = cat_f), size=3) +
  geom_vline(xintercept=0, color = "black", linetype= "dotted") +
  facet_wrap( ~ cat_f + cntry, scales = "free_x", strip.position = "bottom") +
  theme(strip.text.x = element_blank()) + theme(legend.position = c(0.8,0.1)) +
  geom_text(aes(label = cntry, x =  -Inf, y = Inf), vjust = 2, hjust = -1) 

enter image description here

Rosane Rech
  • 123
  • 1
  • 5
  • Thank you for the help. I tried using `ggfocre` to divide between the three categories (contracting gap, no change, widening gap). But then it is not plotting the countries anymore, do you by any chance know why this could be happening? – Jack Jun 07 '21 at 13:05
  • Sorry, I have never used `ggforce` yet. Are you trying to have one row for each category (contracting gap, no change, widening gap)? – Rosane Rech Jun 08 '21 at 03:12
  • Yes to have multiple graphs per each row of contracting gap, no change and widening gap. Each graph represent a country that belongs to one of the three categories – Jack Jun 08 '21 at 06:23
  • You have 13 widening gap, 4 no change, and 6 contracting gap. Just tell me exactly how you would like to arrange them (columns and rows) and I can try to do it for you. Alternatively, you can use colours and shapes in the plot above to differentiate the behaviour, and/or, add the information of widening gap, no change and contracting gap written in the plots. – Rosane Rech Jun 08 '21 at 14:10
  • Thanks a lot, maybe if you could show me the colour way in any order that you want – Jack Jun 08 '21 at 14:18