Donald Boyd
2009-Aug-25 03:04 UTC
[R] allowing line wrap for long strip text in xyplot (lattice)
Hi. Am brand new to R and to mailing lists - have never posted anywhere before, so hope I do this right. Am using R 2.9.1 with lattice graphics (just installed, fully up to date). Am doing trellis xyplot with y (emp=employment), x (yearmo=a time measure) and conditioning variable (indf - factor describing industry) -- i.e., (emp ~ yearmo | indf), where all three variables are in a dataframe. The conditioning variable is a factor with a long text description (e.g., "Offices of bank holding companies and of other holding companies (60551112)" constructed from a much-less-descriptive industry code - in this case, 60551112). That long text goes into the strip text above each panel. With default settings the text for many of the factor levels is too long for the strip area and I simply see some of it but not all of it. I would like to be able to see the full text description, which means I would like to allow it to wrap over several lines in the strip area - say 2 or 3 lines. Note that the text itself does NOT have any embedded line-wrap characters (\n) and I would like to avoid writing something to guess where to insert them into each of more than 1,000 levels of the factor. I suspect that xyplot, perhaps through par.strip.text, has some simple way of allowing it to do intelligent line wrap. I have searched high and low, but have not figured this out: - I have read the lattice documentation but haven't quite figured out from that what I should do - I have tried using lines=2 in par.strip.text and lineheight=2 but the former appears to change the height of the strip area without forcing line wrap, and the latter as I understand it controls spacing between lines - I have tried workarounds like using cex=0.2 in par.strip.text to make the text really small, but it gets hard to read; and I have tried abbreviate=TRUE in par.strip.text but I would rather have full titles I am sure there is a really simple solution to this but I can't figure it out. If anyone could tell me how I can get long strip titles in xyplot to wrap lines (without inserting "\n"s in them) I would appreciate it greatly. Thanks! Don Boyd To be more concrete, here is sample code, stripped to its essentials. indf is the factor that has very long text for some of its levels: xyplot(emp ~ yearmo | indf, data = subset(empall, subset=(yearmo>=as.Date("2006/12/01"))), scales = list(relation="free", x = list(rot = 45)), type = "b", par.strip.text = list(lines=2, cex=.6), layout=c(4,3), xlab=NULL, # put reference lines on the plot panel=function(...){ panel.xyplot(...) panel.abline(h=seq(-10,5,by=1)) } ) [[alternative HTML version deleted]]
Donald Boyd
2009-Aug-25 03:19 UTC
[R] allowing line wrap for long strip text in xyplot (lattice)
Hi. Am brand new to R and to mailing lists - have never posted anywhere before, so hope I do this right. Am using R 2.9.1 with lattice graphics (just installed, fully up to date). Am doing trellis xyplot with y (emp=employment), x (yearmo=a time measure) and conditioning variable (indf - factor describing industry) -- i.e., (emp ~ yearmo | indf), where all three variables are in a dataframe. The conditioning variable is a factor with a long text description (e.g., "Offices of bank holding companies and of other holding companies (60551112)" constructed from a much-less-descriptive industry code - in this case, 60551112). That long text goes into the strip text above each panel. With default settings the text for many of the factor levels is too long for the strip area and I simply see some of it but not all of it. I would like to be able to see the full text description, which means I would like to allow it to wrap over several lines in the strip area - say 2 or 3 lines. Note that the text itself does NOT have any embedded line-wrap characters (\n) and I would like to avoid writing something to guess where to insert them into each of more than 1,000 levels of the factor. I suspect that xyplot, perhaps through par.strip.text, has some simple way of allowing it to do intelligent line wrap. I have searched high and low, but have not figured this out: - I have read the lattice documentation but haven't quite figured out from that what I should do - I have tried using lines=2 in par.strip.text and lineheight=2 but the former appears to change the height of the strip area without forcing line wrap, and the latter as I understand it controls spacing between lines - I have tried workarounds like using cex=0.2 in par.strip.text to make the text really small, but it gets hard to read; and I have tried abbreviate=TRUE in par.strip.text but I would rather have full titles I am sure there is a really simple solution to this but I can't figure it out. If anyone could tell me how I can get long strip titles in xyplot to wrap lines (without inserting "\n"s in them) I would appreciate it greatly. Thanks! Don Boyd To be more concrete, here is sample code, stripped to its essentials. indf is the factor that has very long text for some of its levels: xyplot(emp ~ yearmo | indf, data = subset(empall, subset=(yearmo>=as.Date("2006/12/01"))), scales = list(relation="free", x = list(rot = 45)), type = "b", par.strip.text = list(lines=2, cex=.6), layout=c(4,3), xlab=NULL, # put reference lines on the plot panel=function(...){ panel.xyplot(...) panel.abline(h=seq(-10,5,by=1)) } ) [[alternative HTML version deleted]]
Deepayan Sarkar
2009-Aug-25 21:07 UTC
[R] allowing line wrap for long strip text in xyplot (lattice)
On Mon, Aug 24, 2009 at 8:04 PM, Donald Boyd<boydd at hughes.net> wrote:> Hi. Am brand new to R and to mailing lists - have never posted anywhere > before, so hope I do this right. > > Am using R 2.9.1 with lattice graphics (just installed, fully up to date). > > Am doing trellis xyplot with y (emp=employment), x (yearmo=a time measure) > and conditioning variable (indf - factor describing industry) -- i.e., (emp > ~ yearmo | indf), where all three variables are in a dataframe. The > conditioning variable is a factor with a long text description (e.g., > "Offices of bank holding companies and of other holding companies > (60551112)" constructed from a much-less-descriptive industry code - in this > case, 60551112). That long text goes into the strip text above each panel. > With default settings the text for many of the factor levels is too long for > the strip area and I simply see some of it but not all of it. I would like > to be able to see the full text description, which means I would like to > allow it to wrap over several lines in the strip area - say 2 or 3 lines. > Note that the text itself does NOT have any embedded line-wrap characters > (\n) and I would like to avoid writing something to guess where to insert > them into each of more than 1,000 levels of the factor. I suspect that > xyplot, perhaps through par.strip.text, has some simple way of allowing it > to do intelligent line wrap. I have searched high and low, but have not > figured this out: > - I have read the lattice documentation but haven't quite figured out from > that what I should do > - I have tried using lines=2 in par.strip.text and lineheight=2 but the > former appears to change the height of the strip area without forcing line > wrap, and the latter as I understand it controls spacing between lines > - I have tried workarounds like using cex=0.2 in par.strip.text to make the > text really small, but it gets hard to read; and I have tried > abbreviate=TRUE in par.strip.text but I would rather have full titles > > I am sure there is a really simple solution to this but I can't figure it > out. If anyone could tell me how I can get long strip titles in xyplot to > wrap lines (without inserting "\n"s in them) I would appreciate it greatly.Lattice has no facilities for automatic wrapping. However, 1. see ?strwrap to get your strings broken, and ?paste to join them with "\n"-s. 2. You will need to manually find a suitable 'width' for the strwrap call, depending on your device size and layout 3. You will also need to use lines= in par.strip.text, but you should be able to figure out the appropriate value from the results of strwrap. -Deepayan