【问题标题】:Showing sums for stacked bar chart显示堆积条形图的总和
【发布时间】:2020-08-05 08:54:37
【问题描述】:

我有以下数据框:

#>    region product delivery price
#> 1   Japan  laptop      yes   500
#> 2   Japan  laptop       no   400
#> 3   Japan printer      n/a   200
#> 4 America  laptop       no   600
#> 5 America  laptop      yes   620
#> 6 America  laptop      yes   300
#> 7 America printer      n/a   300
#> 8   China  laptop      yes   400

转载于此:

structure(list(region = c("Japan", "Japan", "Japan", "America", 
"America", "America", "America", "China"), product = c("laptop", 
"laptop", "printer", "laptop", "laptop", "laptop", "printer", 
"laptop"), delivery = c("yes", "no", "n/a", "no", "yes", "yes", 
"n/a", "yes"), price = c(500, 400, 200, 600, 620, 300, 300, 400
)), row.names = c(NA, -8L), class = c("tbl_df", "tbl", "data.frame"
))

下面的代码会生成一个条形图,比较每个地区的笔记本电脑和打印机的价格。笔记本电脑有交付选项,因此笔记本电脑列堆叠有“否”区域和“是”区域。

ggplot(df, aes(product, y = price, fill = delivery)) + 
  geom_col(position = "stack") + 
  facet_grid(.~region, switch = "x")

在它们各自的列中,我需要按地区显示笔记本电脑和打印机的总价格,对于笔记本电脑,我还想查看带和不带交付的笔记本电脑的不带价格。我试过了:

geom_text(aes(label = price), size = 3, hjust = 0.5, vjust = 3, position = "stack") 

但是,这只是在每列中显示所有单独的价格。

【问题讨论】:

  • 你能附上你的代码生成的图吗?
  • 完成,数据集也稍作更新

标签: r ggplot2 charts stacked-chart


【解决方案1】:

每个堆叠条的总和

如果要每列加总和,建议你先算好再提示。

# This calculates the totals per category
tots = df %>%
  group_by(region, product) %>%
  summarise(total=sum(price)) 

# This plots the results
df %>%
  ggplot(aes(product, y = price, fill = delivery)) + 
  geom_col(position = "stack") + 
  facet_grid(.~region, switch = "x") +
  geom_text(aes(product, total, fill= NULL, label = total), data=tots, size = 3, hjust = 0.5, vjust = 3, position = "stack") 

geom_text 使用包含要提示的总数的 tots 数据框。

结果如下:

添加每次交付的小计

没什么大的区别,你只需要添加一个小计 df 并同时提示:

tots = df %>%
  group_by(region, product) %>%
  summarise(total=sum(price)) 
stots = df %>%
  group_by(region, product, delivery) %>%
  summarise(stotal=sum(price)) %>% 
  arrange(desc(delivery))
df %>%
  ggplot(aes(product, y = price, fill = delivery)) + 
  geom_col(position = "stack") + 
  facet_grid(.~region, switch = "x") +
  geom_text(aes(product, stotal, fill= NULL, label = stotal), data=stots, size = 3, hjust = 0.5, vjust = 3, position = "stack") +
  geom_text(aes(product, total, fill= NULL, label = total, fontface=2), data=tots, size = 4, hjust = 0.5, vjust = -0.5, position = "stack") 

我做到了(并将字体更改为 2,因此总数为粗体)

在里面添加每次交付的累计金额

几乎相同的技巧,但之前聚合了 tots 数据。 我通过在summarise 之上执行arrangemutate 并在group_by 中精确传递来做到这一点。

tots2 = df %>%
  group_by(region, product, delivery) %>%
  summarise(total=sum(price)) %>% 
  arrange(desc(delivery)) %>% 
  mutate(cumtot=cumsum(total))
df %>%
  ggplot(aes(product, y = price, fill = delivery)) + 
  geom_col(position = "stack") + 
  facet_grid(.~region, switch = "x") +
  geom_text(aes(product, total, fill= NULL, label = cumtot), data=tots2, size = 3, hjust = 0.5, vjust = 3, position = "stack") 

输出:

累计金额

我在绘制之前计算了累积价格:

df %>%
  group_by(region, product) %>%
  arrange(desc(delivery)) %>% 
  mutate(cum_price = cumsum(price)) %>% 
  ggplot(aes(product, y = price, fill = delivery)) + 
  geom_col(position = "stack") + 
  facet_grid(.~region, switch = "x") +
  geom_text(aes(label = cum_price), size = 3, hjust = 0.5, vjust = 3, position = "stack") 

添加arrange是为了确保您的总和以与您的绘图相同的顺序累积。

结果如下:

【讨论】:

  • 感谢您的回答。问题是,我只想看到每个地区的一笔款项。因此,例如在第一列中,我只想在蓝色部分看到 920,在绿色部分看到 600,然后在条形上方总共看到 1520。
  • 对于在“累积”一词中引入歧义,我深表歉意,我已将其改写为“总和”
  • 不用担心,我编辑了答案以提示两种解决方案
  • 是否仍然可以获得各个堆栈的总和?即(笔记本电脑)920 表示是(没有组成部分 620),600 表示否,总共 1520?
  • 好的,我加了
猜你喜欢
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 2019-06-07
  • 2021-02-06
  • 2012-08-24
  • 2018-11-30
相关资源
最近更新 更多