[R] lattice panel fine control
Dennis Murphy
djmuser at gmail.com
Thu Jun 2 01:54:16 CEST 2011
Hi:
See inline.
On Wed, Jun 1, 2011 at 10:00 AM, maxbre <mbressan at arpa.veneto.it> wrote:
> Hello R experts,
>
> what follows is my reproducible example:
>
> mydata<-structure(list(ped.avg = c(335.9, 110.8, 645.7, 638.9, 1468.1,
> 126.4, 4811.1, 88.5, 868.5, 656.6, 723.6, 654, 2.8, 15, 14.2,
> 17.5, 15.4, 112.1, 424.7, 18.3, 19.9, 28.6, 25.6, 23.5, 15.4,
> 27, 62.1, 15.6, 74.6), ped.erst = c(96, 53.2, 615.2, 616.5, 512.9,
> 56.2, 1851.8, 57.1, 579.5, 613.2, 601.1, 613.6, 1.3, 6.3, 6.5,
> 6.1, 6.3, 42, 166.4, 6.5, 6.5, 7.6, 8, 7, 6.3, 8.8, 24.6, 6.3,
> 35.6), tv.avg = c(670.4, 320, 282.4, 266.6, 2077.3, 383.1, 7116,
> 335.1, 642.9, 291, 405.1, 280.8, 5, 18.5, 16.5, 28.6, 24.8, 150.2,
> 366, 40.3, 38.4, 51.9, 52.9, 48.1, 27.8, 73.8, 168.3, 32.8, 151.2
> ), tv.erst = c(233.8, 243.4, 245.9, 246.4, 389.9, 240.4, 1530.7,
> 242.8, 232.4, 245.1, 239.7, 245.7, 2.2, 6.9, 6, 11.5, 9.5, 69.5,
> 139.5, 17.9, 16.7, 22.9, 27.3, 24.5, 12.7, 46.3, 102.2, 15.7,
> 71.2), family = structure(c(1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L,
> 5L, 5L, 5L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 20L, 20L, 20L,
> 20L, 20L, 20L, 20L, 20L, 20L, 20L), .Label = c("non-orto PCB",
> "non-orto PCB", "non-orto PCB", "non-orto PCB", "mono-orto PCB",
> "mono-orto PCB", "mono-orto PCB", "mono-orto PCB", "mono-orto PCB",
> "mono-orto PCB", "mono-orto PCB", "mono-orto PCB", "PCDD", "PCDD",
> "PCDD", "PCDD", "PCDD", "PCDD", "PCDD", "PCDF", "PCDF", "PCDF",
> "PCDF", "PCDF", "PCDF", "PCDF", "PCDF", "PCDF", "PCDF"), class = "factor"),
> name = structure(c(28L, 29L, 22L, 26L, 18L, 19L, 20L, 21L,
> 23L, 24L, 25L, 27L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
> 11L, 12L, 13L, 14L, 15L, 16L, 17L), .Label = c("D4", "D5",
> "D6a", "D6b", "D6c", "D7", "D8", "F4", "F5a", "F5b", "F6a",
> "F6b", "F6c", "F6d", "F7a", "F7b", "F8", "P105", "P114",
> "P118", "P123", "P126", "P156", "P157", "P167", "P169", "P189",
> "P77", "P81"), class = "factor"), ped.lower = c(239.9, 57.6,
> 30.5, 22.400, 955.2, 70.2, 2959.3, 31.4, 289, 43.4, 122.5,
> 40.4, 1.5, 8.7, 7.7, 11.4, 9.1, 70.1, 258.3, 11.8, 13.4,
> 21, 17.6, 16.5, 9.1, 18.2, 37.5, 9.3, 39), ped.upper = c(431.9,
> 164, 1260.9, 1255.4, 1981, 182.6, 6662.9, 145.6, 1448, 1269.8,
> 1324.7, 1267.6, 4.1, 21.3, 20.7, 23.6, 21.7, 154.1, 591.1,
> 24.8, 26.4, 36.2, 33.6, 30.5, 21.7, 35.8, 86.7, 21.9, 110.2
> ), tv.lower = c(436.6, 76.6, 36.5, 20.2, 1687.4, 142.7, 5585.3,
> 92.3, 410.5, 45.9, 165.4, 35.1, 2.8, 11.6, 10.5, 17.1, 15.3,
> 80.7, 226.5, 22.4, 21.7, 29, 25.6, 23.6, 15.1, 27.5, 66.1,
> 17.1, 80), tv.upper = c(904.2, 563.4, 528.3, 513, 2467.2,
> 623.5, 8646.7, 577.9, 875.3, 536.1, 644.8, 526.5, 7.2, 25.4,
> 22.5, 40.1, 34.3, 219.7, 505.5, 58.2, 55.1, 74.8, 80.2, 72.6,
> 40.5, 120.1, 270.5, 48.5, 222.4)), .Names = c("ped.avg",
> "ped.erst", "tv.avg", "tv.erst", "family", "name", "ped.lower",
> "ped.upper", "tv.lower", "tv.upper"), row.names = c(NA, -29L), class =
> "data.frame")
>
>
> and this is the code I worked so far (in order to clear out what is the
> final result I would like to get)
>
> mydata$family <- factor(mydata$family, levels=as.character(mydata$family))
>
> library(lattice)
>
> myplot<-xyplot(ped.avg ~ tv.avg | family,
> data=mydata,
> strip=strip.custom(bg='white'), col.line=1, main="title",
> xlab="tv [fg/m3]", ylab="ped [fg/m3]",
> scales= list(x=list(relation="free", log=TRUE), y=list(relation="free",
> log=TRUE)),
> )
>
> update(myplot,
> xlim=list(c(1.9,3),c(2,3.8),c(0.5,2.7),c(1.2,2.4)),
> ylim=list(c(1.9,3),c(2,3.8),c(0.5,2.7),c(1.2,2.4)),
> cex=0.6, aspect="iso",
> panel=function(...){
> panel.abline(a=0, b=1, lty=1, col=1)
> }
> )
>
> obviously there is something wrong in the resulting chart and therefore
> these are my questions:
>
> 1- how to draw a diagonal lines in each of the panels without “deleting”
> points? (in my example the use of panel.abline is somehow getting rid of the
> points)
You forgot to plot the points in the panel function; you needed to add
panel.xyplot(x, y, ...). panel.abline() is not getting rid of the
points - you never indicated that you wanted them plotted.
> 2- how to label points with the “name”? (no clue for this)
One way is with panel.text(); see below. Another option is to use the
directlabels package, which has better algorithms for positioning text
relative to points.
> 3- how to draw error bar points referring of both x and y axes? (no clue for this)
I have no clue what you mean by this; are you referring to error bar
plots in the margins of each scatterplot? If so, that's not easy to do
since AFAIK there's no panel.errorbar() function. If this is what you
mean, you may have to write a panel function to add that functionality
to an xyplot; since you want different scales in each panel, the panel
function would have to be sensitive to scaling, which means you would
need to pay attention to the units in which you'd be plotting. If
you're lucky, someone may help you out with that.
Rug plots or density strips might be less intrusive than error bar
plots; for the latter, see the denstrip package. For rug plots, see
panel.rug(). IIRC, the denstrip package has a panel function for use
in lattice. These aren't necessarily better than error bar plots, but
they are alternatives that are useful to know about.
> 4- how to format axes tickmark labels with a scientific notation? (no clue
> for this)
See the example code below. The log base is specified in the scales()
statement for each of x and y; in particular, TRUE is replaced with 10
in each case. One then uses the *scales.component.logpower function
from latticeExtra to do the work, where * is either x or y.
> 5- how to list all these commands inside xyplot() without the need to use
> update()? (for some reasons I do not fully understand I can not ‘move’ all
> commands inside the main plotting– xyplot() - without loosing some relevant
> features of the chart)
Sans the error bar plots, this seems to be close to what you wanted:
myplot<-xyplot(ped.avg ~ tv.avg | family,
data=mydata,
strip=strip.custom(bg='white'), col.line=1, main="title",
xlab="tv [fg/m3]", ylab="ped [fg/m3]",
scales= list(x=list(relation="free", log=10),
y=list(relation="free", log=10)),
prepanel = function(x, y, ...) {
xlim=list(c(1.9,3), c(2,3.8), c(0.5,2.7), c(1.2, 2.4))
ylim=list(c(1.9,3), c(2,3.8), c(0.5,2.7), c(1.2, 2.4))
},
panel = function(x, y, ...) {
panel.xyplot(x, y, ...)
panel.abline(a = 0, b = 1, lty = 1, col = 1)
panel.text(x, y, lab = mydata$name, cex = 0.6)
},
xscale.components = xscale.components.logpower,
yscale.components = yscale.components.logpower
)
The prepanel function sets the xlim and ylim values for each panel.
The panel function plots the points, the 45 degree line and the text
labels, although the points are rather superfluous because they are
overwritten by the labels. Notice that by changing the limits, the 45
degree lines no longer bisect each panel.
If your m3 is really supposed to be m^3 (as in cubic meters), then you
can use expressions to do the work; one way is to replace your xlab
and ylab in the above call with
xlab = expression(paste('tv [fg/', m^3, ']', sep = '')),
ylab= expression(paste('ped [fg/', m^3, ']', sep = '')),
If you want to get rid of the points, take the panel.xyplot() line out
of the panel function (or comment it out).
HTH,
Dennis
>
> any help much appreciated
> sorry for such trivial question but I’m using R since a very short time:
> please keep your replies as much simple and self explanatory as possible
> thank you
>
> maxbre
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/lattice-panel-fine-control-tp3566347p3566347.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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