[R] add median/mean line to stri-plot in lattice

Duncan Mackay dulcalma at bigpond.com
Fri Jan 24 00:45:35 CET 2014


Hi Luigi

Not sure what you want exactly.
I have cleaned up the formula - do that at the data.frame first.
see http://lmdvr.r-forge.r-project.org/figures/figures.html for more
examples on lattice
Please dput your data - makes things easier and we have what you are working
with

There may be better ways of doing this but try

dat <-
structure(list(patient = c("A264", "A264", "A264", "A264", "A264",
"A264", "A264", "H041", "H041", "H041", "H041", "H041", "H041",
"H041", "A263", "A263", "A263", "A263", "A263", "A263", "A263",
"N241", "N241", "N241", "N241", "N241", "N241", "N241"), TB = c("A",
"A", "A", "A", "A", "A", "A", "L", "L", "L", "L", "L", "L", "L",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A"), gene = c("IP10", "IP10", "IP10", "IP10", "IP10", "IP10",
"IP10", "IP10", "IP10", "IP10", "IP10", "IP10", "IP10", "IP10",
"IP10", "IP10", "IP10", "IP10", "IP10", "IP10", "IP10", "IP10",
"IP10", "IP10", "IP10", "IP10", "IP10", "IP10"), stimulus = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("ESAT6",
"CFP10", "Rv3615c", "Rv2654", "Rv3879", "Rv3873", "PHA"), class = "factor"),
    ratio = c(250.9, 399.1, 959.4, 1.994, 589.2, 5.366, 48.27,
    10.32, NA, 3.33, NA, 1, NA, 1, 509.5, 991, 689.6, 1285, 683.7,
    468.7, 47.08, 8.534, 2.733, NA, 11.18, 10.41, 0.7532, NA)), .Names =
c("patient",
"TB", "gene", "stimulus", "ratio"), row.names = c(NA, -28L), class =
"data.frame")

#dat$stimulus = factor(dat$stimulus,    levels =
c("ESAT6","CFP10","Rv3615c", "Rv2654", "Rv3879","Rv3873","PHA"))
datav <- aggregate(ratio ~ TB, dat, mean, na.rm = T)
datme <- aggregate(ratio ~ TB, dat, median, na.rm = T)
datavA <- aggregate(ratio ~ TB+stimulus, dat, mean, na.rm = T)
datmeA <- aggregate(ratio ~ TB+stimulus, dat, median, na.rm = T)

stripplot(ratio ~ TB|stimulus, dat,
          hor=F,
          as.table = TRUE,
          layout = c(7,1),
          scales = list(relation = "same"),
          ylab=expression(bold("Ratio GOI/RG")),
          xlab=expression(bold("Antigen stimulation")),
          main="IP10 expression",
          pch=19,
          col=c("red","black"),
          par.settings = list(strip.background=list(col="white"),
                              superpose.symbol = list(col =
c("red","black"),
                                                      pch = c(1,20))) ,
          par.strip.text=list(font=2),
          panel = function(x, y, ...){
                     pnl = panel.number()
                     # mean
                     panel.abline(h = datavA[datavA[,2]==
levels(datavA[,2])[pnl],"ratio"], col = c("red","black"))
                     # median
                     panel.abline(h = datmeA[datmeA[,2]==
levels(datmeA[,2])[pnl],"ratio"], col = c("red","black"), lty=3)

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

stripplot(ratio ~ TB|stimulus, dat,
          hor=F,
          as.table = TRUE,
          layout = c(7,1),
          scales = list(relation = "same"),
          ylab=expression(bold("Ratio GOI/RG")),
          xlab=expression(bold("Antigen stimulation")),
          main="IP10 expression",
          pch=19,
          col=c("red","black"),
          par.settings = list(strip.background=list(col="white"),
                              superpose.symbol = list(col =
c("red","black"),
                                                      pch = c(1,20))) ,
          par.strip.text=list(font=2),
          panel = function(x,y,...){

                     panel.abline(h = datav[,2], col = c("red",
"black"),lty=1)
                     panel.abline(h = datme[,2], col = c("red",
"black"),lty=3)

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

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au




-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Luigi Marongiu
Sent: Friday, 24 January 2014 06:55
To: r-help at r-project.org
Subject: [R] add median/mean line to stri-plot in lattice

dear all,
I set a series of variable subdivided in classes (there are 7 in here,
defined by the clause 'levels'), and I am showing the results in strip
chart using the package 'lattice'. Whitin each class the samples are
further subdivided in "active" (A) and "latent" (L). The function I have
written is:

stripplot(my.data.IP10$ratio ~
factor(my.data.IP10$TB)|factor(my.data.IP10$stimulus,
   levels = c("ESAT6","CFP10","Rv3615c", "Rv2654", "Rv3879",
"Rv3873","PHA")),
   my.data, hor=F, layout = c(7,1), scales = list(relation = "same"),
   ylab=expression(bold("Ratio GOI/RG")), xlab=expression(bold("Antigen
stimulation")), main="IP10 expression", pch=19, col="black",
   par.settings = list(strip.background=list(col="white")),
   par.strip.text=list(font=2))

which defines the figure attached "IP10".
Providing the median or average values (that will depend on the
distribution of the final data), is there a way to add a line overlapping
each of these classes and subclasses? I reckon it will be a vector of 14
elements, which can be called "Mvect".
In addition is there a way to differently colour the subclasses? lest say
black for active and white for latent? I tried with 'group' but with not
success.

Thank you for any help, I am not quite familiar with the Latex codes. I am
also attaching the dataframe containing the data for further reference.
Best regards
Luigi




More information about the R-help mailing list