显示堆积条形图的总和

时间:2020-08-05 08:54:37

标签: r ggplot2 charts stacked-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")

enter image description here

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

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

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

1 个答案:

答案 0 :(得分:3)

每个堆叠栏的总数

如果您想将每列的总和相加,建议您事先计算然后提示。

# 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数据框,其中包含要提示的总数。

结果如下:

enter image description here

为每次交货添加小计

没什么大不同,您只需要添加小计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,因此总字体为粗体)

enter image description here

在内部添加每次交付的累计金额

几乎相同的技巧,但是之前汇总了tots数据。 我是通过在arrange上方进行mutatesummarise并在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") 

输出:

enter image description here

累计金额

我在像这样绘制之前计算了累计价格:

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,以确保您的金额以与情节相同的顺序累加。

结果如下:

enter image description here