Search code examples
rggplot2geom-text

How do I reduce the space between geom_text labels?


I have managed to construct the following plot, in which I use stripped down plot containing only geom_text labels to showcase in-group populations for another plot:

enter image description here

The distance between the geom_text labels is too large however, and the in-group populations are not "quite" vertically aligned with the groups they should be representing.

Is there a way to change this distance?

The code for the plot is as follows:

gymnasiegrov_utfall<-totdata%>%as_tibble() %>%
group_by(gymnasiegrov, totstatus_tri) %>% 
summarise(antal = n()) %>% 
mutate(andel = antal / sum(antal))%>% 
ggplot(.) + 
geom_col(mapping = aes(x = gymnasiegrov, y = andel)) + 
coord_flip() + 
facet_wrap(~totstatus_tri)

gymnasiegrov_utfall_antal<-totdata%>% 
ggplot(aes(x=gymnasiegrov))+ 
geom_text(stat='count',aes(label=..count..),hjust="inward",y=1) + 
coord_flip()+
theme_invisible+
scale_y_discrete(breaks=NULL)+
scale_x_discrete(breaks=NULL)

ggarrange(gymnasiegrov_utfall, gymnasiegrov_utfall_antal, ncol=2, widths = c(.85,.15))

subset of data with 1 observation for each category:

structure(list(gymnasiegrov = structure(c(1L, 10L, 6L, 7L, 11L, 
5L, 16L, 3L, 9L, 14L, 12L, 8L, 13L, 15L, 18L, 4L, 2L, 17L), .Label = c("Oklart", 
"friskoleprogram", "Handels- och administrationsprogrammet", 
"specialutformat program", "Vård- och Omsorgsprogrammet", "teknikprogrammet", 
"komvux", "samhälls- och ekonomiprogrammet", "medieprogrammet", 
"naturvetenskapliga programmet", "samhällsvetenskapliga programmet", 
"bygg, el, fordon, hantverk, sjöfart, industriteknik", "naturbruksprogrammet", 
"estetiska programmet", "Barn- och Fritidsprogrammet", "ekonomiprogrammet/ ekonomi", 
"ekonomiprogrammet/ juridik", "Hotell- och Restaurang"), class = c("ordered", 
"factor")), totstatus_tri = structure(c(2L, 3L, 1L, 3L, 2L, 3L, 
2L, 1L, 1L, 1L, 3L, 3L, 1L, 2L, 1L, 2L, 3L, 1L), .Label = c("pågående studier", 
"tidigt avbrott eller återbud", "sent avbrott"), class = c("ordered", 
"factor"))), row.names = c(NA, -18L), groups = structure(list(
    gymnasiegrov = structure(1:18, .Label = c("Oklart", "friskoleprogram", 
    "Handels- och administrationsprogrammet", "specialutformat program", 
    "Vård- och Omsorgsprogrammet", "teknikprogrammet", "komvux", 
    "samhälls- och ekonomiprogrammet", "medieprogrammet", "naturvetenskapliga programmet", 
    "samhällsvetenskapliga programmet", "bygg, el, fordon, hantverk, sjöfart, industriteknik", 
    "naturbruksprogrammet", "estetiska programmet", "Barn- och Fritidsprogrammet", 
    "ekonomiprogrammet/ ekonomi", "ekonomiprogrammet/ juridik", 
    "Hotell- och Restaurang"), class = c("ordered", "factor")), 
    .rows = list(1L, 17L, 8L, 16L, 6L, 3L, 4L, 12L, 9L, 2L, 5L, 
        11L, 13L, 10L, 14L, 7L, 18L, 15L)), row.names = c(NA, 
-18L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

Solution

  • I still think it would be simpler to use the method I described yesterday, but if you prefer the look of this method, then you can do something like this:

    library(ggpubr)
    
    gymnasiegrov_utfall <- totdata %>% 
                           as_tibble() %>%
                           group_by(gymnasiegrov, totstatus_tri) %>% 
                           summarise(antal = n()) %>% 
                           mutate(andel = antal / sum(antal)) %>% 
                           ggplot(.) + 
                           geom_col(mapping = aes(x = gymnasiegrov, y = andel)) + 
                           coord_flip() + 
                           facet_wrap(~totstatus_tri)
    
    gymnasiegrov_utfall_antal <- totdata %>% 
                                 ggplot(aes(x = gymnasiegrov)) + 
                                 geom_text(stat = 'count', aes(label = ..count..), y = 1) + 
                                 coord_flip() +
                                 theme_pubclean() +
                                 theme(strip.text = element_text(margin = margin(1, 0, 5, 0))) +
                                 labs(y = "\n", x = "") +
                                 scale_y_discrete(breaks = NULL) +
                                 scale_x_discrete(breaks = NULL) +
                                 facet_wrap(.~ "count")
    
    ggarrange(gymnasiegrov_utfall, gymnasiegrov_utfall_antal, ncol = 2, widths = c(.85,.15))
    

    enter image description here