[R] Adding a vertical line to plot with two overlapping density plots
Hi, A simple question I hope. I wish to add a single vertical line to a plot with several density plots. Here is a simplified example. thedata - data.frame(x1=rnorm(100,1,1),x2=rnorm(100,3,1)) #create data thedata.m-melt(thedata) densityplot(~value, thedata.m, groups=variable,auto.key=list(columns=2)) #this gives the two density plots # what I wish now is to add a vertical line, for example v=2. Many thanks, Josh. [[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.
Re: [R] Adding a vertical line to plot with two overlapping density plots
Hi Hi, A simple question I hope. I wish to add a single vertical line to a plot with several density plots. Here is a simplified example. thedata - data.frame(x1=rnorm(100,1,1),x2=rnorm(100,3,1)) #create data thedata.m-melt(thedata) densityplot(~value, thedata.m, groups=variable,auto.key=list(columns=2)) #this gives the two density plots # what I wish now is to add a vertical line, for example v=2. If you mean densityplot from lattice here is a function (addapted from Gabor Grothendieck's code] which adds arbitrary line(s) into panels. Is options are similar to abline and once=TRUE means you want different lines to different panels. addLine function (a = NULL, b = NULL, v = NULL, h = NULL, ..., once = FALSE) { tcL - trellis.currentLayout() k - 0 for (i in 1:nrow(tcL)) for (j in 1:ncol(tcL)) if (tcL[i, j] 0) { k - k + 1 trellis.focus(panel, j, i, highlight = FALSE) if (once) panel.abline(a = a[k], b = b[k], v = v[k], h = h[k], ...) else panel.abline(a = a, b = b, v = v, h = h, ...) trellis.unfocus() } } Regards Petr Many thanks, Josh. [[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. __ 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.
Re: [R] Adding a vertical line to plot with two overlapping density plots
thank you very much Petr. Yes, I meant densityplot form lattice. The code I gave plots the densities of both x1 and x2 in one panel. Could you show me how to integrate the function addline into the code so that a vertical line v=0 is added? On 4 January 2012 15:55, Petr PIKAL petr.pi...@precheza.cz wrote: Hi Hi, A simple question I hope. I wish to add a single vertical line to a plot with several density plots. Here is a simplified example. thedata - data.frame(x1=rnorm(100,1,1),x2=rnorm(100,3,1)) #create data thedata.m-melt(thedata) densityplot(~value, thedata.m, groups=variable,auto.key=list(columns=2)) #this gives the two density plots # what I wish now is to add a vertical line, for example v=2. If you mean densityplot from lattice here is a function (addapted from Gabor Grothendieck's code] which adds arbitrary line(s) into panels. Is options are similar to abline and once=TRUE means you want different lines to different panels. addLine function (a = NULL, b = NULL, v = NULL, h = NULL, ..., once = FALSE) { tcL - trellis.currentLayout() k - 0 for (i in 1:nrow(tcL)) for (j in 1:ncol(tcL)) if (tcL[i, j] 0) { k - k + 1 trellis.focus(panel, j, i, highlight = FALSE) if (once) panel.abline(a = a[k], b = b[k], v = v[k], h = h[k], ...) else panel.abline(a = a, b = b, v = v, h = h, ...) trellis.unfocus() } } Regards Petr Many thanks, Josh. [[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. [[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.
Re: [R] Adding a vertical line to plot with two overlapping density plots
thank you very much Petr. Yes, I meant densityplot form lattice. The code I gave plots the densities of both x1 and x2 in one panel. Could you show me how to integrate the function addline into the code so that a vertical line v=0 is added? The function is used after you make a plot. addLine(v=2) shall be OK before you of course need to define the function addLine - function (a = NULL, b = NULL, v = NULL, h = NULL, ..., once = FALSE) { tcL - trellis.currentLayout() k - 0 for (i in 1:nrow(tcL)) for (j in 1:ncol(tcL)) if (tcL[i, j] 0) { k - k + 1 trellis.focus(panel, j, i, highlight = FALSE) if (once) panel.abline(a = a[k], b = b[k], v = v[k], h = h[k], ...) else panel.abline(a = a, b = b, v = v, h = h, ...) trellis.unfocus() } } Regards Petr On 4 January 2012 15:55, Petr PIKAL petr.pi...@precheza.cz wrote: Hi Hi, A simple question I hope. I wish to add a single vertical line to a plot with several density plots. Here is a simplified example. thedata - data.frame(x1=rnorm(100,1,1),x2=rnorm(100,3,1)) #create data thedata.m-melt(thedata) densityplot(~value, thedata.m, groups=variable,auto.key=list(columns=2)) #this gives the two density plots # what I wish now is to add a vertical line, for example v=2. If you mean densityplot from lattice here is a function (addapted from Gabor Grothendieck's code] which adds arbitrary line(s) into panels. Is options are similar to abline and once=TRUE means you want different lines to different panels. addLine function (a = NULL, b = NULL, v = NULL, h = NULL, ..., once = FALSE) { tcL - trellis.currentLayout() k - 0 for (i in 1:nrow(tcL)) for (j in 1:ncol(tcL)) if (tcL[i, j] 0) { k - k + 1 trellis.focus(panel, j, i, highlight = FALSE) if (once) panel.abline(a = a[k], b = b[k], v = v[k], h = h[k], ...) else panel.abline(a = a, b = b, v = v, h = h, ...) trellis.unfocus() } } Regards Petr Many thanks, Josh. [[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. __ 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.
Re: [R] Adding a vertical line to plot with two overlapping density plots
That works, but if all you want is one vertical line at 0, this will work densityplot(~value, thedata.m, groups=variable,auto.key=list(columns=2), panel = function(x, y, ...) { panel.densityplot(x, ...) panel.abline(v=0) } ) -- David L Carlson Associate Professor of Anthropology Texas AM University College Station, TX 77843-4352 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Petr PIKAL Sent: Wednesday, January 04, 2012 10:27 AM To: josh rosen Cc: r-help@r-project.org Subject: Re: [R] Adding a vertical line to plot with two overlapping density plots thank you very much Petr. Yes, I meant densityplot form lattice. The code I gave plots the densities of both x1 and x2 in one panel. Could you show me how to integrate the function addline into the code so that a vertical line v=0 is added? The function is used after you make a plot. addLine(v=2) shall be OK before you of course need to define the function addLine - function (a = NULL, b = NULL, v = NULL, h = NULL, ..., once = FALSE) { tcL - trellis.currentLayout() k - 0 for (i in 1:nrow(tcL)) for (j in 1:ncol(tcL)) if (tcL[i, j] 0) { k - k + 1 trellis.focus(panel, j, i, highlight = FALSE) if (once) panel.abline(a = a[k], b = b[k], v = v[k], h = h[k], ...) else panel.abline(a = a, b = b, v = v, h = h, ...) trellis.unfocus() } } Regards Petr On 4 January 2012 15:55, Petr PIKAL petr.pi...@precheza.cz wrote: Hi Hi, A simple question I hope. I wish to add a single vertical line to a plot with several density plots. Here is a simplified example. thedata - data.frame(x1=rnorm(100,1,1),x2=rnorm(100,3,1)) #create data thedata.m-melt(thedata) densityplot(~value, thedata.m, groups=variable,auto.key=list(columns=2)) #this gives the two density plots # what I wish now is to add a vertical line, for example v=2. If you mean densityplot from lattice here is a function (addapted from Gabor Grothendieck's code] which adds arbitrary line(s) into panels. Is options are similar to abline and once=TRUE means you want different lines to different panels. addLine function (a = NULL, b = NULL, v = NULL, h = NULL, ..., once = FALSE) { tcL - trellis.currentLayout() k - 0 for (i in 1:nrow(tcL)) for (j in 1:ncol(tcL)) if (tcL[i, j] 0) { k - k + 1 trellis.focus(panel, j, i, highlight = FALSE) if (once) panel.abline(a = a[k], b = b[k], v = v[k], h = h[k], ...) else panel.abline(a = a, b = b, v = v, h = h, ...) trellis.unfocus() } } Regards Petr Many thanks, Josh. [[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. __ 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. __ 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.