Skip to content

Introducing \n's so that par.strip.text can produce multiline strips in lattice

5 messages · Dennis Murphy, Ashim Kapoor, David Winsemius

#
Hi:

This worked for me - I needed to modify some of the strip labels to
improve the appearance a bit and also reduced the strip font size a
bit to accommodate the lengths of the strings. The main thing was to
change \\n to \n.

Firstly, I created a new variable called Indic as a character variable
and then did some minor surgery on three of the strings:

Indic <- as.character(imports$Indicator)
Indic[3 + 6 *(0:5)] <- "Chemicals and related\n   products imports"
Indic[4 + 6 *(0:5)] <- "Pearls, semiprecious &\nprecious stones imports"
Indic[5 + 6 *(0:5)] <- "Metaliferrous ores &\nmetal scrap imports"

# Read Indic into the imports data frame as a factor:
imports$Indic <- factor(Indic)

# Redo the plot:
barchart(X03/1000 ~ time | Indic,
         data = imports[which(imports$time != 1), ],
         horiz = FALSE,
         scales = list(x = list(rot=45, labels=paste("Mar",2007:2011))),
         par.strip.text=list(lineheight=1, lines=2, cex = 0.8))

Dennis
On Wed, Nov 16, 2011 at 11:25 PM, Ashim Kapoor <ashimkapoor at gmail.com> wrote:
#
On Nov 18, 2011, at 1:51 AM, Ashim Kapoor wrote:

            
Of course there is. The key however ti to realize that at the moment  
there are "\\"'s and "n"'s but not any single characters with the  
representation "\n" when printed to the console.

If you are going to attempt to replace the "\\"'s with sub or gsub you  
need to futher realize that in order to match the "\\"'s in the  
current vactor you will need to further escape the "|" characters in  
the regex pattern

 > var
[1] "ee\\nee"
 > nchar(var)
[1] 6
 > grep("n", var)
[1] 1               # so there are ordinary "n" in there
 > grep("\\\\", var)
[1] 1               # and you need to use quadruple escapes (actually
                       escape-1 to generate an escape and escape-2 to  
match the escaped-\


imports$Indicator <- sub("\\\\n", "\n", imports$Indicator)
 > imports
                                            Indicator    Units  
Expression time       X03 id
1.7                            Gold & silver imports Rs.crore        
Ival    7  66170.46  1
2.7                                     Gold imports Rs.crore        
Ival    7  65337.72  2
3.7         Chemicals and related\n products imports Rs.crore        
Ival    7  62669.86  3
4.7  Pearls precious &\n semiprecious stones imports Rs.crore        
Ival    7  33870.17  4
5.7         Metaliferrous ores & metal scrap imports Rs.crore        
Ival    7  36779.35  5
snipped further output

You need to work on learning to create minimal code.