Creating Raster Graphics in R

I am trying to create a raster chart (like a hovmoller chart) and was hoping someone could help. I looked for help with rasterVis and some others, but it seemed that I could not bring their examples into line with my data, which probably should somehow change, which eluded me. I managed to create a graph, but the fill values ​​for the cells do not match the original data. I copied the dput () file of an example of my data frame (hope this is the right way to do this). What I want is the days of the year (DOY) along the x axis with the y axis of 48 rectangles (hour column in DF) above each DOY. These rectangles will be half-hour intervals for each DOY and will be colored according to their value (qc-column in DF), which is 0.1 or 2

So far I have come up with the following code, but there seems to be a problem with distributing the z value (qc column) to a color, I think that for some reason the values ​​are not lining up correctly ..

mcol <- c("green","blue","red")
x=unique(DF[,"DOY"])
y=unique(DF[,"hour"])
z=matrix(DF[,"qc"],nrow=length(unique(DF[,"DOY"])),
                     ncol=length(unique(DF[,"hour"])))
image(x,y,z, col=mcol,
  xlab="Day of Year 2012", 
  ylab="Hour of day",
  main="Hovmoller plot of 2012 qc flags",
useRaster=TRUE)

What seems to be happening is that the matrix of fill values ​​(z) is applied first along the x axis (from left to right) and then loop up, while I need it to start in the lower left corner and go up, then loop left and right (hope this makes some sense!) My sample data here covers only three days, but the complete data set will be for a whole year (366 in 2012). Thanks in advance for any help,

John

structure(list(DOY = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L), hour = c(0.5, 1, 1.5, 2, 2.5, 3, 3.5, 
4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 
11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 
18, 18.5, 19, 19.5, 20, 20.5, 21, 21.5, 22, 22.5, 23, 23.5, 24, 
0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 
8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 
15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 20, 20.5, 21, 
21.5, 22, 22.5, 23, 23.5, 24, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 
4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 
12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 
18.5, 19, 19.5, 20, 20.5, 21, 21.5, 22, 22.5, 23, 23.5, 24), 
    qc = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 1L, 2L, 1L, 1L, 2L, 2L, 0L, 0L, 1L, 0L, 2L, 2L, 2L, 2L, 
    2L, 2L, 0L, 2L, 2L, 0L, 0L, 1L, 2L, 0L, 2L, 0L, 1L, 2L, 1L, 
    2L, 2L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 2L, 2L, 0L, 0L, 
    2L, 0L, 0L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L)), .Names = c("DOY", 
"hour", "qc"), class = "data.frame", row.names = c(NA, -144L))
+5
source share
3 answers

Close! Try something like this (x and y are generated by your code above):

library( sp )
r <- raster(nrows=length(y), ncols=length(x), xmn=min(x), xmx=max(x), ymn=min(y), ymx=max(y))
values(r) <- t(DF$qc)
spplot( r , cuts = 2 )

spplot sp

enter image description here

, geom_tile ( , ), :

p <-    ggplot( DF , aes(factor(DOY), hour ) ) + 
    geom_tile( aes(fill = factor(qc) ) , color = "#D9D9D9" ) + 
    scale_fill_brewer(name="QC", type = "div" , palette = "RdBu" )+
    scale_x_discrete( name = "Day" , expand = c(0,0) ) +
    scale_y_continuous( name = "Hour" , limits = c(0.5,24) , expand = c(0,0) , breaks = seq(0,24,2) )+
    coord_equal()
print(p)

enter image description here

+4

, , ...

library(ggplot2)
ggplot(DF,aes(DOY,hour,fill=qc))+geom_raster()

, , . , , , . .

+3

hovmoller rasterVis 4D (, ). , , .

lattice::levelplot.

:

tt <- seq(as.POSIXct('2013-01-01'), by='hour', length=8760)
vals <- 1:24 - 12.5
myDF <- data.frame(vals, tt)

, :

hour <- function(x)as.numeric(format(x, '%H'))
DoY <- function(x)as.numeric(format(x, '%j'))

Then we download the packages and define a theme with a consistent palette from the package RColorBrewer:

library(lattice)
library(latticeExtra)
myTheme <- custom.theme(region=brewer.pal(n=10, 'RdBu'))

And finally, we are ready to display the data:

levelplot(vals ~ DoY(tt)*hour(tt),
          data=myDF,
          xlab='Day', ylab='Hour',
          par.settings=myTheme)

levelplot result

For a more complete solution, you may be interested in the stripfunction from the metvurstpackage .

0
source

All Articles