rggplot2geom-textggimage

How to evenly plot geom_text in R?


I am trying to create an infographic style plot in ggplot but am having a lot of trouble figuring out how to start. I want to plot counts of a variable (women) inside of an image, with the name of the country underneath. I figured this would need to be through geom_text labels but I don't know how to get them evenly spaced in a grid and haven't been successful in my internet searches. If anyone could point me in the right direction of what to be searching for or general advice that would be great!

Country   Women
Austria     1
Belgium     3
France      1
Germany     5
etc...

This is an example of what I'm trying to do: example of what I want


Solution

  • You can use the expand.grid() function to lay out coordinates in an evenly spaced grid. Here is an example with some dummy data:

    library(ggplot2)
    
    df <- data.frame(
      Country = LETTERS[1:12],
      Women = sample(12)
    )
    
    grid <- expand.grid(x = 1:4, y = 1:3)
    
    df <- cbind(df, grid)
    
    ggplot(df, aes(x, y * 2)) +
      geom_text(aes(label = Women)) +
      geom_text(aes(label = Country), nudge_y = -1) +
      geom_text(aes(label = "\u2640"), size = 15, nudge_y = -0.075) +
      theme_void()
    

    Created on 2020-07-13 by the reprex package (v0.3.0)

    You'd probably have to fiddle a bit with the nudges in your real plot. These were just chosen for illustration purposes.