Search code examples
rggplot2geom-text

How to reduce space when using geom_text, paste and \n


I am using geom_text, paste, and \n functions in ggplot2 to add text from estimates and standard errors into a bar plot.

geom_text(aes(label=paste(round(Percent,0), "%", "\n(",round(SE*100, 2), ")", sep = "")), position=position_dodge(width=0.9), vjust=-0.25)

I stack these numbers ontop of each other.

However, the gap between the estimate and the standard error on the figure is larger than I'd like to be. How can I reduce this white space gap?

Here is the plot:

enter image description here

Here is the code to reproduce the plot:

Plot_DF.2.2 <- structure(list(V = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L
), .Label = c("Unvaccinated", "Vaccinated"), class = "factor"), 
SN = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("0", 
                                                             "1", "2", "3"), class = "factor"), Freq = c(323L, 221L, 144L, 
                                                                                                         84L, 101L, 222L, 398L, 976L), Total = c(772L, 772L, 772L, 
                                                                                                                                                 772L, 1697L, 1697L, 1697L, 1697L), Percent = c(41.839378238342, 
                                                                                                                                                                                                28.6269430051813, 18.6528497409326, 10.880829015544, 5.95167943429582, 
                                                                                                                                                                                                13.0819092516205, 23.4531526222746, 57.5132586918091), Proportion = c(0.41839378238342, 
                                                                                                                                                                                                                                                                      0.286269430051813, 0.186528497409326, 0.10880829015544, 0.0595167943429582, 
                                                                                                                                                                                                                                                                      0.130819092516205, 0.234531526222746, 0.575132586918091), 
SE = c(0.0177540926189768, 0.0162684428410836, 0.0140195836873372, 
       0.0112074784287869, 0.00574320564141126, 0.00818558521265792, 
       0.0102854512025968, 0.0119996831224857), margin.error = c(0.0348520936473382, 
                                                                 0.0319356953668124, 0.0275210822684065, 0.0220007913743278, 
                                                                 0.0112645151307286, 0.0160549097906227, 0.0201735107415641, 
                                                                 0.023535743021727), lower = c(0.383541688736081, 0.254333734685001, 
                                                                                               0.15900741514092, 0.0868074987811126, 0.0482522792122296, 
                                                                                               0.114764182725582, 0.214358015481182, 0.551596843896364), 
upper = c(0.453245876030758, 0.318205125418626, 0.214049579677733, 
          0.130809081529768, 0.0707813094736867, 0.146874002306828, 
          0.25470503696431, 0.598668329939818)), row.names = c(NA, 
                                                               8L), class = "data.frame")

P2 <- ggplot(Plot_DF.2.2, aes(x = SN,  y =Proportion, fill = SN)) +
  facet_wrap(V ~ .) +
  geom_bar(stat="identity") +
  labs(x = "Number of Vaccinated Discussants in Respondents' Social Network",
       # y = "Percent",
       title = "Distribution of Vaccination in Social Networks",
       subtitle = "Conditional on Vaccination Status",
       caption = "Note: Numbers represent estimated percentage with estimates' standard errors in parentheses.\nError bar respresents confidence interval around the estimate.") +
  theme_minimal() +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  theme(legend.position = "none",
        plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5),
        plot.caption= element_text(hjust = 0),
        axis.title.y=element_blank()) +
  geom_text(aes(label=paste(round(Percent,0), "%", "\n(",round(SE*100, 2), ")", sep = "")), position=position_dodge(width=0.9), vjust=-0.25); P2


Thank you!


Solution

  • geom_text takes a lineheight argument, which defaults to 1.2. Here it is at 0.9:

    ggplot(Plot_DF.2.2, aes(x = SN, y = Proportion, fill = SN)) +
      facet_wrap(V ~ .) +
      geom_col() +
      labs(x = "Number of Vaccinated Discussants in Respondents' Social Network",
           title = "Distribution of Vaccination in Social Networks",
           subtitle = "Conditional on Vaccination Status",
           caption = paste("Note: Numbers represent estimated percentage with",
                           "estimates' standard errors in parentheses.",
                           "\nError bar represents confidence interval",
                           "around the estimate.")) +
      theme_minimal() +
      scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
      theme(legend.position = "none",
            plot.title = element_text(hjust = 0.5),
            plot.subtitle = element_text(hjust = 0.5),
            plot.caption= element_text(hjust = 0),
            axis.title.y=element_blank()) +
      geom_text(aes(label = paste0(round(Percent,0), "%", "\n(", round(SE*100, 2), ")")), 
                position = position_dodge(width = 0.9), vjust = -0.25,
                lineheight = 0.9)
    

    enter image description here