-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.opencat.R
More file actions
85 lines (64 loc) · 2.83 KB
/
plot.opencat.R
File metadata and controls
85 lines (64 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#opens a category to define what was spend inside it
plot.opencat<-function(f.category,
f.date.start=Sys.Date(),
f.date.stop=Sys.Date()){
##Coherce f.dates as a Date class object ----
f.date.start <- ymd(f.date.start)
f.date.stop <- ymd(f.date.stop)
##Input Error -----
if (f.date.stop < f.date.start) {
stop("Stop date prior to Start date. Please correct")
}
#define the dates -------------
#start date
day(f.date.start) <- 1#Define the initial start date to search
#stop date
day(f.date.stop) <- 1
month(f.date.stop) <-month(f.date.stop) + 1
day(f.date.stop) <-day(f.date.stop) - 1#point it to monthend
#ORGANIZE THE FILE----------------
#obtain the file
tempframe <- data
<<<<<<< HEAD:opencat.R
#turn negative amounts in positive
tempframe$amount <- abs(as.numeric(tempframe$amount))
=======
#turn negative values in positive
tempframe$value <- as.numeric(tempframe$value)
>>>>>>> refs/remotes/origin/master:plot.opencat.R
#Transform the date column in a date vector
tempframe$date <- as.Date(tempframe$date)
#create the specific tempframe for the answer
tempframe <- tempframe[tempframe$date >= f.date.start&
tempframe$date<=f.date.stop&
tempframe$category==f.category,]
#Prepare the output amounts
amounts.to.print <- tapply(tempframe$amount,
ymd(tempframe$date),
sum)
#sort values to print to be from newer to older
values.to.print<-values.to.print[length(values.to.print):1]
####Return of error --------------------
if (nrow(tempframe) == 0) {
stop("No information in the selected period")
}
#------------------
#return amounts
#print the plot ------------------
<<<<<<< HEAD:opencat.R
theplot <- barplot(amounts.to.print,
names.arg = names(amounts.to.print),
col=1:length(levels(tempframe$date)),
=======
theplot <- barplot(values.to.print,
names.arg = as.yearmon(as.Date(rownames(values.to.print))),
col=1:length(values.to.print),
>>>>>>> refs/remotes/origin/master:plot.opencat.R
ylab="Dollars",
xlab="Month",
main = paste("Total spent by month in",
as.character(f.category)))
#return(amounts.to.print)
return(data.frame(amount=paste("$",amounts.to.print),
row.names = names(amounts.to.print)))
}