简体   繁体   English

用正值和负值标记堆积条形图

[英]Labelling stacked bar chart with positive and negative values

I am using ggplot2 to create a stacked bar chart with negative values, and trying to add sum of the parts as label on top of each bar.我正在使用 ggplot2 创建一个带有负值的堆积条形图,并尝试将各部分的总和添加为每个条形顶部的标签。 The code works fine for bars without negative values, but the label when a negative value is present stays inside the bar.该代码适用于没有负值的条,但存在负值时的标签保留在条内。

Example:例子:

test = c("Test1", "Test1", "Test1", "Test2", "Test2", "Test2", "Test3", "Test3", "Test3")  
student = c("A", "B", "C", "A", "B", "C", "A", "B", "C")  
value = c(5,5,5,3,3,3,-2,6,7)

dummy = data.frame(test, student, value)

g = ggplot(data=dummy, aes(x=student, y=value, fill=test)) +   
      geom_bar(stat="identity") +   
      scale_fill_manual(values=c("brown4", "steelblue", "goldenrod3")) +
      geom_text(aes(label=value), size =3, position=position_stack(vjust=0.5), colour="white") +
      theme_classic() + 
      theme(text=element_text(family="serif", size=15, colour="black")) +   
      theme(axis.title=element_text(family="serif", size=15, colour="black")) +   
      theme(legend.title = element_blank()) +   
      theme(legend.position = c(0.2, 0.7)) +
      stat_summary(fun.y = sum, aes(label = ..y.., group = student), geom = "text", vjust = -1) +
      scale_y_continuous(limits = c(-4,20))

g

The result is the following chart:结果如下图:

带有堆积条形和负值的图表 The sum for the bars without negative value works fine on top of the bar, but the sum for the bar with the negative value (Student A) is in the middle of the red bar.没有负值的条的总和在条的顶部工作正常,但具有负值的条(学生 A)的总和位于红色条的中间。

How can I fix this?我怎样才能解决这个问题?

You use fun.y = sum as the summary function, which adds all the y values in the group, including negative values.您使用fun.y = sum作为汇总函数,它将组中的所有y值相加,包括负值。 This gives the correct sum for the label, but a bad position.这给出了标签的正确总和,但位置不好。 For the position calculation, we want to calculate a sum of only the values greater than 0.对于位置计算,我们只想计算大于 0 的值的总和。

stat_summary let's us specify up to 3 functions, fun.y , fun.ymin , and fun.ymax . stat_summary让我们指定最多 3 个函数, fun.yfun.yminfun.ymax We'll modify fun.y , the position, to be the sum of positive values, and we'll add in a fun.ymax as the regular sum and use that for the label:我们将修改fun.y位置,使其成为正值的总和,我们将添加fun.ymax作为常规sum并将其用于标签:

g = ggplot(data=dummy, aes(x=student, y=value, fill=test)) +   
      geom_bar(stat="identity") +   
      scale_fill_manual(values=c("brown4", "steelblue", "goldenrod3")) +
      geom_text(aes(label=value), size =3, position=position_stack(vjust=0.5), colour="white") +
      theme_classic() + 
      theme(text=element_text(family="serif", size=15, colour="black")) +   
      theme(axis.title=element_text(family="serif", size=15, colour="black")) +   
      theme(legend.title = element_blank()) +   
      theme(legend.position = c(0.2, 0.7)) +
      stat_summary(fun.y = function(y) sum(y[y > 0]), fun.ymax = sum,
                   aes(label = ..ymax.., group = student), geom = "text", vjust = -1) +
      scale_y_continuous(limits = c(-4,20))
g

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM