代码之家  ›  专栏  ›  技术社区  ›  jerH

从ggplot bar plot中省略一些数据标签

  •  2
  • jerH  · 技术社区  · 2 年前

    我有一些代码可以从CDCs网站下载新冠肺炎疫苗接种数据,进行一些操作,最终生成这样的条形图

    enter image description here

    你会注意到马列底部的零。马州未接种疫苗的人数越来越少,今天最终达到0。撇开你是否认为马实际上没有未接种疫苗的人不谈,我可以相信这个数字相当小。。。。

    有什么方法可以让ggplot不包括0数据标签吗?

    finalBarPlotData <- structure(list(State = structure(c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 
    9L, 1L, 10L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 10L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 1L, 10L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
    9L, 1L, 10L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 10L), .Label = c("US", 
    "CA", "FL", "GA", "MA", "NM", "OH", "OK", "TN", "UT"), class = "factor"), 
        variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
        3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
        4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Boosted", 
        "Eligible", "Ineligible", "Incomplete", "Unvax"), class = "factor"), 
        value = c(10811037.5, 5225168.9, 1825544.6, 2168196.8, 581220.4, 
        2941639.4, 761232.2, 1397638.8, 80651731.4, 624473.9, 16751023.5, 
        8810766.1, 3818756.4, 3144199.2, 864186.6, 3745298.6, 1439699.8, 
        2217344.2, 133078622.6, 1382887.1, 1257568, 470455, 236940, 
        211389, 64755, 192495, 95483, 127987, 8775787, 72985, 4080930, 
        2745163, 1151722, 1382887, 363931, 592129, 553536, 548320, 
        38221889, 258139, 6611664, 4226184, 3584460, 0, 222736, 4217538, 
        1107020, 2537884, 67511493, 867473)), row.names = c(NA, -50L
    ), class = "data.frame")
    

    以及绘图的代码

    stackColors <- c(Boosted = "green", Eligible="blue", Ineligible="yellow2", 
                     Incomplete ="orange", Unvax = "red3")
    
    
    boosterBar<- ggplot(finalBarPlotData, aes(x=State, y=value,
                                              fill=factor(variable,
                                                          levels = c("Boosted", "Eligible",
                                                                     "Ineligible", "Incomplete",
                                                                     "Unvax")))) +
      geom_bar(stat="identity", position="fill") +   #percent stacked
      labs(x="", y="% of Population", fill="",
           caption="Data: CDC\nNumber in bars represent millions of residents",
           #subtitle = paste("Data as of", format(newestDate, "%A, %B %e, %Y"))) +
      scale_y_continuous(labels = function(x) paste0(x*100, "%")) + # Multiply by 100 & add %  
      theme(plot.title = element_text(size = rel(1), face = "bold"),
            plot.subtitle = element_text(size = rel(0.7)),
            plot.caption = element_text(size = rel(1)),
            axis.text.y = element_text(color='black'),
            axis.title.y = element_text(color='black'),
            axis.text.x = element_text(size=rel(1.25), angle = 45,hjust = 1)) +
      scale_fill_manual(values = stackColors, labels=c("Received Booster","Fully Vax, Booster Eligible","Fully Vax, Booster Ineligible", "First Dose Only", "Unvaccinated")) +
      geom_text(aes(label=round(value/1000000,digits=2)), position=position_fill(vjust=0.5), color="black")
    
    
    print(boosterBar)
    

    提前感谢。。。。

    1 回复  |  直到 2 年前
        1
  •  1
  •   jared_mamrot    2 年前

    在你的情节中有很多事情在发生;也许“最简单”的方法是只打印文本,如果它是>1。

    library(tidyverse)
    
    df <- finalBarPlotData <- structure(list(State = structure(c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 
                                                                 9L, 1L, 10L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 10L, 2L, 3L, 
                                                                 4L, 5L, 6L, 7L, 8L, 9L, 1L, 10L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
                                                                 9L, 1L, 10L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 10L), .Label = c("US", 
                                                                                                                                   "CA", "FL", "GA", "MA", "NM", "OH", "OK", "TN", "UT"), class = "factor"), 
                                             variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
                                                                    3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                                                    4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Boosted", 
                                                                                                                            "Eligible", "Ineligible", "Incomplete", "Unvax"), class = "factor"), 
                                             value = c(10811037.5, 5225168.9, 1825544.6, 2168196.8, 581220.4, 
                                                       2941639.4, 761232.2, 1397638.8, 80651731.4, 624473.9, 16751023.5, 
                                                       8810766.1, 3818756.4, 3144199.2, 864186.6, 3745298.6, 1439699.8, 
                                                       2217344.2, 133078622.6, 1382887.1, 1257568, 470455, 236940, 
                                                       211389, 64755, 192495, 95483, 127987, 8775787, 72985, 4080930, 
                                                       2745163, 1151722, 1382887, 363931, 592129, 553536, 548320, 
                                                       38221889, 258139, 6611664, 4226184, 3584460, 0, 222736, 4217538, 
                                                       1107020, 2537884, 67511493, 867473)), row.names = c(NA, -50L
                                                       ), class = "data.frame")
    
    stackColors <- c(Boosted = "green", Eligible="blue", Ineligible="yellow2", 
                     Incomplete ="orange", Unvax = "red3")
    
    
    boosterBar<- ggplot(finalBarPlotData, aes(x=State, y=value,
                                              fill=factor(variable,
                                                          levels = c("Boosted", "Eligible",
                                                                     "Ineligible", "Incomplete",
                                                                     "Unvax")))) +
      geom_bar(stat="identity", position="fill") +   #percent stacked
      labs(x="", y="% of Population", fill="",
           caption="Data: CDC\nNumber in bars represent millions of residents") +
           #subtitle = paste("Data as of", format(newestDate, "%A, %B %e, %Y"))) +
           scale_y_continuous(labels = function(x) paste0(x*100, "%")) + # Multiply by 100 & add %  
             theme(plot.title = element_text(size = rel(1), face = "bold"),
                   plot.subtitle = element_text(size = rel(0.7)),
                   plot.caption = element_text(size = rel(1)),
                   axis.text.y = element_text(color='black'),
                   axis.title.y = element_text(color='black'),
                   axis.text.x = element_text(size=rel(1.25), angle = 45,hjust = 1)) +
             scale_fill_manual(values = stackColors, labels=c("Received Booster","Fully Vax, Booster Eligible","Fully Vax, Booster Ineligible", "First Dose Only", "Unvaccinated")) +
             geom_text(aes(label=ifelse(value > 1, round(value/1000000,digits=2), "")), position=position_fill(vjust=0.5), color="black")
           
           
           print(boosterBar)
    

    reprex package (v2.0.1)