Error in `check_aesthetics()`: ! Aesthetics must be either length 1 or the same as the data (10253): fill

pacman::p_load( "exactextractr","raster","ggplot2","unikn","mapview",
"gridExtra","rgdal","fields",
"RColorBrewer","ncdf4","rasterVis", "moments", "tictoc","rgeos",
"snow","future.apply","sp","tigris","sf","tidyr","rcartocolor","parallel","snow","future.apply")

require(maptools)
data(wrld_simpl)

nc = nc_open("C:/Users/tamis/Downloads/FDSI.nc")
time_var= ncvar_get(nc,'Time')
Latitude= ncvar_get(nc,'Latitude')
Longitude= ncvar_get(nc,'Longitude')
FDSI= ncvar_get(nc,'FDSI')
nc_close(nc)
FDSIbrk= brick("C:/Users/tamis/Downloads/FDSI.nc")

IPCC_shp = rgdal::readOGR("C:/Users/tamis/Downloads/SampleData-master/SampleData-master/CMIP_land/CMIP_land.shp",verbose = FALSE)
head(IPCC_shp@data)

global_crop1 = crop(coastlines,extent(IPCC_shp))

raster::plot(IPCC_shp, add=TRUE) # Add IPCC land regions in blue color
raster::plot(coastlines, add=TRUE) # Add global coastline

#~ Extract feature data as data frame

In order to make the operation faster we have to extract the desired layers.

Subset the raster data to the dates of interest

nl=nlayers(FDSIbrk)

start_date <- which(getZ(FDSIbrk) == '20210821')
end_date <- which(getZ(FDSIbrk) == '20210822')
FDSI_brk_sub <- FDSIbrk[[start_date:end_date]]

featureData = exact_extract(FDSIbrk, # r brick
IPCC_shp, # Convert shapefile to sf (simple feature)
force_df = FALSE, # Output as a data.frame
include_xy = FALSE, # Incluse cell lat-long in output?
fun = 'mean', # Specify any function to apply for each feature extracted data
progress = TRUE) # Progressbar

names(featureData)

Plot mean fdsi map for IPCC

#create a folder where it can be save.
setwd("G:/.shortcut-targets-by-id/1_munBiOkEi1EyelAxqX1FaQ56WRqt47Z/FLASH_shared/Ashely Johnson")
dir.create("Img_Assignment_2")
setwd("G:/.shortcut-targets-by-id/1_munBiOkEi1EyelAxqX1FaQ56WRqt47Z/FLASH_shared/Ashely Johnson/Img_Assignment_2")

Create the preffered colors.

mypal= brewer.pal(11,"Spectral") # Max colors available in the palette=11
mypal= colorRampPalette(mypal)(20) # Expand color palette to 20plot(venshp)

for(i in 2678:nlayers(FDSI_brk_sub)) {

mean_map=ggplot() +
geom_sf(data = (IPCC_shp), # CONUS shp as sf object (simple feature)
aes(fill = featureData[[i]])) + # Plot fill color= mean soil moisture
scale_fill_gradientn(colors=rev(mypal), # Use user-defined colormap
name = "Mean FDSI", # Name of the colorbar
na.value = "transparent", # transparent NA cells
labels = (c("0", "0.2", "0.4", "0.6", "0.8")), # Labels of colorbar
breaks = seq(0,0.8,by =0.2), # Set breaks of colorbar
limits = c(0,0.8))+ # To invert color, use -1
coord_sf(crs = 2163)+ # Reprojecting polygon 4326 or 3083
theme_void() + # Plot theme. Try: theme_bw
theme(legend.position = c(0.2, 0.1),
legend.direction = "horizontal",
legend.key.width = unit(5, "mm"),
legend.key.height = unit(4, "mm")) + ggtitle(names(featureData[i]))

Saving the plot to disk:

ggsave(plot = mean_map, # ggplot object to be exported
filename =paste(names(featureData[i]), "png", sep="."), # Export file name
bg = "white", # Background color
units="in", dpi=300, # Export dim unit & Dots per inch (resolution)
width=10, height=7) # Height, width
}

mean_map

Hello.
Thanks for providing code , but you could take further steps to make it more convenient for other forum users to help you.

Share some representative data that will enable your code to run and show the problematic behaviour.

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

I also note that you can omit code that is a prelude to setting up your problem, by just providing a representation of the object(s) they resulted in, if this is more convenient, this should be assessed on a case by case basis. Certainly if your issue relates to inability to plot; there doesnt seem to be a reason to include further code after that to save the plot, as presumably this isnt part of your problem requiring of a solution.

Try to reduce what you share as much as possible, while making it runnable by forum users (as per the guide) .

Good luck !

would I do this by saving my workspace and uploading from there on

Did you read the guide ?

head(featureData)
mean.X20150331 mean.X20150401 mean.X20150402 mean.X20150403 mean.X20150404
mean.X20150405 mean.X20150406 mean.X20150407 mean.X20150408 mean.X20150409
mean.X20150410 mean.X20150411 mean.X20150412 mean.X20150413 mean.X20150414

The guide does not say to do that ...
Is there a part of the guide that is unclear ?

yes as the guide is a step by step however my data set when following the guide doesn't reflect the averages for each specific date

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.