简体   繁体   English

R中的条形图以直观地可视化收入和支出

[英]barplot in R to intuitively visualize income and expenditure

year month income expense diff
2016 07 50 15 35
2016 08 30 70 -40

I have the data described above, and I want to draw a barplot, income and expense every months in group, left to right, side by side. 我有上述数据,我想每个月,从左到右,并排绘制一个图表,收入和支出。 For example, in July 2016, it has two vertical bars, between (2016-07,-15) and (2016-07, 35) for income, and between (2016-08, -40) and (2016-08,30) for expense. 例如,在2016年7月,它有两个竖线,收入介于(2016-07,-15)和(2016-07,35)之间,介于(2016-08,-40)和(2016-08,30之间) )的费用。 I managed to make a graph with R code below, which is different from what I really want. 我设法用下面的R代码制作了一个图形,这与我真正想要的有所不同。

year<- c(2016,2016)
month<-c(07,08)
income<-c(50,30)
expense<-c(15,70)
diff<-c(35,-40)
(dtt<-data.frame(YEAR=year, MONTH=month, INCOME=income, EXPENSE=expense, DIFF=diff))
dttm<-as.matrix(dtt)
barplot(t(dttm[,c(3,4)]), beside=TRUE)

在此处输入图片说明

I want to modify this plot like this.Any help will be appreciate. 我想像这样修改这个图,任何帮助将不胜感激。

在此处输入图片说明

Thanks for your attention. 感谢您的关注。 At first I want to add to my previous information, that is, for August 2016 , it has two vertical bars, between (2016-08, 0) and (2016-08, 30) for income, and between (2016-08, -40) and (2016-08,30) for expense. 首先,我想添加到我的先前信息中,即对于2016年8月,它有两个竖线,分别是(2016-08,0)和(2016-08,30)之间的收入,以及(2016-08, -40)和(2016-08,30)的费用。 And I drew a jpeg picture below which was the graph I wanted to draw with R commands. 我在下面画了一张jpeg图片,这是我想用R命令绘制的图形。 I hope it makes my presentation easier to be understood. 我希望它使我的演示文稿更容易​​理解。

在此处输入图片说明

x

month income_expense
1 2016-07             50
2 2016-07            -15
3 2016-08             30
4 2016-08            -75

x$id <- seq_along(x$income_expense)
x$type <- ifelse(x$income_expense > 0, "in",
    "out")
x[x$month %in% c("Starting Cash", "End Cash"),
    "type"] <- "net"
month_type <- paste(x$month, x$type, sep="-")
x.paste<-cbind(x, month_type)

for (i in 1:(length(x.paste)/2)) {
x.paste$start5[2*i-1] <- ifelse(abs(x.paste$income_expense[2*i-1]) > abs(x.paste$income_expense[2*i]) , x.paste$income_expense[2*i],
    0)
x.paste$end5[2*i-1] <- ifelse(abs(x.paste$income_expense[2*i-1]) > abs(x.paste$income_expense[2*i]) , x.paste$income_expense[2*i-1]+x.paste$income_expense[2*i],
    x.paste$income_expense[2*i-1])
x.paste$start5[2*i] <- ifelse(abs(x.paste$income_expense[2*i-1]) > abs(x.paste$income_expense[2*i]) ,x.paste$income_expense[2*i] ,
    x.paste$income_expense[2*i-1]+x.paste$income_expense[2*i])
x.paste$end5[2*i] <- ifelse(abs(x.paste$income_expense[2*i-1]) > abs(x.paste$income_expense[2*i]) , 0,
    x.paste$income_expense[2*i-1])
}

png("Waterfall_bs01.png", width = 500, height = 300, pointsize = 12, bg = "white")
library(ggplot2)
ggplot(x.paste, aes(month_type, fill = type)) + geom_rect(aes(x = month_type,
xmin = id - 0.45, xmax = id + 0.45, ymin = end5,
ymax = start5))

graphics.off()

Jimbou 金宝

I want to draw expense and income column by month. 我想按月绘制费用和收入列。 In order to do this, I move each vector vertically and then, pass it to geom_rect(). 为此,我垂直移动每个矢量,然后将其传递给geom_rect()。 Thanks anyway. 不管怎么说,还是要谢谢你。

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

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