Hi All, I need a small help plotting median lines on lattice boxplots. # Data a <- rep(c("A","B"), each=10) b <- rep(c("a","a+b","b","b+a"), each=5) c <- c(1,9,5,2,7,7,8,8,8,5,4,5,3,2,5,6,7,8,9,1) x <- data.frame(a, b, c) med.A <- median(subset(x$c, x$a=="A")) med.B <- median(subset(x$c, x$a=="B")) median(group A) = 7 median(group B) = 5 I need help to plot two (horizontal) lines, one for each subgroup. - Line 1 coordinates should be (0, 7) to (2.5, 7) - Line 2 coordinates should be (2.5, 5) to (5, 5) I'm unable to control the length of these lines, can anyone please help me with this, I would highly appreciate any help. (I tried using panel.curve, but of no use) # Plotting code: library(lattice) bwplot(c ~ b, data = x, panel = function(...) { panel.abline(v=2.5, col.line="gray") panel.bwplot(...) } ) Thank in advance, -- Xin Ge. [[alternative HTML version deleted]]
Xin, Xin Ge wrote:> Hi All, > > I need a small help plotting median lines on lattice boxplots. > > # Data > a <- rep(c("A","B"), each=10) > b <- rep(c("a","a+b","b","b+a"), each=5) > c <- c(1,9,5,2,7,7,8,8,8,5,4,5,3,2,5,6,7,8,9,1) > x <- data.frame(a, b, c) > med.A <- median(subset(x$c, x$a=="A")) > med.B <- median(subset(x$c, x$a=="B")) > > median(group A) = 7 > median(group B) = 5 > > I need help to plot two (horizontal) lines, one for each subgroup. > - Line 1 coordinates should be (0, 7) to (2.5, 7) > - Line 2 coordinates should be (2.5, 5) to (5, 5) > > I'm unable to control the length of these lines, can anyone please help me > with this, I would highly appreciate any help. (I tried using panel.curve, > but of no use)There could be a better way, but here's a method using the lower-level grid functions. bwplot(c ~ b, data = x, panel = function(...) { panel.abline(v=2.5, col.line="gray") grid.lines(x = unit(c(0, 2.5), "native"), y = unit(c(med.B, med.B), "native"), gp = gpar(col = "red")) grid.lines(x = unit(c(2.5, 5), "native"), y = unit(c(med.A, med.A), "native"), gp = gpar(col = "red")) panel.bwplot(...) } )
On Wed, Jun 9, 2010 at 6:49 PM, Xin Ge <xingemaillist at gmail.com> wrote:> Hi All, > > I need a small help plotting median lines on lattice boxplots. > > # Data > a <- rep(c("A","B"), each=10) > b <- rep(c("a","a+b","b","b+a"), each=5) > c <- c(1,9,5,2,7,7,8,8,8,5,4,5,3,2,5,6,7,8,9,1) > x <- data.frame(a, b, c) > med.A <- median(subset(x$c, x$a=="A")) > med.B <- median(subset(x$c, x$a=="B")) > > median(group A) = 7 > median(group B) = 5 > > I need help to plot two (horizontal) lines, one for each subgroup. > - Line 1 coordinates should be (0, 7) to (2.5, 7) > - Line 2 coordinates should be (2.5, 5) to (5, 5) > > I'm unable to control the length of these lines, can anyone please help me > with this, I would highly appreciate any help. (I tried using panel.curve, > but of no use)You need panel.segments. -Deepayan> # Plotting code: > library(lattice) > > bwplot(c ~ b, data = x, > ? panel = function(...) > ? { > ? ? ?panel.abline(v=2.5, col.line="gray") > ? ? ?panel.bwplot(...) > ? } > ) > > Thank in advance, > > -- > Xin Ge. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >