[R] Summing up layers

maettuw at students.unibe.ch maettuw at students.unibe.ch
Tue Jul 19 14:27:29 CEST 2016


Hello everyone

I have gridded output data from a climate model. I would like to take the average of these grids and plot them. I have tried to plot it with gglot or image.plot (field/raster package). I have to do this extensively meaning that a manual way is not what I am looking for. My data has three dimensions. Longitude,Latitude and the variable itself. Below is a code that works perfectly fine. This is a plot for one single year.

H3 <- ((detrended_TOA_rad[,,28:28]))
image.plot(lon,lat,H3,col=tim.colors())

setwd("/scratch/maettu/maettu/data/fup013/echam5/net_Radiation")
spatial_TOA_net_Rad <- open.nc("spatial_yearmean_TS_TOA_net_radiation.nc")
TOA_net_rad <- read.nc(spatial_TOA_net_Rad)
detrended_TOA_rad <- TOA_net_rad$ttr
lon_TOA <- TOA_net_rad$lon
lat_TOA <- TOA_net_rad$lat


Down here I have tried to make a plot over several years, which did not work!!

H3 <- ((detrended_TOA_rad[,,28:38]))
image.plot(lon,lat,H3,col=tim.colors())

-->

Error in seq.default(zlim[1], zlim[2], , nlevel) :
  'from' cannot be NA, NaN or infinite

I googled that error message but didnt get smart out of it.
The ggplot way worked better but the problem is that I cannot create a dataset that contains the coordinates as well as the values. Therefore the projection is not correct.

melted1 <- melt(H3)

ggplot(aes(x = Var1, y = Var2, fill = value), data = melted1) +geom_tile() +geom_tile() + scale_fill_distiller(palette = "Set1") +
  geom_polygon(data=map,aes(x=long, y=lat, group=group), colour="black", fill="transparent")

I have also tried to aggregate with the "overlay" function from the raster packege. However I cannot coerce my array files into a raster...
So the main problem is that I cannot aggrregate the layers.

Appreciate the help! Matthias

	[[alternative HTML version deleted]]



More information about the R-help mailing list