[R-sig-Geo] Changing axes of a plot
Robert J. Hijmans
r.hijmans at gmail.com
Mon Nov 22 17:43:28 CET 2010
Ogbos,
You can do this:
library(raster)
xy<-cbind(x, y)
r <- rasterize(xy, raster(nrow=120, ncol=60), fun=sum)
rb <- rev(rainbow(255,s=1,v=1,start=0,end=7/12,gamma=1,alpha=1))
image(r, col=rb, ylab = "Longitude", xlab="Latitude" )
Or, in your code, instead of this
z = t(grid3)
this might work:
z = <- t(grid3[nrow(grid3):1,])
Robert
On Sun, Nov 21, 2010 at 10:51 PM, ogbos okike <ogbos.okike at gmail.com> wrote:
> Dear all,
> I tried to use the code below to generate 3 X 3 grid of latitude-longitude
> data. I use image to view the grid. However, latitude and longitude are on
> the wrong axes. Thus, I want to swap the axes. I tried to swap x and y in:
> image(x, ..) that did not help. I also wish to add color code to the plot.
> Thanks for kind help.
>
> portion of the data:
> -3.3851 -65.3236
> -3.2696 -65.2364
> -3.2679 -65.2349
> -17.6404 164.7025
> -4.8285 148.8214
> 2.6477 -67.6568
> -0.2350 -73.4833
> -16.8655 40.9587
> 8.1179 -61.6474
> -0.2755 93.3401
> -17.1733 119.9011
> 1.1245 -69.7640
> -20.0035 -149.3088
> -3.4200 177.8753
> 3.0133 -67.5590
> 15.4684 -21.9331
> -17.6166 120.2656
> -17.3888 165.9368
> -17.6953 164.7335
> -1.8623 -74.0017
> -3.3562 -71.1195
> -11.5775 130.1496
>
> code:
> xy<-matrix(c(x, y), ncol=2)
>
> # 3 deg grid for counts
> grid3 <- matrix(0, nrow=120, ncol=60)
>
> # accumulate counts of points in cells
> for (i in 1:nrow(xy)) {
> if (xy[i, 1] == 180)
> ix = nrow(grid3)
> else
> ix <- floor((xy[i,1] + 180) / 3) + 1
>
> if (xy[i, 2] == 90)
> iy = ncol(grid3)
> else
> iy <- floor((xy[i,2] + 90) / 3) + 1
>
> # catch any subscript errors
> tryCatch(
> grid3[ix, iy] <- grid3[ix, iy] + 1,
> error = function(e) cat("Ignoring point out of bounds:", xy[i,], "\n") )
> }
> rb<-rev(rainbow(255,s=1,v=1,start=0,end=7/12,gamma=1,alpha=1))
> image( x = seq(-88.5, 88.5, 3), y = seq(-178.5, 178.5, 3), z = t(grid3),
> col=rb, ylab = "Longitude", xlab="Latitude" )
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
More information about the R-sig-Geo
mailing list