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

David Winsemius dwinsemius at comcast.net
Fri Nov 18 14:20:00 CET 2011


On Nov 18, 2011, at 1:51 AM, Ashim Kapoor wrote:

> Dear Dennis,
>
> Many thanks.I was wondering if there was a way to edit the variable  
> and put
> \n's in it. Is there ?

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.

-- 
David.

>
> Thank you,
> Ashim
>
> On Thu, Nov 17, 2011 at 4:07 PM, Dennis Murphy <djmuser at gmail.com>  
> wrote:
>
>> 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:
>>> Dear all,
>>>
>>> I have the following data, which has \\n in place of \n. I  
>>> introduced
>> \n's
>>> in the csv file so that I could use it in barchart in lattice.  
>>> When I did
>>> that and read it into R using read.csv, it read it as \\n. My  
>>> question is
>>> how do I introduce "\n" in the middle of a long string of quoted  
>>> text so
>>> that lattice can make multiline strips. Hitting Enter which is  
>>> supposed
>> to
>>> introduce \n's does'nt work because when I goto the middle of the  
>>> line
>> and
>>> press enter Open Office thinks that I am done with editing my text  
>>> and
>>> takes me to the next line.
>>>
>>>
>>>> dput(imports)
>>> structure(list(Indicator = structure(c(5L, 4L, 2L, 12L, 8L, 7L,
>>> 5L, 4L, 2L, 12L, 8L, 7L, 5L, 4L, 2L, 12L, 8L, 7L, 5L, 4L, 2L,
>>> 12L, 8L, 7L, 5L, 4L, 2L, 12L, 8L, 7L, 5L, 4L, 2L, 12L, 8L, 7L
>>> ), .Label = c("", "Chemicals and related\\n products imports",
>>> "Coal export", "Gold imports", "Gold & silver imports", "Iron ore
>> export",
>>> "Iron & steel imports", "Metaliferrous ores & metal scrap imports",
>>> "Mica export", "Ores & minerals\\nexport", "Other ores &\\nminerals
>>> export",
>>> "Pearls precious &\\n semiprecious stones imports", "Processed
>> minerals\\n
>>> export"
>>> ), class = "factor"), Units = structure(c(2L, 2L, 2L, 2L, 2L,
>>> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
>>> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,  
>>> 2L), .Label =
>> c("",
>>> "Rs.crore"), class = "factor"), Expression = structure(c(2L,
>>> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
>>> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
>>> 2L, 2L, 2L), .Label = c("", "Ival"), class = "factor"), time = c(7,
>>> 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10,
>>> 10, 10, 10, 11, 11, 11, 11, 11, 11, 1, 1, 1, 1, 1, 1), X03 =  
>>> c(66170.46,
>>> 65337.72, 62669.86, 33870.17, 36779.35, 27133.25, 71829.14,  
>>> 67226.04,
>>> 75086.89, 29505.61, 31750.99, 32961.26, 104786.39, 95323.8,  
>>> 134276.63,
>>> 76263, 36363.61, 41500.36, 140440.36, 135877.91, 111269.69,  
>>> 76678.27,
>>> 36449.89, 36808.06, 162253.77, 154346.72, 124895.76, 142437.03,
>>> 42872.16, 43881.85, 109096.024, 103622.438, 101639.766, 71750.816,
>>> 36843.2, 36456.956), id = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
>>> 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L,
>>> 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L)), row.names = c("1.7",
>>> "2.7", "3.7", "4.7", "5.7", "6.7", "1.8", "2.8", "3.8", "4.8",
>>> "5.8", "6.8", "1.9", "2.9", "3.9", "4.9", "5.9", "6.9", "1.10",
>>> "2.10", "3.10", "4.10", "5.10", "6.10", "1.11", "2.11", "3.11",
>>> "4.11", "5.11", "6.11", "1.1", "2.1", "3.1", "4.1", "5.1", "6.1"
>>> ), .Names = c("Indicator", "Units", "Expression", "time", "X03",
>>> "id"), class = "data.frame", reshapeLong = structure(list(varying =
>>> structure(list(
>>>   X03 = c("X03.07", "X03.08", "X03.09", "X03.10", "X03.11",
>>>   "X03.1")), .Names = "X03", v.names = "X03", times = c(7,
>>> 8, 9, 10, 11, 1)), v.names = "X03", idvar = "id", timevar = "time"),
>> .Names
>>> = c("varying",
>>> "v.names", "idvar", "timevar")))
>>>>
>>>
>>> On which I want to run
>>>
>>> barchart(X03/1000~time|Indicator,
>>>        data=imports[which(imports$time!=1),],
>>>        horiz=F,
>>>        scales=list(x=list(rot=45,labels=paste("Mar",2007:2011))),
>>>        par.strip.text=list(lineheight=1,lines=2))
>>>
>>> Many thanks,
>>> Ashim.
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list