Search code examples
rggplot2geom-textggfittext

In `ggfittext::geom_fit_text` , it seems can't use `vjust`


In ggfittext::geom_fit_text , it seems can't use vjust . Below amount and value are override . Anyone can help on this ? Thanks

library(tidyverse)
library(ggfittext)

diamonds %>% group_by(color,clarity) %>% 
  summarise(amount= sum(x),value = sum(y)) %>% 
  ggplot(aes(x=color,y=clarity,fill=amount))+
  geom_tile()+
  ggfittext::geom_fit_text(vjust=2,contrast = TRUE,
                           aes(label = amount))+
  ggfittext::geom_fit_text(vjust=-1,contrast = TRUE,
                           aes(label = value))

Solution

  • As a general rule vjust and hjust should be used to set alignment and should only be set in the range of 0 to 1. Instead, when you want to shift your labels there are always better options, e.g. in geom_fit_text you could use the place= argument to nicely place the labels at the bottom or the top of the bars:

    library(tidyverse)
    library(ggfittext)
    
    diamonds %>%
      group_by(color, clarity) %>%
      summarise(amount = sum(x), value = sum(y)) %>%
      ggplot(aes(x = color, y = clarity, fill = amount)) +
      geom_tile() +
      ggfittext::geom_fit_text(
        contrast = TRUE,
        aes(label = amount),
        place = "top"
      ) +
      ggfittext::geom_fit_text(
        contrast = TRUE,
        aes(label = value),
        place = "bottom"
      )
    #> `summarise()` has grouped output by 'color'. You can override using the
    #> `.groups` argument.