[R] alignment problem for italicised legend text in pdf output from a barplot

David Winsemius dwinsemius at comcast.net
Wed Aug 21 02:59:38 CEST 2013


On Aug 20, 2013, at 2:28 PM, Nixon, John wrote:

> A minimal R source code file producing this is:
> 
> data=c(1,2)
> pdf(file="error.pdf")
> barplot(data)
> legend(1.5,c(expression(italic("C.sativa"),italic("B.rapa"))),horiz=TRUE)
> dev.off()
> 
> The problem is that the two italicised strings are not aligned in the pdf output, with the string on the left being lower than the string on the right by size of about two dots. Removing the calls to expression and italic produces correct alignment, but italic is required in the final figure.
> 

It's being caused by the descender of the letter "p". There are no letters with descenders in the first expression. `phantom` lets you add space and apparently does so both horizonatly which I how I've used it before and vertically as this demonstrates.  Do this:

data=c(1,2)
pdf(file="error.pdf")
barplot(data)
legend(1.5,c(expression( phantom("p")*italic("C.sativa"), italic("B.rapa"))),horiz=TRUE)
dev.off()

-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list