在堆叠的ggplot geom_bar中以不同的颜色添加数据标签

时间:2019-11-15 19:10:06

标签: r ggplot2

我正在尝试在此图表上添加数据标签。海军蓝色的数据标签很完美,但是对于天蓝色的数据条,我需要更高一些的黑色。我将代码和数据附加在底部。

enter image description here

ggplot(total.subs, aes(Date,value, fill = variable, label = value)) + #fill variable to make stack colors
        scale_fill_manual("legend", values = c("sources" = "Navy blue", "recipients" = "sky blue")) + #change colors for
        geom_bar(stat = "identity", #stat identiy is for sum                                         #source and recipient  
          position = position_stack(reverse = TRUE)) + # position stack is for reversing the colors + stacks
        xlab("") + #leave the xlabel blank
        ylab("") + #leave the ylabel blank
        theme(axis.line=element_blank(),
                axis.text.y=element_blank(),
                axis.ticks=element_blank(),
                axis.title.y=element_blank(),
                panel.background=element_blank(),
                panel.border=element_blank(),
                panel.grid.major=element_blank(),
                panel.grid.minor=element_blank(),
                plot.background=element_blank()
              ) + geom_text(size = 4, color = "white", position = position_stack(vjust =.95, reverse = T))
  #geom_text(aes(label=variable), position=position_dodge(width=2), vjust=2) +          
  #geom_text(aes(label= value), position=position_dodge(width=5), vjust=5)

我的数据如下

structure(
  list(
    Date = c(
      "Dec-09", "Dec-10", "Dec-11", "Dec-12", 
      "Dec-13", "Dec-14", "Dec-15", "Dec-16", "Dec-17", "Dec-18", "YTD-2019", 
      "Dec-09", "Dec-10", "Dec-11", "Dec-12", "Dec-13", "Dec-14", "Dec-15", 
      "Dec-16", "Dec-17", "Dec-18", "YTD-2019"
    ), variable = c(
      "sources", "sources", "sources", "sources", "sources", "sources", "sources", 
      "sources", "sources", "sources", "sources", "recipients", "recipients", 
      "recipients", "recipients", "recipients", "recipients", "recipients", 
      "recipients", "recipients", "recipients", "recipients"
    ), value = c(
      189, 872, 1507, 2181, 2784, 3125, 4091, 4775, 5284, 3723, 3989, 2, 
      11, 23, 41, 56, 96, 128, 129, 148, 144, 150
    )
  ),
  class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
  row.names = c(NA, -22L)
)

1 个答案:

答案 0 :(得分:1)

您可以在variable调用中将aes用作颜色geom_text。您还可以创建一个新变量vjust来用作变量aes。这样一来,您就可以控制数据的颜色和调整:

library(tidyverse)

total.subs <- structure(
  list(
    Date = c(
      "Dec-09", "Dec-10", "Dec-11", "Dec-12", 
      "Dec-13", "Dec-14", "Dec-15", "Dec-16", "Dec-17", "Dec-18", "YTD-2019", 
      "Dec-09", "Dec-10", "Dec-11", "Dec-12", "Dec-13", "Dec-14", "Dec-15", 
      "Dec-16", "Dec-17", "Dec-18", "YTD-2019"
    ), variable = c(
      "sources", "sources", "sources", "sources", "sources", "sources", "sources", 
      "sources", "sources", "sources", "sources", "recipients", "recipients", 
      "recipients", "recipients", "recipients", "recipients", "recipients", 
      "recipients", "recipients", "recipients", "recipients"
    ), value = c(
      189, 872, 1507, 2181, 2784, 3125, 4091, 4775, 5284, 3723, 3989, 2, 
      11, 23, 41, 56, 96, 128, 129, 148, 144, 150
    )
  ),
  class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
  row.names = c(NA, -22L)
)

total.subs <- total.subs %>% 
  mutate(
    vjust = if_else(variable == 'recipients', -0.8, 1.2)
  )

ggplot(
  total.subs,
  aes(Date,value, fill = variable, label = value) #fill variable to make stack colors
) + 
  scale_fill_manual(
    "legend",
    values = c("sources" = "Navy blue", "recipients" = "sky blue") #change colors for
  ) + 
  geom_bar(
    stat = "identity", #stat identiy is for sum #source and recipient  
    position = position_stack(reverse = FALSE)  # position stack is for reversing the colors + stacks
  ) +
  xlab("") + #leave the xlabel blank
  ylab("") + #leave the ylabel blank
  theme(axis.line=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks=element_blank(),
        axis.title.y=element_blank(),
        panel.background=element_blank(),
        panel.border=element_blank(),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        plot.background=element_blank()
  ) +
  geom_text(
    aes(color = variable, vjust = vjust), size = 4,
    position = position_stack(reverse = FALSE),
    show.legend = FALSE
  ) +
  scale_color_manual(values = c("sources" = "white", "recipients" = "black"))

enter image description here