Search code examples
rggplot2graphvisualization

ggplot2 Recreate horizontal rectangles like another graph


My goal is to recreate this graph: link

I know how to plot the flag and country with geom_text, but I am not sure about the rectangle with text in the middle(geom_rect() ? ). This is the df I created, but I have no idea how to plot.

df <- tibble(
  country = c('Argentina', 'Uruguay', 'Chile', 'Bolivia', 'Paraguay', 'Ecuador'),
  start = c(1976, 1973, 1973, 1971, 1954, 1972),
  end = c(1983, 1984, 1990, 1978, 1989, 1976),
  dictator = c('Juntas militares', 'Juntas militares', 'Pinochet', 'Hugo Banzer', 'Alfredo Stroessner', 'Guillermo Rodríguez Lara')
)

btw this is my first question so any tips on how to improve, would be appreciated


Solution

  • Try the geom_segment geometry.

    library(ggplot2)
    
    # Data
    df <- tibble(
        country = c('Argentina', 'Uruguay', 'Chile', 'Bolivia', 'Paraguay', 'Ecuador'),
        start = c(1976, 1973, 1973, 1971, 1954, 1972),
        end = c(1983, 1984, 1990, 1978, 1989, 1976),
        dictator = c('Juntas militares', 'Juntas militares', 'Pinochet', 'Hugo Banzer', 'Alfredo Stroessner', 'Guillermo Rodríguez Lara')
    )
    
        ggplot(data = df) +
        geom_segment(
            mapping = aes(x = start, xend = end, y = country, yend = country),
            size = 10,
            alpha = 0.5,
            color = "midnightblue"
        ) + 
        geom_text(aes(x = (start + end)/2, y = country, label = dictator)) +
        theme_minimal()
    

    enter image description here