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, on the same graph. The values should be on the Y axis and the dates on the X axis.
Click Here for dataset csv
This is the (probably wrong) code I have been using:
c.o<-read.csv(file="co.csv",head=TRUE) ggplot(c.o, aes(Year, a, b, c, d, e,f))+geom_line()
and here's what the output from the code looks like:
Can anyone point me in the right direction? I wasn't able to find anything in previous threads.
PROBLEM SOLVED, SEE BELOW.
One additional thing I would like to know:
Is it possible to add an extra line to the plot which represents the average of all variables across time, and have some smoothing below and above that line to represent individual variations?
Method 1: Using Basic R methods First, we create a data vector that has data for all the time series that have to be drawn. Then we plot the time series using the first dataset and plot() function. Then add other time series using line() function to the existing plot.
We can put multiple graphs in a single plot by setting some graphical parameters with the help of par() function. R programming has a lot of graphical parameters which control the way our graphs are displayed. The par() function helps us in setting or inquiring about these parameters.
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()
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With