【问题标题】:Stop text labels from overlapping in ggplot2阻止文本标签在 ggplot2 中重叠
【发布时间】:2020-11-14 15:24:57
【问题描述】:

所以我有一个这样的数据框

df <- structure(list(Reportable = c("A", "B", 
                                  "A", "B", "A", 
                                  "B", "A", "B", 
                                  "A", "B", "A", 
                                  "B", "A", "B"
), Location1_Description = c("MAIN/BRANCH", "MAIN/BRANCH", "YARD", 
                             "YARD", "PART", "PART", "SHOP", 
                             "SHOP", "LOT", "LOT", "HIGHWAY/ROADWAY", 
                             "HIGHWAY/ROADWAY", "OFFICE", "OFFICE"
), count = c(146L, 447L, 83L, 241L, 44L, 89L, 38L, 83L, 16L, 
             28L, 4L, 30L, 11L, 21L), pct = c("25%", "75%", "26%", "74%", 
                                              "33%", "67%", "31%", "69%", "36%", "64%", "12%", "88%", "33%", 
                                              "64%"), total = c("146 (25%)", "447 (75%)", "83 (26%)", "241 (74%)", 
                                                                "44 (33%)", "89 (67%)", "38 (31%)", "83 (69%)", "16 (36%)", "28 (64%)", 
                                                                "4 (12%)", "30 (88%)", "11 (33%)", "21 (64%)"), total1 = c("146\n(25%)", 
                                                                                                                           "447\n(75%)", "83\n(26%)", "241\n(74%)", "44\n(33%)", "89\n(67%)", 
                                                                                                                           "38\n(31%)", "83\n(69%)", "16\n(36%)", "28\n(64%)", "4\n(12%)", 
                                                                                                                           "30\n(88%)", "11\n(33%)", "21\n(64%)")), row.names = c(NA, -14L
), class = c("tbl_df", "tbl", "data.frame"))
                                                                                

我也是这样设计的

library(tidyverse)
library(stringr)
library(ggthemes)
ggplot(df, aes(fill=Reportable, y=count, x=as.factor(Location1_Description), label = total)) +
  geom_bar(position="stack", stat="identity")+
  aes(stringr::str_wrap(as.factor(Location1_Description), 15), count) +
  labs(x = "", y = "Injury Count", fill = "")+
 geom_text(size = 3, position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values = c("darkorange", "cornflowerblue") ) +
  theme_hc() +
  coord_flip()

如果您注意到,标签会在图中不断相互重叠。我尝试了一些尺寸变化,但我无法让它看起来清晰。我怎么能说清楚每个迷你吧在说什么???

我的最终结果应该是一个图,我可以清楚地看到每个条的 count/pct (total) 是多少。

【问题讨论】:

  • @aosmith 这不是轴标签,而是绘图本身的标签
  • 数据帧代码损坏。它以, row.names = c(NA, -14L 结束。请修复它。我无法运行您的代码,但您是否尝试过使用 ggrepel 包?
  • @DanielR Apoligies,已修复

标签: r ggplot2 tidyverse stringr


【解决方案1】:

您已经在文本标签上有position_stack,因此您也必须手动position_dodge

ggplot(Location1_counts, aes(fill = Reportable, y = count, 
                             x = as.factor(Location1_Description), 
                             label = total)) +
  geom_bar(position = "stack", stat = "identity") +
  aes(stringr::str_wrap(as.factor(Location1_Description), 15), count) +
  labs(x = "", y = "Injury Count", fill = "") +
  geom_text(data = Location1_counts %>% 
                   mutate(total = ifelse(Reportable == "B", "", total)),
            size = 3, vjust = 2, position = position_stack(vjust = 0.5)) +
  geom_text(data = Location1_counts %>% 
                   mutate(total = ifelse(Reportable == "A", "", total)),
            size = 3, vjust = -1.5, position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values = c("darkorange", "cornflowerblue") ) +
  theme_hc() +
  coord_flip()

reprex package (v0.3.0) 于 2020 年 7 月 24 日创建

【讨论】:

    【解决方案2】:

    另一种选择是使用ggfittext 包。

    library(tidyverse)
    library(stringr)
    library(ggthemes)
    
    df <- data.frame(
      stringsAsFactors = FALSE,
      Reportable = c("A","B","A","B","A","B","A","B",
                     "A","B","A","B","A","B"),
      Location1_Description = c("MAIN/BRANCH","MAIN/BRANCH","YARD",
                                "YARD","PART","PART","SHOP","SHOP",
                                "LOT","LOT","HIGHWAY/ROADWAY",
                                "HIGHWAY/ROADWAY","OFFICE","OFFICE"),
      count = c(146L,447L,83L,241L,44L,89L,38L,
                83L,16L,28L,4L,30L,11L,21L),
      pct = c("25%","75%","26%","74%","33%","67%",
              "31%","69%","36%","64%","12%",
              "88%","33%","64%"),
      total = c("146 (25%)","447 (75%)","83 (26%)",
                "241 (74%)","44 (33%)","89 (67%)",
                "38 (31%)","83 (69%)","16 (36%)","28 (64%)",
                "4 (12%)","30 (88%)","11 (33%)",
                "21 (64%)"),
      total1 = c("146\n(25%)","447\n(75%)","83\n(26%)",
                 "241\n(74%)","44\n(33%)","89\n(67%)",
                 "38\n(31%)","83\n(69%)","16\n(36%)",
                 "28\n(64%)","4\n(12%)","30\n(88%)",
                 "11\n(33%)","21\n(64%)")
    )
    
    p <- ggplot(df, aes(fill = Reportable, y = count, x = as.factor(Location1_Description), label = total)) +
      geom_bar(position = "stack", stat = "identity") +
      aes(stringr::str_wrap(as.factor(Location1_Description), 15), count) +
      labs(x = "", y = "Injury Count", fill = "") +
      scale_fill_manual(values = c("darkorange", "cornflowerblue")) +
      theme_hc() +
      coord_flip()
    
    library(ggfittext)
    p +
      geom_bar_text(position = "stack", 
                    outside = TRUE,
                    min.size = 2,
                    reflow = TRUE)
    

    p +
      geom_bar_text(position = "stack", 
                    outside = TRUE,
                    grow = FALSE,
                    min.size = 2)
    

    reprex package (v0.3.0) 于 2020 年 7 月 24 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      相关资源
      最近更新 更多