Plotting multiple time-series in ggplot

前端 未结 1 617
清歌不尽
清歌不尽 2020-11-27 05:19

I have a time-series dataset consisting of 10 variables.

I would like to create a time-series plot, where each 10 variable is plotted in different colors, over time,

相关标签:
1条回答
  • 2020-11-27 06:06

    If your data is called df something like this:

    library(ggplot2)
    library(reshape2)
    meltdf <- melt(df,id="Year")
    ggplot(meltdf,aes(x=Year,y=value,colour=variable,group=variable)) + geom_line()
    

    enter image description here

    So basically in my code when I use aes() im telling it the x-axis is Year, the y-axis is value and then the colour/grouping is by the variable.

    The melt() function was to get your data in the format ggplot2 would like. One big column for year, etc.. which you then effectively split when you tell it to plot by separate lines for your variable.

    0 讨论(0)
提交回复
热议问题