显示堆叠条形图的总和

Showing sums for stacked bar chart

我有以下数据框:

#>    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")

在各自的专栏中,我需要按地区显示笔记本电脑和打印机的总价,对于笔记本电脑,我还想查看带和不带送货的笔记本电脑的dum 价格。我试过:

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

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

每个堆积条的总和

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

# 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 以确保您的总和按照与绘图相同的顺序累积。

结果如下: