Thursday, April 25, 2013

RG#89: Raster in R


 require(grid)
grid.raster(matrix(colors()[1:600], ncol=20))





set.seed(1234)

mt <- matrix (sample(c("red","red1", "yellow", "purple", "green1", "green4", "blue"), 10000, replace = TRUE), ncol = 100)

grid.raster(mt)


 
rgb.palette <- colorRampPalette(c("red", "orange", "blue"),
                                space = "rgb") 
 
bg.palette <- colorRampPalette(c("blue", "green"), space = "rgb")

gr.palette <-  colorRampPalette(c("green", "red"),
                                space = "rgb")
 
 
colrs <- matrix(c(rgb.palette(20),bg.palette(20),gr.palette(20)),   nrow=6, ncol=10)
grid.newpage()
 grid.raster(colrs)
  grid.raster(colrs, interpolate=FALSE)
# raster in ggplot2
require(ggplot2)

# Generate data
funp <- function (n,r=2) {
 xv <- seq(-r*pi, r*pi, len=n)
 df1 <- expand.grid(x=xv, y=xv)
 df1$r <- sqrt(df1$x^2 + df1$y^2)
 df1$z <- cos(df1$r^2)*exp(-df1$r/6)
 df1
}
qplot(x, y, data = funp(1000), fill = z, geom = "raster") + theme_bw()










No comments:

Post a Comment

Note: Only a member of this blog may post a comment.