data visualization - Drawing a moving average-style line representing the amount of quota left (R graph drawing) -


2nd edit realise revised code close, not quite there; should reasonably calculate remaining daily average where have data, not continually increase until end of month (i.e. should horizontal after 20th).

edit: i've worked out how include "daily average remaining" (after googling found "within"). i'm struggling getting line draw. new code:

library("ggplot2") library("sitools")  host=c("red", "blue", "green") finish=as.date("2015-04-30") start=as.date("2015-04-01") date=rep(seq(start, finish, "days"), each=3) bytes=c(sample(1e7:2e8, 60), rep(0, 30)) download = data.frame(bytes, date, host) download=within(download, days_remain <- as.numeric((finish - date), units="days")) download=within(download, avg_remain <- ((8e9 - cumsum(bytes))/days_remain)) ggplot(download, aes(x = date, y = bytes, fill = host)) + plot(download$date, download$avg_remain) + geom_area() + scale_fill_brewer(palette="paired") +  scale_y_continuous("download", labels=f2si) 

i'll leave below in context:

i'm drawing graph of usage of isp's monthly download quota, categorized host uses it. i've got part working quite well. i'm struggling include line shows average download available per day calculated subtracting total usage.

i think similar moving average graph, won't related. don't know of correct term search for, nor can make code work because i'm not enough @ r.

here's example shows random data, grouped 3 hosts (red, green, , blue), month of april 2015. assumes data stops on 20th. there's horizontal line plotted @ magic number represents average allowance assuming constant usage. want line move based on cumulative usage.

library("ggplot2") library("sitools")  host=c("red", "blue", "green") date=rep(seq(as.date("2015-04-01"), as.date("2015-04-30"), "days"), each=3) bytes=c(sample(1e7:5e7, 60), rep(0, 30)) download = data.frame(bytes, date, host)  ggplot(download, aes(x = date, y = bytes, fill = host)) + geom_hline(yintercept=285000000) + geom_area() + scale_fill_brewer(palette="paired") +  scale_y_continuous("download", labels=f2si) 

ideally, black line should change based on whether we've gone on or under our "daily average limit", giving easy view of how quota per day we've got left.

i've tried creating for() loop calculates total cumulative usage, subtracting total quota (8 gibytes), couldn't work out how put vector(?) ggplot and have plotted magic black line on same graph.

any ideas how can "daily limit" line on graph?

@andy-w thanks. think misunderstood wanted, that's poor explanation. code allowed me wanted, because yours worked!

library("ggplot2")  host=c("red", "blue", "green") finish=as.date("2015-04-30") start=as.date("2015-04-01") date=rep(seq(start, finish, "days"), each=3) bytes=c(sample(1e7:2e8, 60), rep(0, 30)) download = data.frame(bytes, date, host) avg_remain=aggregate(bytes ~ date, download, sum) avg_remain=within(avg_remain,  days_remain <- as.numeric((finish - date), units="days")) avg_remain=within(avg_remain, per_day <- (8e9 - cumsum(bytes))/days_remain)  p = ggplot(download, aes(x = date, y = bytes)) + geom_area(aes(fill=host)) +      scale_fill_brewer(palette="paired") +  scale_y_continuous("download") p2 = p + geom_line(data=avg_remain, aes(x=date, y = per_day)) 

gives:

enter image description here

again, much.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -