Problems in plotting yearly data

65 views
Skip to first unread message

lily li

unread,
Dec 7, 2016, 12:05:36 PM12/7/16
to ggp...@googlegroups.com
Hi all,

I have some problems in plotting time series data. For example, I have several datasets, df1, df2, df3, etc., while each dataset has the same structure. Different datasets represent flow at different gauges.

df1
year   month   day   flow    time
2002    1           1       5      2002-01-01
2002    1           2       6      2002-01-02
2002    1           3       5.5   2002-01-03
...

df2
year   month   day   flow    time
2002    1           1      7.5    2002-01-01
2002    1           2      8       2002-01-02
2002    1           3      7.6    2002-01-03
...

I tried to plot daily time-series data, using the code:
p1 = ggplot()+ geom_line(data=df1,aes(x=time,y=flow,color='DF1') + geom_line(data=df2,aes(x=time,y=flow,color='DF2') + scale_color_manual('',limits=c('DF1', 'DF2'), values=c('red', 'blue'))
print(p1)

Now I want to plot annual values, but got no luck. Could anyone tell me the problem? Thanks.
df1.yr = aggregate(flow~year, data=df1, sum)
df2.yr = aggregate(flow~year, data=df2, sum)
...

p2 = ggplot()+ geom_line(data=df1.yr, aes(x=year,y=flow, color='DF1.yr')+ geom_line(data=df2.yr, aes(x=year,y=flow,color='DF2.yr')+ scale_color_manual('', limits=c('DF2.yr', 'DF2.yr'), values=c('red','blue'))
print(p2)

It has the error message:
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Error in data.frame(x =, y= function (save = 'default', status=0, :arguments imply differing number of rows:

Brandon Hurr

unread,
Dec 7, 2016, 12:49:30 PM12/7/16
to lily li, ggp...@googlegroups.com
I think you need to combine your datasets into one and then plot. 

see:
dplyr::bind_rows()

I would also suggest a mutate step to add a new column that defines the dataset that you can then color by later. 

df1 %>%
mutate(colorby = "DF1")
df2 %>%
mutate(colorby = "DF2")

If you have a lot of datasets, you might want to look into some other method that is more generic for creating a column that is the name of the dataframe/tibble. 

B


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

lily li

unread,
Dec 7, 2016, 12:57:28 PM12/7/16
to Brandon Hurr, ggp...@googlegroups.com
Hi Brandon,
Thanks for your reply. I can combine the datasets into one. I'm also wondering why it cannot plot the aggregated yearly data? I can plot daily data using the same code structure.

Brandon Hurr

unread,
Dec 7, 2016, 1:03:12 PM12/7/16
to lily li, ggp...@googlegroups.com
Could you dput() your dataset or an example one that could be aggregated using the code you supplied? A complete example would really help me understand what you want to do. 

B
Reply all
Reply to author
Forward
0 new messages