【问题标题】:Adding percent change between bars on a ggplot在ggplot上的条形之间添加百分比变化
【发布时间】:2017-10-19 17:33:35
【问题描述】:

我有一个 geom_bar 图,其中包含视觉本身上每个条的值的标签。

这是生成绘图的代码:

# libraries
library(shiny)
library(tidyverse)

# funnel bar blot
  output$funnel_plot <- renderPlot({
    ggplot(exampledf, aes(x = reorder(Funnel, -Sessions), y = Sessions)) +
      geom_bar(stat = "identity", fill = "#008080", alpha = 0.6) +
      stat_summary(aes(label = scales::comma(..y..)), fun.y = 'sum', geom = 'text', col = 'white', vjust = 1.5) +
      xlab("Step") +
      ylab("Events") +
      scale_y_continuous(labels = function(l) {l = l / 1000; paste0(l, "K")})
  })

这里有一个数据框供输入。

exampledf <- structure(list(Channel = c("Facebook", "Youtube", "SEM", "Organic", 
"Direct", "Email", "Facebook", "Youtube", "SEM", "Organic", "Direct", 
"Email", "Facebook", "Youtube", "SEM", "Organic", "Direct", "Email", 
"Facebook", "Youtube", "SEM", "Organic", "Direct", "Email"), 
    Promo = c("None", "None", "None", "None", "None", "None", 
    "Partner Offer", "Partner Offer", "Partner Offer", "Partner Offer", 
    "Partner Offer", "Partner Offer", "Print Code", "Print Code", 
    "Print Code", "Print Code", "Print Code", "Print Code", "Affiliate Promo", 
    "Affiliate Promo", "Affiliate Promo", "Affiliate Promo", 
    "Affiliate Promo", "Affiliate Promo"), Sessions = c(26126, 
    16885, 32229, 2446, 16353, 79202, 7688, 83475, 48834, 53475, 
    71238, 78728, 76710, 125571, 125719, 17142, 103206, 181082, 
    27071, 42571, 716, 139871, 21676, 59560), AddToCart = c(7228, 
    4436, 8344, 575, 4275, 23681, 1982, 18489, 14433, 14995, 
    20769, 20119, 18471, 35566, 33423, 5187, 28138, 48186, 7140, 
    11602, 190, 35795, 5193, 17064), Registrations = c(2780, 
    1706, 3209, 221, 1644, 9108, 762, 7111, 5551, 5767, 7988, 
    7738, 7104, 13679, 12855, 1995, 10822, 18533, 2746, 4462, 
    73, 13767, 1997, 6563), ShippingDetails = c(1069, 656, 1234, 
    85, 632, 3503, 293, 2735, 2135, 2218, 3072, 2976, 2732, 5261, 
    4944, 767, 4162, 7128, 1056, 1716, 28, 5295, 768, 2524), 
    Checkout = c(668, 410, 771, 53, 395, 2189, 183, 1709, 1334, 
    1386, 1920, 1860, 1707, 3288, 3090, 479, 2601, 4455, 660, 
    1072, 17, 3309, 480, 1577), Transactions = c(556, 341, 642, 
    44, 329, 1824, 152, 1424, 1111, 1155, 1600, 1550, 1422, 2740, 
    2575, 399, 2167, 3712, 550, 893, 14, 2757, 400, 1314)), class = "data.frame", row.names = c(NA, 
-24L), .Names = c("Channel", "Promo", "Sessions", "AddToCart", 
"Registrations", "ShippingDetails", "Checkout", "Transactions"
))

这是情节的屏幕截图:

我想在显示百分比变化的每个条之间添加一个新行(也许是一个新的统计摘要?)。对于前 2 个柱、会话和添加到购物车,值已从 1.4M 变为 385k = 下降约 72%。所以,我希望在条形之间以某种方式显示“72%”。

有没有直接的方法来做到这一点?

我可以创建一个表格并在可视化下显示它,但我想看看它看起来如何将百分比下降添加到可视化本身。

如何添加从左到右读取的每个条形之间的百分比下降?

【问题讨论】:

标签: r ggplot2


【解决方案1】:

你可以试试:

as.tbl(df) %>% 
  gather(key, value, -Channel, -Promo) %>% 
  group_by(key) %>% 
  summarise(Sum=sum(value)) %>% 
  arrange(-Sum) %>% 
  mutate(End=lag(Sum),
         xpos=1:n()-0.5,
         Diff=End-Sum,
         Percent=paste(round(Diff/End*100,1),"%")) %>% 
    ggplot(aes(x = reorder(key, -Sum), y = Sum)) +
    geom_col(alpha = 0.6) + 
    stat_summary(aes(label = scales::comma(..y..)), fun.y = 'sum', 
                 geom = 'text', col = 'white', vjust = 1.5) +
    geom_segment(aes(x=xpos, y = End, xend=xpos, yend=Sum)) +
    geom_text(aes(x=xpos,y =  End-Diff/2, label=Percent),hjust=-0.2)

【讨论】:

  • 很好,如果段加入每个酒吧管理的中点会欣喜若狂:)
  • 不明白你的意思,试着把x和xend分别改成...=reorder(key, -Sum)
  • 这个方案信息量大,比这里的excel版本更好excel bar chart
  • 所以你的意思是stat_summary(aes(group=1), fun.y=sum, geom="line") 还是简单的geom_line(aes(group=1))
猜你喜欢
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
  • 2023-04-02
相关资源
最近更新 更多