【问题标题】:Removing "False"-condition scales::dollar labels on ifelse within geom_label删除 geom_label 中 ifelse 上的“假”条件 scales::dollar 标签
【发布时间】:2018-06-05 19:22:40
【问题描述】:

仅当值为负时才尝试建立单独的条形数据标签。对于包含简单整数的变量,我能够做到这一点,但是对于需要使用千位分隔符格式化为美元的变量,我似乎无法摆脱“NA”标签。

DolSumPlot <- ggplot(data = DolSums, aes(x = Group.1, fill = Group.2)) + 
  geom_bar(aes(weight = x), position = position_stack(reverse = TRUE)) + 
  coord_flip() + 
  labs(title = "Dollars Billed by Technician and Shop, Between 02/01/2018 and 05/31/2018", 
       y = "Dollars Billed", x = "Technician", fill = "Shop") + 
  scale_y_continuous(limits= c(NA,NA),
                     labels = scales::dollar,
                     breaks = seq(0, 50000 + 10000, 5000*2),
                     minor_breaks = seq(0,50000 + 10000, by = 5000)) +
  scale_fill_brewer(palette = "Set1") + 
  geom_label(aes(label=scales::dollar(ifelse(DolSums$x < 0, DolSums$x,NA)), 
                 y = DolSums$x), 
             show.legend = FALSE, size = 2.6, colour = "white", fontface = "bold")

数据:

DolSums = structure(list(Group.1 = c((names)), Group.2 = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
5L, 5L, 5L, 5L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Radio", 
"Video", "Engineering", "800Mhz", "PSSRP", "Other"), class = "factor"), 
    x = c(4646, 16008.5, 48793.1, 4040, 14468.25, 13332, 1565.5, 
    6060, 6549.85, 2929, 4444, 3257.25, 5904, 2029.5, 3321, 6767, 
    8105.25, 8105.25, 8130.5, 3131, 5075.25, 3383.5, 4418.75, 
    23381.5, 1363.5, -2323, 29133.45, 2550.25, 505, 26042.85, 
    35203.55, 35940.85, 1641.25, 45066.2, 37541.7, 606, 45439.9
    )), .Names = c("Group.1", "Group.2", "x"), row.names = c(NA, 
-37L), class = "data.frame")

【问题讨论】:

    标签: r ggplot2 label


    【解决方案1】:

    您可以通过在geom_label 中使用data 参数并仅对带有负数x 的行进行子集化来做到这一点。另请注意,由于您已经将DolSums 作为输入,因此无需编写DolSums$x。相反,使用列名直接引用特定列:

    library(ggplot2)
    
    ggplot(data = DolSums, aes(x = Group.1, fill = Group.2)) + 
      geom_bar(aes(weight = x), position = position_stack(reverse = TRUE)) + 
      coord_flip() + 
      labs(title = "Dollars Billed by Technician and Shop, Between 02/01/2018 and 05/31/2018", 
           y = "Dollars Billed", x = "Technician", fill = "Shop") + 
      scale_y_continuous(limits= c(NA,NA),
                         labels = scales::dollar,
                         breaks = seq(0, 50000 + 10000, 5000*2),
                         minor_breaks = seq(0,50000 + 10000, by = 5000)) +
      scale_fill_brewer(palette = "Set1") + 
      geom_label(data = DolSums[DolSums$x < 0,], 
                 aes(label=scales::dollar(x), 
                     y = x), 
                 show.legend = FALSE, size = 2.6, colour = "white", fontface = "bold")
    

    【讨论】:

    • 嘿,这是一个巧妙的技巧!我没有意识到您实际上可以设置一个不同的数据集来生成标签,这与主要数据不冲突。你帮了大忙,谢谢。
    • @MarkBunster 确实很整洁。事实上,ggplot2 中的几乎所有geom 函数都有一个数据参数,允许您输入该特定几何所需的任何数据集。您只需要检查文档以确保。
    • 想想这一切背后的主要天才就在我身边……再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    相关资源
    最近更新 更多