0

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,

enter image description here

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"

  • 2
    Can you edit the question with the output of `dput(head(2nd_dataframe, n = 20))`? – Rui Barradas Feb 09 '23 at 11:57
  • 1
    Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Please do not post an image of code/data/errors [for these reasons](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). Just include the data directly using e.g. dput(head(NAME_OF_DATA)) or data.frame(...)). – stefan Feb 09 '23 at 11:59
  • 2
    I find it difficult to see how an aesthetic of `x=\`year<-\`(date, 2000)` could produce an x-axis with 2011 in the year. Am I missing something? – r2evans Feb 09 '23 at 13:42
  • 1
    Your date is likely not actually date. Try to `str(2nd_dataframe)` to see the format and try to use `as.Date()` to change it to class date. – jyr Feb 09 '23 at 14:14
  • Sorry, I hope this is better. – Janice Brahney Feb 12 '23 at 10:39
  • to give example of the data try to use the `dput` function it will give us a piece of code that we can run to get your data – jyr Feb 14 '23 at 07:00

0 Answers0