Pad
Pad

Reputation: 911

Plotting monthly and annual data on the same graph using r

I've plotted a dataframe containing multiple monthly variables of data (example data below) - this data is plotted monthly so there are 12 rows to the dataframe. I want to plot annual data on the same graph as daily dots on top of the monthly lines, but can't figure it out.

library(ggplot2)
#generate monthly data
df <- data.frame(Month= seq(as.Date("2004/1/1"), by='month', length.out=12),
             data = (seq(1:12)), 
             data2 = ((12:1)))

#melt the data together
samplemelt <- melt(df, id.vars='Month', variable.name ='Methods')

#create daily data to plot also
SampleData=data.frame(day=seq(as.Date("2004/1/1"), by='day', length.out=365),
                          data=rnorm(n=365,mean=6,sd=2))

#plot the data             
ggplot(samplemelt, aes(Month, value))+
  geom_line(aes(colour=Methods), size=1)

This plots the two lines, but I want to add the daily values as dots to the same plot. I've tried

ggplot(samplemelt, aes(Month, value))+
geom_line(aes(colour=Methods), size=1)+
geom_point(data=SampleData$data)

but this just gives an error Error: ggplot2 doesn't know how to deal with data of class numeric

Not sure if this is possible or if I'm missing something very obvious. Please help!

Upvotes: 0

Views: 2008

Answers (1)

Pad
Pad

Reputation: 911

As @AntoniosK pointed out the answer is to use:

ggplot()+ geom_line(data = samplemelt, aes(Month, value, colour=Methods), size=1)+ 
geom_point(data = SampleData, aes(day, data))

Upvotes: 1

Related Questions