So I’m trying to plot some data over time using ggplot and managed to get everything to work properly, however the R session reset and now when I try to run it I receive an error that says “Error in as.character(x$label) :
cannot coerce type ‘closure’ to vector of type ‘character'”. As far as I’m aware everything is defined, but perhaps it is getting confused with functions in the lubridate package? some other ideas would be greatly appreciated!
The data I used is several thousand rows long so I cannot include the whole thing but the code I used is:
texel <- read.csv("TexelData.csv")
###Format data
obs <- texel %>% mutate(
Date = readr::parse_date(substr(texel$system.index, 1, 8), format = "%Y%m%d"),
Day = format(Date, "%d"),
Month = format(Date, "%m"),
Year = format(Date, "%y")
)
obs$cellID <- parse_factor(str_split_i(obs$system.index, "_", -1))
obs$Yday <- lubridate::yday(obs$Date)
###Take averages
d <- obs %>%
group_by(Date) %>%
summarise(across(c(NDVI, NDWI), mean, na.rm = TRUE))
d$Day <-day(d$Date)
d$Month <- month(d$Date)
d$Year <- year(d$Date)
d$Yday <- lubridate::yday(d$Date)
n_obs <- obs %>%
group_by(Date) %>%
summarise(length(cellID))
d$n_obs <- n_obs$'length(cellID)'
##Plot by yday
ggplot(data = d, mapping = aes(x = Yday, y = NDVI)) +
geom_point(data = d, mapping = aes(x = Yday, y = NDVI), color = 'gray90', size = 3) +
geom_point(data = d, mapping = aes(x = Yday, y = NDVI, color = Year), size = 3) +
theme_classic() + scale_color_viridis_c(direction = 1) + scale_shape() + labs(title = id)
##Plot for the whole period
ggplot(d, mapping = aes(x = Date, y = NDVI)) +
geom_point(aes(color = DoY), size = 3) +
geom_point(d, mapping = aes(y = NDVI, size = n_obs), color = 'orange', shape = 1) +
geom_smooth(d, mapping = aes(y = NDVI), method = "lm", color = 'orange') +
theme_classic() + scale_color_viridis_c(direction = 1) + scale_shape() + labs(title = id)
And for reference, the first few rows of the data frame d look like this:
Date NDVI NDWI Day Month Year Yday n_obs
<date> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <int>
1 2018-04-16 0.0573 -0.0579 16 4 2018 106 1999
2 2018-04-21 0.0132 -0.00252 21 4 2018 111 349
3 2018-04-26 0.0418 -0.0535 26 4 2018 116 6657
4 2018-05-06 0.0263 -0.0596 6 5 2018 126 170
5 2018-05-11 0.0573 -0.0877 11 5 2018 131 17734
6 2018-05-21 0.0598 -0.0992 21 5 2018 141 323
7 2018-05-26 0.0186 -0.0323 26 5 2018 146 17038
8 2018-05-31 -0.0165 0.0456 31 5 2018 151 753
9 2018-06-05 -0.0575 0.0787 5 6 2018 156 4847
10 2018-06-30 0.0394 -0.0527 30 6 2018 181 1321
Everything else seems to run fine, but it stops at:
##Plot by yday
ggplot(data = d, mapping = aes(x = Yday, y = NDVI)) + geom_point(data = d, mapping = aes(x = Yday, y = NDVI), …. [TRUNCATED]
Error in as.character(x$label) :
cannot coerce type ‘closure’ to vector of type ‘character’
Any ideas or suggestions would be greatly appreciated! 🙂