双向条形图,两侧均带有正号ggplot2 [英] Bidirectional bar chart with positive labels on both sides ggplot2

查看:271
本文介绍了双向条形图,两侧均带有正号ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ggplot中创建双向条形图,其中轴标签和轴上方和下方的数据标签均为正.例如,如果您的数据是:

I'm try to create a bidirectional bar chart in ggplot where axis labels and data labels both above and below the axis are positive. So for example, if your data was:

myData <- data.frame(category = c("yes", "yes", "no", "no"), month = c('Jan', 'Feb', 'Jan', 'Feb'), values = c(6, 5, 4, 3))

我想要两列,一列表示一月,一列表示二月,其中"yes"值出现在带有正轴和数据标签的针形柱上,而"no"值指向下,也具有正轴和数据标签.它们之间将有一个"0"值栏.这在ggplots中可能吗?如果可以,那么如何实现呢?谢谢.

I'd want two columns, one for January and one for February, where the 'yes' values appeared pin bars pointing up with positive axis and data labels, and the 'no' values pointing down, also with positive axis and data labels. There would be a '0' value bar in between them. Is this possible in ggplots, and if so, how can it be accomplished? Thanks.

推荐答案

您是否正在寻找类似的东西?我们可以将"no"作为否定值传递,但是让ggplot将它们显示为正值.标签也一样.

Are you looking for something like this? We can pass the "no"s as negatives, but have ggplot display them as positive values. Same thing for the labels.

myData$values2 <- ifelse(myData$category == "no", -1 * myData$values, myData$values)

library(ggplot2)
ggplot(data = myData) + geom_bar(aes(x=month,y=values2,fill=category),stat="identity",position="identity") +
                        geom_text(aes(x=month,y=values2,label=abs(values2)),vjust = ifelse(myData$values2 >= 0, 0, 1)) +
                        scale_y_continuous(labels=abs)

这篇关于双向条形图,两侧均带有正号ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆