How can I access dimensions of labels plotted by `geom_text` in `ggplot2`?

前端 未结 1 1783
青春惊慌失措
青春惊慌失措 2021-02-09 01:18

As far as I can see ggplot2 knows the dimensions of labels plotted by geom_text. Otherwise the check_overlap option would not wor

相关标签:
1条回答
  • 2021-02-09 01:58

    If you are just looking to avoid overlapping labels, the ggrepel package works pretty well.

    library(ggplot2)
    library(ggrepel)
    df <- data.frame(x = c(1, 2), 
                     y = c(1, 1), 
                     label = c("label-one-that-might-overlap-another-label", 
                               "label-two-that-might-overlap-another-label"), 
                     stringsAsFactors = FALSE)
    ggplot(df, aes(x, y)) + 
      geom_text_repel(aes(label = label), check_overlap = F) + 
      xlim(0, 3) 
    

    The above code produces the graph below.

    0 讨论(0)
提交回复
热议问题