I'm learning to use ggplot2
and am looking for the smallest ggplot2
code that reproduces the base::plot
result below. I've tried a few things and they all ended up being horrendously long, so I'm looking for the smallest expression and ideally would like to have the dates on the x-axis (which are not there in the plot
below).
df = data.frame(date = c(20121201, 20121220, 20130101, 20130115, 20130201),
val = c(10, 5, 8, 20, 4))
plot(cumsum(rowsum(df$val, df$date)), type = "l")
After a couple of years, I've settled on doing:
Try this:
Just remove
geom_point()
if you don't want it.Edit: Since you require to plot the data as such with x labels are dates, you can plot with
x=1:5
and usescale_x_discrete
to setlabels
a newdata.frame
. Takingdf
:Since you say you'll have more than 1
val
for "date", you can aggregate them first usingplyr
, for example.Then you can proceed with the same command by replacing
x = 1:5
withx = seq_len(nrow(dd))
.Jan Boyer seems to have found a more concise solution to this problem in this question, which I have shortened a bit and combined with the answers of Prradep, so as to provide a (hopefully) up-to-date-answer:
Note that
date
is not in Date-Format, butcharacter
, and thatvalue
is already grouped as suggested by Prradep in his answer above.