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:
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')
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")