I'm trying to plot multiple years of monthly data in one plot using this code:
TmpData<-read.csv('BeaverDay.csv', header=TRUE)
TmpData$Date <- as.Date(with(TmpData, paste(Year, Month, Day, sep="-" )),"%Y-%m-%d")
dat<-data.frame(Var=TmpData$CaMgDTE , date=TmpData$Date)
ggplot(dat, aes(`year<-`(date, 2011), Var, col = factor(year))) +
geom_line() +
labs(x = "", col = "year")
It works beautifully (sometimes) for one file,
but not for any other file that is formatted the same way. I cannot figure out what is different between the files or runs.
I keep getting this error:
aesthetics must be either length 1 or the same as the data(xxx)
and this error:
> Error in `geom_line()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `unique.default()`:
! unique() applies only to vectors
For one file, it just gives a warning about removing the NAs, but still creates the figure. So I don't think NAs are the problem.
Here is a snippet of the data that works
data.frame(head(TmpData)) Day Month Year DateCode JulianD CaNa AgTUg Al_Diss_Mg Al_T_Mg AsExMg 1 17 11 1987 32098 322 16.12 NA NA NA NA 2 1 12 1987 32112 336 16.12 NA NA NA NA 3 15 12 1987 32126 350 15.05 NA NA NA NA 4 29 12 1987 32140 364 16.28 NA NA NA NA 5 12 1 1988 32154 12 15.21 NA NA NA NA 6 26 1 1988 32168 26 14.68 NA NA NA NA
Here is a snippet of the data that doesn't
data.frame(head(TmpData)) Day Month Year DateCode JulianD AgTUg Al_Diss_Mg Al_T_Mg AsExMg AsTMg 1 5 6 1981 29742 157 NA NA NA NA NA
2 12 10 1981 29871 286 NA NA NA NA NA
3 18 2 1982 30000 49 NA NA NA NA NA
4 21 6 1982 30123 173 NA NA NA NA NA
5 25 6 1982 30127 177 NA NA NA NA NA
6 2 7 1982 30134 184 NA NA NA NA NA
(note the variable I am plotting is not shown as the files contain >100 columns)
I've tried changing the plotting year (date, 1988)
I've tried removing NA's but, as stated above NAs are not a problem for one of the files
I've checked the class of the Date column and it is a "Date"