[R-sig-Geo] Creating multipanel levelplot with common color legend

Pascal Oettli kridox at ymail.com
Thu Feb 14 07:45:43 CET 2013


One possibility.

I also include the list, for someone to do better job.


age2100 <- read.csv("testinglattice.csv",header=T,sep=",")
colnames(age2100) <- c('Eastings','Northings','Salinity','Time')

sub.basesal <- subset(age2100, Time=='basesal')
tmp <- with(sub.basesal, akima::interp(Eastings, Northings, Salinity, 
xo=seq(min(Eastings),max(Eastings), length=100),
 
yo=seq(min(Northings),max(Northings), length=100)))
sub.basesal <- expand.grid(Eastings=tmp$x,Northings=tmp$y)
sub.basesal$Salinity <- as.vector(tmp$z)
sub.basesal$Time <- factor('basesal')
rm(tmp)

sub.sal2050 <- subset(age2100, Time=='sal2050')
tmp <- with(sub.sal2050, akima::interp(Eastings, Northings, Salinity, 
xo=seq(min(Eastings),max(Eastings), length=100),
 
yo=seq(min(Northings),max(Northings), length=100)))
sub.sal2050 <- expand.grid(Eastings=tmp$x,Northings=tmp$y)
sub.sal2050$Salinity <- as.vector(tmp$z)
sub.sal2050$Time <- factor('sal2050')
rm(tmp)

sub.sal2100 <- subset(age2100, Time=='sal2100')
tmp <- with(sub.sal2100, akima::interp(Eastings, Northings, Salinity, 
xo=seq(min(Eastings),max(Eastings), length=100),
 
yo=seq(min(Northings),max(Northings), length=100)))
sub.sal2100 <- expand.grid(Eastings=tmp$x,Northings=tmp$y)
sub.sal2100$Salinity <- as.vector(tmp$z)
sub.sal2100$Time <- factor('sal2100')
rm(tmp)

age2100.akima <- rbind(sub.basesal, sub.sal2050, sub.sal2100)


   levelplot(Salinity ~ Eastings+Northings | Time, age2100,
             panel=function(x,y,subset,...)
             {
               panel.levelplot.points(x,y,subset,...)
             },
             aspect = "iso", scales = list(draw=FALSE),
             cuts=40,smooth=T,
             colorkey = list(space = "right"),
             col.regions = rainbow(100, start = 3/15, end = 1),
             xlim=c(435000,468900),ylim=c(3337500,3374500),
             autokey=list(title="An image"),
             main="",
             col.symbol=0) +
   contourplot(Salinity ~ Eastings+Northings | Time, age2100.akima, cuts=40)

Pascal


Le 14/02/2013 11:59, Janesh Devkota a écrit :
> Hi Pascal,
>
> Thank you so much. I have been stuck on this for more than 4 hours. I am
> glad I asked it here. One quick question here: is it possible add
> contour line on such type of data ?
>
> Thank you
>
> Best Regards,
> Janesh
>
>
> On Wed, Feb 13, 2013 at 8:49 PM, Pascal Oettli <kridox at ymail.com
> <mailto:kridox at ymail.com>> wrote:
>
>     Hi,
>
>     This should work
>
>
>     age2100 <- read.csv("testinglattice.csv",__header=T,sep=",")
>     colnames(age2100) <- c('Eastings','Northings','__Salinity','Time')
>
>     levelplot(Salinity ~ Eastings+Northings | Time, age2100,
>
>                 panel=function(x,y,subset,...)
>                 {
>                   panel.levelplot.points(x,y,__subset,...)
>                 },
>                 aspect = "iso", scales = list(draw=FALSE),
>                 cuts=40,smooth=T,
>                 colorkey = list(space = "right"),
>                 col.regions = rainbow(100, start = 3/15, end = 1),
>                 xlim=c(435000,468900),ylim=c(__3337500,3374500),
>                 autokey=list(title="An image"),
>                 main="",
>                 col.symbol=0)
>
>
>
>     Le 14/02/2013 11:36, Janesh Devkota a écrit :
>
>         Hi Pascal,
>
>         This is a salinity data at three different times. Actually I have
>         combined three data sets into one and have given the type to
>         each data
>         set. So, for each data set I have x, y and z which represent
>         latitude,
>         longitude and salinity for that location. For each times, x and
>         y are
>         same and only variables that vary are z and the type. Type here is a
>         variable that shows that these data are measured at different
>         times and
>         I want to make panel for each type. First type is "basesal",
>         second type
>         is "sal2050" and third type is "sal2100". I want to make panels
>         for each
>         type.
>
>         I combined my three data sets into one and I sent you the
>         combined data
>         set.
>
>         Does this explanation make sense ? Please let me know if you are
>         still
>         confused.
>
>         Thank you so much for your time.
>
>         Best Regards,
>         Janesh
>
>
>         On Wed, Feb 13, 2013 at 8:30 PM, Pascal Oettli <kridox at ymail.com
>         <mailto:kridox at ymail.com>
>         <mailto:kridox at ymail.com <mailto:kridox at ymail.com>>> wrote:
>
>              Hi,
>
>              I don't understand on which criterion you can base your
>         multipanel plot.
>
>              Pascal
>
>              Le 14/02/2013 11:23, Janesh Devkota a écrit :
>
>                  Hi Pascal, thank you so much for that nice example.
>         Actually, my
>                  data is
>                  not continuous data. My data is discrete data. The code
>         I have
>                  used so
>                  far is as follows and I have also attached a link to my
>         data. I have
>                  shown two options: one without any panels and one with
>                  multipanels. For
>                  single graph it is working fine but for multipanel it
>         throws an
>                  error
>                  "Error in limits.and.aspect(default.____prepanel,
>         prepanel = prepanel,
>
>                  have.xlim = have.xlim,  :  need at least one panel"
>
>                  The data testinglattice.csv can be found on
>         https://www.dropbox.com/s/____rq7e5vhaqard405/____testinglattice.csv
>         <https://www.dropbox.com/s/__rq7e5vhaqard405/__testinglattice.csv>
>
>
>         <https://www.dropbox.com/s/__rq7e5vhaqard405/__testinglattice.csv <https://www.dropbox.com/s/rq7e5vhaqard405/testinglattice.csv>>
>
>                  library(latticeExtra)
>                  library(sp)
>                  library(rgdal)
>                  library(lattice)
>                  library(gridExtra)
>                  getwd()
>
>                  age2100<-
>         read.csv("testinglattice.csv",____header=T,sep=",")
>
>
>                  ## Without type and layout parameter
>                  levelplot(age2100$z~age2100$x+____age2100$y,
>         as.data.frame(age2100),
>                              panel=function(x,y,subset,...)
>                              {
>                                panel.levelplot.points(x,y,____subset,...)
>
>                              },
>                              aspect = "iso", scales = list(draw=FALSE),
>                  xlab="Eastings",
>                  ylab = "Northings",
>                              cuts=40,smooth=T,
>                              colorkey = list(space = "right"),
>                              col.regions = rainbow(100, start = 3/15,
>         end = 1),
>
>         xlim=c(435000,468900),ylim=c(____3337500,3374500),
>                              autokey=list(title="An image"),
>                              main="",
>                              col.symbol=0)
>
>                  ## With type and layout parameter
>
>
>         levelplot(age2100$z~age2100$x+____age2100$y|age2100$type,__layout=__c(2,2),
>                  age2100,
>                              panel=function(x,y,subset,...)
>                              {
>                                panel.levelplot.points(x,y,____subset,...)
>
>                              },
>                              aspect = "iso", scales = list(draw=FALSE),
>                  xlab="Eastings",
>                  ylab = "Northings",
>                              cuts=40,smooth=T,
>                              colorkey = list(space = "right"),
>                              col.regions = rainbow(100, start = 3/15,
>         end = 1),
>
>         xlim=c(435000,468900),ylim=c(____3337500,3374500),
>                              autokey=list(title="An image"),
>                              main="",
>                              col.symbol=0)
>
>                  I added the parameter type to define for different panels.
>
>                     Would you please let me know what can I do to make it
>                  multipanel ? BTW
>                  your example works perfect.
>
>                  Thanks.
>
>                  Janesh
>
>
>                  On Wed, Feb 13, 2013 at 7:27 PM, Pascal Oettli
>         <kridox at ymail.com <mailto:kridox at ymail.com>
>                  <mailto:kridox at ymail.com <mailto:kridox at ymail.com>>
>                  <mailto:kridox at ymail.com <mailto:kridox at ymail.com>
>         <mailto:kridox at ymail.com <mailto:kridox at ymail.com>>>> wrote:
>
>                       Hello,
>
>                       In your data frame, you need a column to
>         categorize your
>                  data 'z'.
>
>                       grid1 <- expand.grid(x=1:10, y=1:10,
>                       w=c('Cat.1','Cat.2','Cat.3','______Cat.4'))
>
>
>
>                       grid1$z <- round(rnorm(400,50,25),0)
>
>                       levelplot(z ~ x+y | w, grid1, layout=c(2,2))
>
>                       If it does't match what you need, it is
>         complicated to go
>                  further
>                       without knowing the structure of your data.
>
>                       HTH,
>                       Pascal
>
>
>                       Le 14/02/2013 10:13, Janesh Devkota a écrit :
>
>                           Hello Pascal,
>
>                           Thank you for your reply. But I couldn't find
>         how can I
>                  use the same
>                           concept to my data.
>
>                           The code I have used is as follows:
>
>                           levelplot(age2100$z~age2100$x+______age2100$y,
>                  as.data.frame(age2100),
>                                       panel=function(x,y,subset,...)
>                                       {
>
>           panel.levelplot.points(x,y,______subset,...)
>
>
>                                         sp.polygons(hello)
>                                       },
>                                       aspect = "iso", scales =
>         list(draw=FALSE),
>                           xlab="Eastings",
>                           ylab = "Northings",
>                                       cuts=40,smooth=T,
>                                       colorkey = list(space = "right"),
>                                       col.regions = rainbow(100, start =
>         3/15,
>                  end = 1),
>
>                  xlim=c(435000,468900),ylim=c(______3337500,3374500),
>
>
>                                       autokey=list(title="An image"),
>                                       main="",
>                                       col.symbol=0)
>
>                           where age2100 is a dataset with x, y and z
>         coordinates.
>                  and I
>                           have added
>                           a polygon here.
>
>                           I found it very difficult to relate with that
>         example.
>
>                           Another trial I did was using
>                           grid.arrange(p1, p1, p1,ncol=2)
>
>                           This way I got the three plots not sharing the
>         same
>                  legend. Can
>                           anyone
>                           suggest with the code I provided ?
>
>                           Janesh
>
>
>                           On Wed, Feb 13, 2013 at 6:19 PM, Pascal Oettli
>                  <kridox at ymail.com <mailto:kridox at ymail.com>
>         <mailto:kridox at ymail.com <mailto:kridox at ymail.com>>
>                           <mailto:kridox at ymail.com
>         <mailto:kridox at ymail.com> <mailto:kridox at ymail.com
>         <mailto:kridox at ymail.com>>>
>                           <mailto:kridox at ymail.com
>         <mailto:kridox at ymail.com> <mailto:kridox at ymail.com
>         <mailto:kridox at ymail.com>>
>                  <mailto:kridox at ymail.com <mailto:kridox at ymail.com>
>         <mailto:kridox at ymail.com <mailto:kridox at ymail.com>>>>> wrote:
>
>                                Hello,
>
>                                Please have a look at the 2nd example
>         given by
>                  ?levelplot
>                           ("lattice"
>                                package).
>
>                                It uses a data frame.
>
>                                HTH,
>                                Pascal
>
>
>                                Le 14/02/2013 08:46, Janesh Devkota a écrit :
>
>                                    Hello All,
>
>                                    I have created a levelplot and I want
>         to use
>                  multipanel
>                                    levelplot with same
>                                    common color legend.
>
>                                    Lets say I have a plot with name p1.
>                                    I tried some methods such as:
>
>                                    print(p1, split=c(1,1,1,2), more=TRUE)
>                                    print(p1, split=c(1,2,1,2))
>
>                                    I couldn't control the look and feel
>         of the
>                  plot with
>                           that function.
>
>                                    I tried another method:
>
>                                    print(p1, position=c(0, .5, 1, 1),
>         more=TRUE)
>                                    print(p1, position=c(0, 0, 1, .5))
>
>                                    These methods would just make two
>         columns of
>                  the plot
>                           at center.
>
>                                    I think I need to use the trellis
>         function but I
>                           couldn't figure
>                                    out. Can
>                                    anyone show an example of using
>         multipanel
>                  levelplot
>                           with the
>                                    common legend
>                                    ?
>
>                                    Thanks.
>
>                                    Janesh
>
>                                             [[alternative HTML version
>         deleted]]
>
>
>                    _______________________________________________________
>
>
>                                    R-sig-Geo mailing list
>         R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>         <mailto:R-sig-Geo at r-project.__org <mailto:R-sig-Geo at r-project.org>>
>                  <mailto:R-sig-Geo at r-project.
>         <mailto:R-sig-Geo at r-project.>____org
>         <mailto:R-sig-Geo at r-project.__org <mailto:R-sig-Geo at r-project.org>>>
>                           <mailto:R-sig-Geo at r-project
>         <mailto:R-sig-Geo at r-project>.
>                  <mailto:R-sig-Geo at r-project
>         <mailto:R-sig-Geo at r-project>.>______org
>                  <mailto:R-sig-Geo at r-project.
>         <mailto:R-sig-Geo at r-project.>____org
>         <mailto:R-sig-Geo at r-project.__org
>         <mailto:R-sig-Geo at r-project.org>>>>
>         https://stat.ethz.ch/mailman/________listinfo/r-sig-geo
>         <https://stat.ethz.ch/mailman/______listinfo/r-sig-geo>
>                  <https://stat.ethz.ch/mailman/______listinfo/r-sig-geo
>         <https://stat.ethz.ch/mailman/____listinfo/r-sig-geo>>
>
>
>           <https://stat.ethz.ch/mailman/______listinfo/r-sig-geo
>         <https://stat.ethz.ch/mailman/____listinfo/r-sig-geo>
>                  <https://stat.ethz.ch/mailman/____listinfo/r-sig-geo
>         <https://stat.ethz.ch/mailman/__listinfo/r-sig-geo>>>
>
>
>         <https://stat.ethz.ch/mailman/______listinfo/r-sig-geo
>         <https://stat.ethz.ch/mailman/____listinfo/r-sig-geo>
>                  <https://stat.ethz.ch/mailman/____listinfo/r-sig-geo
>         <https://stat.ethz.ch/mailman/__listinfo/r-sig-geo>>
>
>           <https://stat.ethz.ch/mailman/____listinfo/r-sig-geo
>         <https://stat.ethz.ch/mailman/__listinfo/r-sig-geo>
>                  <https://stat.ethz.ch/mailman/__listinfo/r-sig-geo
>         <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>>>>
>
>
>
>
>



More information about the R-sig-Geo mailing list