Hi Luigi
If you str(stripplot(.)) ie your example you will see that the x is categorical and it is harder to put lines onto it than xyplot so that is what I referred to Try this - I have added a column and put in segments - I converted it to xyplot because of above I have set the x axis limits so if you change them the segments may need changing abline works OK as it does not have reference to the x axis I deleted your pch argument and modified the par.settings to suit I have not checked the values but may be necessary to put in extra lines of code for the 2 "groups" you may now be able to work with groups = TB and modify to suit if you need to work with groups see ?panel.superpose ?panel.groups 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$x <- ifelse(dat$TB == "A",1,2) datavA <- aggregate(ratio ~ TB+stimulus, dat, mean, na.rm = T) datmeA <- aggregate(ratio ~ TB+stimulus, dat, median, na.rm = T) xyplot(ratio ~ x|stimulus, dat, hor=F, as.table = TRUE, layout = c(7,1), ylab=expression(bold("Ratio GOI/RG")), xlab=expression(bold("Antigen stimulation")), main="IP10 expression", scales = list(x = list(alternating = FALSE, at = c(1,2), labels = c("A","L"))), xlim = c(0,3), col=c("red","black"), par.settings = list(strip.background=list(col="white"), superpose.symbol = list(col = c("red","black"), pch = c(19,19))), par.strip.text=list(font=2), panel = function(x, y, ...){ pnl = panel.number() # mean panel.segments(x0 = x-0.25, y0 = datavA[datavA[,2]== levels(datavA[,2])[pnl],"ratio"], x1= x+0.25, y1 = datavA[datavA[,2]== levels(datavA[,2])[pnl],"ratio"], col = c("red","black")) # median panel.segments(x0 = x-0.25, y0 = datmeA[datmeA[,2]== levels(datmeA[,2])[pnl],"ratio"], x1= x+0.25, y1 = datmeA[datmeA[,2]== levels(datmeA[,2])[pnl],"ratio"], lwd = 2, col = c("magenta","grey")) panel.stripplot(x,y, ...) } ) Duncan From: Luigi Marongiu [mailto:marongiu.lu...@gmail.com] Sent: Sunday, 26 January 2014 03:58 To: Duncan Mackay Subject: Re: [R] add median/mean line to stri-plot in lattice Dear Duncan, sorry to pester you again with this issue. The figure "strip_chart" was obtained using the basic R setting and that was perfectly satisfactory for its purpose; now I need to expand such approach to the "result database", which is more complex than the one that generated "strip_chart". In the code you provided me the lines are correctly drawn, only thing I need to make them shorter as in the "strip_chart" example, a task that I obtained with the code: segments(x-0.1, y, x+0.1, lwd=2) where y was the vector containing the median values of the data. What would be the equivalent for Lattice? I reckon it should be done with replacing panel.abline with something else. Many thanks again. And thank you also for reference, I will study it but it might take sometimes. Best regards Luigi 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, ...) } ) On Sat, Jan 25, 2014 at 11:45 AM, Duncan Mackay <dulca...@bigpond.com> wrote: Hi Luigi If you create a numeric variable of 1:5 to correspond as the x's - Normal, Borderline, . You could then plot the points as an xyplot -xyplot gives you more flexibility to do things. then add the lines as segments in a panel function Use scales for the labels as scales = list(x = list(at = 1:5, labels = c("Normal","Borderline", .))), Regards Duncan From: Luigi Marongiu [mailto:marongiu.lu...@gmail.com] Sent: Saturday, 25 January 2014 00:45 To: Duncan Mackay Subject: Re: [R] add median/mean line to stri-plot in lattice Dear Duncan, sorry for the data, it was a bit too bulky to simply put it here as a table, although you recreated it very good. What i would like to do is something similar to the figure attached, which was done with the normal strip chart. For each group there is the line indicating the median value. In the query I had the data is more complex and therefore "lattice" was a better choice, but the syntax is very difficult, in fact you had to implement a function whithin the function stripplot. Essentially there should be a short line overlapping each data set representing the median (or average) values. Probably it will be a matter of changing panel.abline, but I don't know how. Thank you very much for your help Luigi On Thu, Jan 23, 2014 at 11:45 PM, Duncan Mackay <dulca...@bigpond.com> wrote: 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: mac...@northnet.com.au -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Luigi Marongiu Sent: Friday, 24 January 2014 06:55 To: r-help@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 [[alternative HTML version deleted]] ______________________________________________ R-help@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.