Skip to content

lattice panel fine control

9 messages · Bert Gunter, Dennis Murphy, Massimo Bressan +1 more

#
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)
2-	how to label points with the ?name?? (no clue for this)
3-	how to draw error bar points referring of both x and y axes? (no clue for
this)
4-	how to format axes tickmark labels with a scientific notation? (no clue
for this)
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)

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.
#
It is perhaps not entirely clear from ?update, but update replaces
your original lattice call with your new call and re-drawsthe plot.
This means that the panel function used for the update() plot is the
one in update(), which only draws the line. That's why you see no
points.

Obviously, then, you need to include both points and abline in your
panel call to get both. However, if you would like to follow the
strategy that you have indicated, you can do so by using the layer()
functionality in the lattceExtra package, in which you would do
something similar to what you have shown.

library(latticeExtra)  ## after you have installed it from CRAN, possibly
?layer

for further info.

Your numerous other questions can be answered by close reading of the
?xyplot Help file and/or adding additional functionality to your panel
call. For example, text can be added via the panel.text() function.

?lattice

will get you info on this and other low level graphics functionality,
as well as references to consult. I would also suggest Deepayan
Sarkar's book, if you intend to use lattice seriously.

Cheers,
Bert
On Wed, Jun 1, 2011 at 10:00 AM, maxbre <mbressan at arpa.veneto.it> wrote:

  
    
#
Hi:

See inline.
On Wed, Jun 1, 2011 at 10:00 AM, maxbre <mbressan at arpa.veneto.it> wrote:
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.
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.
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.
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.
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
#
thank you so much for the very detailed indications which turned out to be a
real help in ponting me to the right direction; 

referring back to my previous questions there is something still open:

2- I'm in trouble with the point labels because I would like to rotate them
by an angle of 90 degrees (and I did not find mention of anything like
"angle" or "rot" to accomplish this task)
panel.text(x, y, lab = mydata$name, cex = 0.6, pos=3, offset=0.5, "....here
something to rotate labels")

3- I was referring to the error bar of each point (standard error of
points); I think this could be accomplished by "arrows" but the following
line is giving me an error
panel.arrows(x=tv.avg-tv.erst, y=ped.avg-ped.erst, x1=tv.avg+tv.erst,
y1=ped.avg+ped.erst, angle=90, code=3)

thanks again for your great help in bootstrapping me to the lattice features

maxbre

--
View this message in context: http://r.789695.n4.nabble.com/lattice-panel-fine-control-tp3566347p3568424.html
Sent from the R help mailing list archive at Nabble.com.
#
You did not read the help files carefully enough.

The Help for panel.text tells you that it is the same function as
ltext. ltext lists a bunch of parameters, srt among them, and refers
you to the corresponding base R graphics function, which is text().

?text then refers you to par for this and other miscellaneous parameters.

?par then tells you that srt gives the string rotation in degrees, the
answer to your question.

So, yes, it's a bit rough going; but careful attention to the docs
DOES get you there.

-- Bert
On Thu, Jun 2, 2011 at 7:12 AM, maxbre <mbressan at arpa.veneto.it> wrote:

  
    
#
ok thanks again for your reply: this is what I worked out so far
#start code
xyplot(ped.avg ~ tv.avg | family, 
               data=mydata, 
               strip=strip.custom(bg='white'), col.line=1, main="title", 
               xlab = expression(paste('tv [ fg/', m^3, ' ]', sep = '')), 
			   ylab = expression(paste('ped [ fg/', m^3, ' ]', sep = '')),

               scales= list(x=list(relation="free", log=10), 
                            y=list(relation="free", log=10)
							),
			   as.table=TRUE,
			   								
               prepanel = function(x, y, ...) { 
	xlim=list(c(1, 3), c(0.2,2.8), c(1.8,3.8),c(1.8,3)) 
             ylim=list(c(1, 3), c(0.2,2.8), c(1.8,3.8),c(1.8,3)) 
	dx=list(c(1.4,1.8,2.2,2.6), c(0.6,1,1.4,1.8,2.2,2.6), c(2.2,2.6,3,3.4,3.8),
c(2.2,2.6,3))
	dy=list(c(1.4,1.8,2.2,2.6), c(0.6,1,1.4,1.8,2.2,2.6), c(2.2,2.6,3,3.4,3.8),
c(2.2,2.6,3))
				  }, 
               panel = function(x, y,...) { 
                  panel.xyplot(x, y, cex=0.5,...) 
                  panel.abline(a = 0, b = 1, lty = 2, col ="gray") 
                  panel.text(x, y, lab = mydata$name, cex = 0.5, pos=3,
offset=1, srt=90)
				                   }, 
               xscale.components = xscale.components.logpower, 
               yscale.components = yscale.components.logpower 
    ) 
#end code

but still something is not clear at all to me; 
I've been reading the help files but I must confesss that are quite obscure
to me; I would say there is a strange behaviour of this bit of code because
I can not properly get a fine control of each panel x and y axis

 prepanel = function(x, y, ...) { 
	xlim=list(c(1, 3), c(0.2,2.8), c(1.8,3.8),c(1.8,3)) 
             ylim=list(c(1, 3), c(0.2,2.8), c(1.8,3.8),c(1.8,3)) 
	dx=list(c(1.4,1.8,2.2,2.6), c(0.6,1,1.4,1.8,2.2,2.6), c(2.2,2.6,3,3.4,3.8),
c(2.2,2.6,3))
	dy=list(c(1.4,1.8,2.2,2.6), c(0.6,1,1.4,1.8,2.2,2.6), c(2.2,2.6,3,3.4,3.8),
c(2.2,2.6,3))
				  }, 

I would like to have for each panel x and y axis equallly sped and with
equal limits; for some reasons I do not understand at all the aboe code did
not get the wanted result

any help for this?

thank you, this is indeed a good learning session to me

--
View this message in context: http://r.789695.n4.nabble.com/lattice-panel-fine-control-tp3566347p3569380.html
Sent from the R help mailing list archive at Nabble.com.
#
You're still not reading the Help carefully/thoroughly.

Check the entry under the "relation" subargument for the "scales"
argument of xyplot. You need relation = "same", which is the default
and so can be omitted.

-- Bert
On Thu, Jun 2, 2011 at 2:02 PM, maxbre <mbressan at arpa.veneto.it> wrote:

  
    
#
there is another big problem I did not realised earlier;
the labelling of point is not correct: the labels appears to be recycled the
same for all panels, why?
and this time I'm quite sure I've been reading the hlp file (as much as I
understand)
thanks


--
View this message in context: http://r.789695.n4.nabble.com/lattice-panel-fine-control-tp3566347p3570111.html
Sent from the R help mailing list archive at Nabble.com.
#
On 2011-06-02 23:53, maxbre wrote:
I suspect that you need to use the 'subscripts' argument.

Peter Ehlers