2

In ggplot2, it is easy to use element_text with argument size=6 to set the font size to 6 pt (For text, size has a unit of pt). However, we cannot use element_line with argument size=0.5 to set the line width to 0.5 pt (For line, size has no unit). So how to solve this problem?

In grid package, I meet with the similar question. The code is as following:

library(grid)
grid.rect(width=unit(5, "cm"), height=unit(5, "cm"), gp=gpar(lwd=unit(2, "cm")))

and the result is: enter image description here

Obviously, the line width is not 2 cm comparing with the width or height of the rectangle.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Tiger
  • 215
  • 2
  • 8
  • For the first question, I have got the answer from [https://github.com/yihui/tikzDevice/issues/68](https://github.com/yihui/tikzDevice/issues/68). The correct lwd unit should be 72.27/96 (96 pixels in R is 1 inch, which is 72.27 points in ggplot figure). So I can set the line width to 0.5 pt in ggplot2 by `size=72.27/96*0.5`. However, the second question is still a problem. – Tiger Feb 02 '16 at 07:28

1 Answers1

1

a lwd unit is obviously 1/96 of an inch for the pdf device, and it extends symmetrically on either side of the line

grid.newpage()
grid.rect(width=unit(1, "cm")+unit(1,"mm"), 
          height=unit(1, "cm")+unit(1,"mm"), 
          gp=gpar(lwd=96/2.54, alpha=0.5, linejoin="mitre",linejoin=1))

enter image description here

However, in windows system, the result is:

enter image description here

Tiger
  • 215
  • 2
  • 8
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Is it different in windows system with in Mac system? – Tiger Feb 05 '16 at 02:31
  • `?par` mentions that the interpretation of lwd is device-specific, so you may have better luck with a pdf device than this interactive device. – baptiste Feb 05 '16 at 04:00