[R] set axis limit in lattice

Duncan Mackay dulcalma at bigpond.com
Thu May 1 04:00:40 CEST 2014


If you just want to limit the axis just insert as arguments
for the x axis
xlim = c(min, max), 
and for the y
ylim = c(min,max) ,
where min and max correspond to the appropriate min and max  for the x and y axes

As for the panel.groups you mention medians where I was using means - if you want medians you will have to amend means to medians  

Make sure for the groups you have the right mode numeric/ factor ?
I do not know what igra is

Duncan

-----Original Message-----
From: Luigi Marongiu [mailto:marongiu.luigi at gmail.com] 
Sent: Thursday, 1 May 2014 03:54
To: Duncan Mackay
Subject: Re: [R] set axis limit in lattice

Dear Duncan,
these solutions are actually splitting the drawing in different panels
and the scale=free is giving different limits to each panel. I still
need the relation=same but just to input myself the fixed limits of
the axis. It should go into the scale argument but i don't know the
actual syntax.
As for the segments, you were perfectly right, there was a problem
with my script so the medians did not match the actual data. Your
solution using panel.groups works on the example, but when I copied it
to the actual dataframe I am working with it did not work despite the
fact of being of quite a general syntax. could you please give a look
at the dataframe I have attached and see where I made a mistake in
applying your solution? what I wrote is:

  my.data.IP10<-subset(my.data, gene=="i" & stimulus != "Unstimulated")

stripplot(
   my.data.IP10$ratio ~
factor(my.data.IP10$igra)|factor(my.data.IP10$stimulus, levels =
c("ESAT6","CFP10","Rv3615c", "Rv2654","Rv3879", "Rv3873","PHA")),
         my.data,
         group = my.data.IP10$igra,
         hor=F,
        layout = c(7,1),
         scales = list(x = list(draw=FALSE)),
   jitter.data=TRUE,
   pch=c(1,16),
   col="black",
         ylab=expression(bold("Ratio GOI/RG")),
         xlab=expression(bold("Antigen stimulation")),
         main="IP10 expression",
         par.settings = list(strip.background=list(col="light grey")),
             par.strip.text=list(font=2),
       key = list(space="top", columns=2, text=list(c("IGRA Negative",
"IGRA Positive"), col="black"), points=list(pch=c(1,16),
col="black")),

 panel = panel.superpose,
        panel.groups = function(x, y, group.number, ...) {

                        panel.segments(x0= x-0.25, x1=x+0.25,
y0=mean(y), y1=mean(y), lty = c(1:2)[group.number],
                         lwd=c(2:1)[group.number], col = c(1:2)[group.number])

                        panel.stripplot(x,y, ...)
              }
)


### Many thanks
### Best regards
Luigi


On Wed, Apr 30, 2014 at 12:15 AM, Duncan Mackay <dulcalma at bigpond.com> wrote:
> Hi Luigi
>
> This produces plenty of white space for the y axes
>
> Y<-max(my.data$copy)
>
> stripplot(
>   copy ~ factor(positivity)|factor(stimulation, levels = c("Unst.",
> "ESAT6","CFP10","Rv3615c", "Rv2654","Rv3879", "Rv3873","PHA")),
>        my.data,
>        group = positivity,
>        hor=F,
>        layout = c(8,1),
>        scales = list(x = list(at = c(1,2), labels = c("N","P")),
>                      y= list(relation = "free")),
>        jitter.data=TRUE, pch=c(16,1), col="black",
>        ylab=expression(bold("Copy")),
>        xlab=expression(bold("Stimulation")),
>        main="Plot",
>        par.settings = list(strip.background=list(col="white")),
>        par.strip.text=list(font=2),
>        key = key.plot,
>        panel = function(x, y, ...)
>   {
>             pnl = panel.number()
>   k<-0
>   for (i in 1:2){
>    datme<-subset(datmeA, positivity==i-1)
>              w<-i+k
>                    panel.segments(x0 = X[w], y0 =  datme[datme[,2]==
> levels(datme[,2])[pnl],"copy"],
>                                   x1 = X[w+1],  y1 = datme[datme[,2]==
> levels(datme[,2])[pnl],"copy"],lwd = 2, col = "black")
>                   k=k+1
>
>       panel.stripplot(x,y, ...)
>    }
>              }
>        )
> You could limit the y-axis to your levels
> stripplot(
>   copy ~ factor(positivity)|factor(stimulation, levels = c("Unst.",
> "ESAT6","CFP10","Rv3615c", "Rv2654","Rv3879", "Rv3873","PHA")),
>        my.data,
>        group = positivity,
>        hor=F,
>        layout = c(8,1),
>        scales = list(x = list(at = c(1,2), labels = c("N","P")),
>                      y= list(relation = "free",
>                                  limits = list(c(0, max panel 1),
>                                                      c(0, max panel 2),
>                                                     ...
>                                                    c(0, max panel 8)),
>       )),
>        jitter.data=TRUE, pch=c(16,1), col="black",
>        ylab=expression(bold("Copy")),
>        xlab=expression(bold("Stimulation")),
>        main="Plot",
>        par.settings = list(strip.background=list(col="white")),
>        par.strip.text=list(font=2),
>        key = key.plot,
>        panel = function(x, y, ...)
>   {
>             pnl = panel.number()
>   k<-0
>   for (i in 1:2){
>    datme<-subset(datmeA, positivity==i-1)
>              w<-i+k
>                    panel.segments(x0 = X[w], y0 =  datme[datme[,2]==
> levels(datme[,2])[pnl],"copy"],
>                                   x1 = X[w+1],  y1 = datme[datme[,2]==
> levels(datme[,2])[pnl],"copy"],lwd = 2, col = "black")
>                   k=k+1
>
>       panel.stripplot(x,y, ...)
>    }
>              }
>        )
>
> Sorting the stimulation groups shows that some of them can be loosely grouped so reordering the factors
>
> stripplot(copy ~ factor(positivity)|factor(stimulation, levels = c("PHA","Rv2654","CFP10","Unst.","Rv3615c", "Rv3873","ESAT6", "Rv3879")),
>        my.data,
>        group = positivity,
>        hor=F,
>        layout = c(2,4),
>        scales = list(x = list(at = c(1,2), labels = c("N","P")),
>                      y= list(relation = "free")),
>        jitter.data=TRUE, pch=c(16,1), col="black",
>        ylab=expression(bold("Copy")),
>        xlab=expression(bold("Stimulation")),
>        main="Plot",
>        par.settings = list(strip.background=list(col="white")),
>        par.strip.text=list(font=2),
>        key = key.plot,
>        panel = function(x, y, ...)
>   {
>             pnl = panel.number()
>   k<-0
>   for (i in 1:2){
>    datme<-subset(datmeA, positivity==i-1)
>              w<-i+k
>                    panel.segments(x0 = X[w], y0 =  datme[datme[,2]==
> levels(datme[,2])[pnl],"copy"],
>                                   x1 = X[w+1],  y1 = datme[datme[,2]==
> levels(datme[,2])[pnl],"copy"],lwd = 2, col = "black")
>                   k=k+1
>
>       panel.stripplot(x,y, ...)
>    }
>              }
>        )
>
> or using panel.groups
>
> stripplot(copy ~ factor(positivity)|factor(stimulation, levels = c("PHA","Rv2654","CFP10","Unst.","Rv3615c", "Rv3873","ESAT6", "Rv3879")),
>        my.data,
>        group = positivity,
>        hor=F,
>        layout = c(2,4),
>        scales = list(x = list(at = c(1,2), labels = c("N","P")),
>                      y= list(relation = "free")),
>        jitter.data=TRUE, pch=c(16,1), col="black",
>        ylab=expression(bold("Copy")),
>        xlab=expression(bold("Stimulation")),
>        main="Plot",
>        par.settings = list(strip.background=list(col="white")),
>        par.strip.text=list(font=2),
>        key = key.plot,
>        panel = panel.superpose,
>        panel.groups = function(x, y, group.number, ...) {
>
>                         panel.segments(x0= x-0.25, x1=x+0.25, y0=mean(y), y1=mean(y), lty = c(1:2)[group.number],
>                         lwd=c(2:1)[group.number], col = c(1:2)[group.number])
>
>                         panel.stripplot(x,y, ...)
>              }
>       )
>
> Regards
>
> Duncan Mackay
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Luigi Marongiu
> Sent: Wednesday, 30 April 2014 03:58
> To: r-help at r-project.org
> Subject: [R] set axis limit in lattice
>
> Dear all,
>
> I would like to set the axis of a figure using the max() so to have
> more control on the limits of the axis -- this because in the actual
> case more independent figures are generated from the same dataframe
> and these must have the same axis scale.
>
> Since the figure is generated using lattice device, the syntax is too
> complicated for me: when I place the xlim=c(0,Y) in the arguments —
> where Y is obtained using max(data) — there is no result. In basic R
> this argument would work virtually in any position.
>
> Any tip?
>
> Best regards
>
> Luigi
>
>  ####CODE
> ### open plot library
> library(lattice)
> my.data<-structure(list(
>    column_1 = 1:120,
>    column_2 = structure(c(
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8,
>  1,2,3,4,5,6,7,8), .Label = c("Unst.", "ESAT6", "CFP10", "Rv3615c",
> "Rv2654", "Rv3879", "Rv3873", "PHA"), class = "factor"),
>  column_3 = structure(c(
> 0,0,0,0,0,0,0,0,
> 0,0,0,0,0,0,0,0,
> 0,0,0,0,0,0,0,0,
> 0,0,0,0,0,0,0,0,
> 0,0,0,0,0,0,0,0,
> 0,0,0,0,0,0,0,0,
> 0,0,0,0,0,0,0,0,
> 1,1,1,1,1,1,1,1,
> 1,1,1,1,1,1,1,1,
> 1,1,1,1,1,1,1,1,
> 1,1,1,1,1,1,1,1,
> 1,1,1,1,1,1,1,1,
> 1,1,1,1,1,1,1,1,
> 1,1,1,1,1,1,1,1,
> 0,0,0,0,0,0,0,0)),
>      column_4 = c(
>  192.0519108,183.6403531,53.46798757,83.60638077,69.60749873,159.4706861,256.8765622,499.2899303,
>  2170.799076,1411.349719,2759.472348,2098.973397,2164.739515,1288.676574,1611.486543,6205.229575,
>  870.7424981,465.9967135,191.8962375,864.0937485,2962.693675,1289.259137,2418.651212,7345.712517,
>  0,168.1198893,674.4342961,101.1575401,47.81596237,0,0,1420.793922,
>  142.6871331,5.466468742,291.9564635,80.73914133,73.02239621,64.47806871,144.3543635,3167.959757,
>  3164.748333,1092.634557,28733.20269,1207.87783,729.6090973,151.8706088,241.2466141,9600.963594,
>  1411.718287,12569.96285,1143.254476,6317.378481,16542.27718,79.68025792,1958.495138,7224.503437,
>  208.4382941,69.48609769,656.691151,0.499017582,7114.910926,187.6296174,41.73980805,8930.784541,
>  4.276752185,0.432300363,60.89228665,1.103924786,0.490686366,1.812993239,7.264531581,1518.610307,
>  2172.051528,595.8513744,17141.84336,589.6565971,1340.287628,117.350942,593.7034054,24043.61463,
>  0,81.83292179,1539.864321,36.41722958,8.385131047,161.7647376,65.21615696,7265.573875,
>  97.84753179,154.051827,0.613835842,10.06138851,45.04879285,176.8284258,18795.75462,30676.769,
>  5780.34957,944.2200834,2398.235596,1083.393165,2541.714557,1251.670895,1547.178549,1792.679176,
>  3067.988416,8117.210173,23676.02226,8251.937547,17360.80494,18563.61561,16941.865,31453.96708,
>  2767.493803,4796.33016,12292.93705,3864.657567,9380.673835,14886.44683,8457.88646,26050.47191)),
> .Names = c("row", "stimulation", "positivity", "copy"), row.names =
> c(NA, -120L),
>  class = "data.frame")
>
> key.plot<-list(
>   space="top", columns=2,
>   text=list(c("Positive", "Negative"), col="black"),
>   points=list(pch=c(16,1), col="black"))
>
> datmeA <- aggregate(copy ~ positivity+stimulation, my.data, median, na.rm = T)
>
> X<-c(0.7, 1.3, 1.7, 2.3)
>
> Y<-max(copy)
>
> stripplot(
>   copy ~ factor(positivity)|factor(stimulation, levels = c("Unst.",
> "ESAT6","CFP10","Rv3615c", "Rv2654","Rv3879", "Rv3873","PHA")),
>        my.data,
>        group = positivity,
>        hor=F,
>        layout = c(8,1),
>        scales = list(x = list(at = c(1,2), labels = c("N","P"))),
>        jitter.data=TRUE, pch=c(16,1), col="black",
>        ylab=expression(bold("Copy")),
>        xlab=expression(bold("Stimulation")),
>        main="Plot",
>        par.settings = list(strip.background=list(col="white")),
>        par.strip.text=list(font=2),
>        key = key.plot,
>        panel = function(x, y, ...)
>   {
>             pnl = panel.number()
>   k<-0
>   for (i in 1:2){
>    datme<-subset(datmeA, positivity==i-1)
>              w<-i+k
>                    panel.segments(x0 = X[w], y0 =  datme[datme[,2]==
> levels(datme[,2])[pnl],"copy"],
>                                   x1 = X[w+1],  y1 = datme[datme[,2]==
> levels(datme[,2])[pnl],"copy"],lwd = 2, col = "black")
>                   k=k+1
>
>       panel.stripplot(x,y, ...)
>    }
>              }
>        )
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list