Skip to content

Bug in legend?

4 messages · Steve Roberts, Kjetil Halvorsen, Brian Ripley +1 more

#
This is a bit obscure but caused me some grief...  R1.5.1/MS 
Windows

There seems to be a peculiarity in legend associated with the use 
of expression text. It seems as if expression text is used in (eg) an 
mtext call then if the call to legend also includes expression text 
and a subscript then the legend box and line spacing depends on 
the value of cex used in the previous call to mtext, rather than that 
specified in the call to legend.

An R file which demonstrates the problem is pasted below (subject 
to the usual line wrapping issues)

Steve.

-------------------------------------------------------------------------------------
par(mfrow=c(2,2))
    
#Drawing a legend containing a subscript following a call to mtext 
also containing expression text - the 
# legend layout is dependent on the cex size specified in the 
previous call to mtext
# (regardless of the size set in the call to legend).....
#This shows the issue...  
for (i in 1:4) 
    {
    plot(c(0,1),c(0,1),type="n",xaxt="n",yaxt="n",xlab="",ylab="")     
#empty plot
    mtext(expression(Expression~text),cex=i/2) #Some text in 
margin - expression TEXT
    
legend(.5,.5,expression(Some[subscripted],Thing),lty=c(1,2),bty="o
",xjust=.5,yjust=.5,cex=1) #Draw legend with subscript in middle of 
plot  
    }
#This does what it should 
for (i in 1:4) 
    {
    plot(c(0,1),c(0,1),type="n",xaxt="n",yaxt="n",xlab="",ylab="")     
#empty plot
    mtext("Normal Text",cex=i/2) #Some text in margin - normal 
TEXT
    
legend(.5,.5,expression(Some[subscripted],Thing),lty=c(1,2),bty="o
",xjust=.5,yjust=.5,cex=1) #Draw legend with subscript in middle of 
plot  
    }
#As does this
for (i in 1:4) 
    {
    plot(c(0,1),c(0,1),type="n",xaxt="n",yaxt="n",xlab="",ylab="")     
#empty plot
    mtext(expression(Expression~text),cex=i/2) #Some text in 
margin - expression TEXT
    
legend(.5,.5,expression(Some~unsubscripted,Thing),lty=c(1,2),bty=
"o",xjust=.5,yjust=.5,cex=1) #Draw legend NO SUBSCRIPT in 
middle of plot  
    }

#A second call to legend gets it right!
    plot(c(0,1),c(0,1),type="n",xaxt="n",yaxt="n",xlab="",ylab="")     
#empty plot
    mtext(expression(Expression~text),cex=2) #Some text in 
margin - expression TEXT
    
legend(.5,.5,expression(Some[subscripted],Thing),lty=c(1,2),bty="o
",xjust=.5,yjust=.5,cex=1) #Draw legend with subscript in middle of 
plot  
    
legend(.5,.5,expression(Some[subscripted],Thing),lty=c(1,2),bty="o
",xjust=.5,yjust=.5,cex=1) #Draw legend with subscript in middle of 
plot  

------------------------------------------------------------------------------------
Dr Steve Roberts 
steve.roberts at man.ac.uk

Biostatistics Group,				
School of Epidemiology and Health Sciences,	
2nd Floor,Stopford Building,			
University of Manchester.			
M13 9PT.					
0161 275 5192					
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Hola!

Why does I get only the underlying representation, and not the level
names, in the following code to recode a factor? Seems like a bug to me.
+                          ifelse(Malaria$Trat=="QT", "SP", "QT"),
+                          Malaria$Trat))
[1] 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  QT 2  2  2  2  2  QT
QT 2 
[25] 2  2  QT 2  2  2 
Levels:  1 2 QT
[1] QT QT QT QT QT QT QT QT QT QT QT QT QT QT QT SP SP SP SP SP SP SP
SP SP
[25] SP SP SP SP SP SP
Levels:  QT SP 


Kjetil Halvorsen
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Wed, 31 Jul 2002, kjetil halvorsen wrote:

            
You have coercion going on there.  Here is a simpler example:
[1] "a"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15"
[16] "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26"

S does the same.
`ifelse' returns a value with the same shape as `test' which is
  filled with elements selected from either `yes' or `no' depending
  on whether the element of `test' is `TRUE' or `FALSE'.

Note that there is no mention of carrying attributes across from yes or no.

I think you want something like

Malaria$TratUl <- Malaria$Trat
Malaria$TratUl[Malaria$DiaC>0] <-
   ifelse(Malaria$Trat=="QT", "SP", "QT")[Malaria$DiaC>0]

since that only works with character vectors on the RHS.

  
    
#
ripley at stats.ox.ac.uk writes:
..and even
[1] "a"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14"
[16] "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26"