0

I would like to add a text description between my countries on the x-axis to differentiate between them. This description would be 1) "Europe" between (France and Italy) and 2) North America between (US and Canada). If it is possible i would like to add some short straight lines so the reader can see how the groups are divided (i provide another plot example below of what i am aiming for). I tried to get around the problem using geom_text in different ways but i did not succeed. Could someone help please?

Here is my plot:

enter image description here

Here is the data and the code:

df= structure(list(country = structure(1:4, .Label = c("France", "Italy", 
                                                 "US", "Canada"), class = "factor"), term = c("unemp", "unemp", "unemp","unemp"), 
               unemployment = c(10, 11.6, 6.3, 6.5
               )), row.names = c(NA, -4L), class = "data.frame")

df %>%
  ggplot(aes(unemployment, country)) + 
  geom_point(mapping=aes(x=unemployment, y=country)) +
  coord_flip()   

Below you find a good example of what i am trying to achieve for my simple plot. There are three groups and there is a text below the names of the countries and some short straight lines on the side to differentiate between the groups. enter image description here

Jack
  • 813
  • 4
  • 17

1 Answers1

2

I think facets might be a good fit for this problem. If you add an extra column with the corresponding area, you can facet your plot by it and then finetune the theme. However, this is as close as I can get to your example plot, as I am not sure how to only show vertical lines for the strip's rectangle:

# example dataframe
df <- structure(list(country = structure(1:4,
                                         .Label = c("France", "Italy", 
                                                       "US", "Canada"),
                                         class = "factor"),
                     term = c("unemp", "unemp", "unemp","unemp"),
                     unemployment = c(10, 11.6, 6.3, 6.5)),
                row.names = c(NA, -4L), class = "data.frame")
# add areas to dataframe
df$area <- c("Europe", "Europe", "North America", "North America")
library(ggplot2)
library(magrittr)
df %>%
  ggplot(aes(unemployment, country)) + 
  geom_point(mapping=aes(x=unemployment, y=country)) +
  # facet by area
  facet_wrap(vars(area), scales = "free_x", strip.position = "bottom") +
  coord_flip() +
  # customise plot look and position of facet strip
  theme_minimal() +
  theme(strip.placement = "outside",
        strip.background = element_rect(line = "solid"),
        panel.spacing = unit(0, "mm"))

Created on 2021-04-13 by the reprex package (v2.0.0)

stragu
  • 1,051
  • 9
  • 15
  • 1
    for the strips - see this discussion. https://stackoverflow.com/questions/54471816/remove-three-sides-of-border-around-ggplot-facet-strip-label – tjebo Apr 13 '21 at 12:02
  • 2
    in addition to this solution, maybe the `ggh4x` package can be useful with multifaceted plots – tjebo Apr 13 '21 at 12:04