本文介绍了停止ggplot2中的文本标签重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  df<-结构(列表(可报告= 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",公路/公路",办公室",办公室"),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%",总计= 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%)"),总计1 = 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),类= c("tbl_df","tbl","data.frame")) 

我这样绘制

 库(tidyverse)库(字符串)图书馆(ggthemes)ggplot(df,aes(fill = Reportable,y = count,x = as.factor(Location1_Description),label = total))+geom_bar(位置=堆栈",状态=身份")+aes(stringr :: str_wrap(as.factor(Location1_Description),15),count)+实验室(x =",y =伤害计数",填充="))+geom_text(大小= 3,位置= position_stack(垂直= 0.5))+scale_fill_manual(值= c("darkorange","cornflowerblue")))+theme_hc()+coord_flip() 

如果您注意到,标签在图中会相互重叠.我尝试了一些尺寸更改,但看不清.我如何弄清楚每个迷你吧在说什么??

我的最终结果应该是一个图,我可以清楚地看到每个条的计数/pct(总计).

解决方案

另一个选择是使用(v0.3.0)创建于2020-07-24 sup>

So I have a dataframe like so

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"))


And I plot as such

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()

If you notice, the labels keep overlapping eachother within the plot. I've tried some size changes but I can't get it to look legible. How could I make it clear what each mini bar is saying???

My end result should be a plot which I can see clearly what count/pct (total) is for each bar.

解决方案

Another option is using the ggfittext package.

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)

这篇关于停止ggplot2中的文本标签重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:34