0

I am sure this is not new for the R community, but is new to me and can't find a clear answer. Assuming this example:

plot(1:10, xlab="", xaxt="n") # supress OX axis
title(xlab="How can I use cm?", line=2.5)
axis(side=1, at=1:10, line=0.2) 

enter image description here

Here I used line argument in function title() to place a label at 2,5 lines of text "outwards from the plot edge" (as described in ?title help). Is there any argument that can take cm, or a way to use cm? Also, how can I find out how many cm does a line of text contains (if there is no other way around)?

Would also be great to know/set the margins in cm and not only like par("mar") [lines of text] or par("mai") [inches]. Is there a way to do that?

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68
  • These things are completely dependent on the device size. Please take a look at `?grconvertx` and [this question](http://stackoverflow.com/q/30765866/1623354). You cannot explicitly get centimeters, but you could convert from inches very easily. – dayne Jul 24 '16 at 19:28
  • As for setting the margins, just convert the centimeters to inches and use `mai` like you referenced. – dayne Jul 24 '16 at 19:30

1 Answers1

1

Using the line2user function from this answer you can convert centimeters to a "line" then convert the line to user coordinates and add things to the plot using xpd = TRUE:

cm2line <- function(x) {
  lh <- par('cin')[2] * par('cex') * par('lheight')
  inch <- x/2.54
  inch/lh
}

par(mai = rep(5/2.54, 4))
plot.new() 
box()
mtext("hello", side = 3, line = cm2line(2))
abline(h = line2user(cm2line(1:5), side = 4), xpd = TRUE)
abline(h = line2user(cm2line(1:5), side = 1), xpd = TRUE)
abline(v = line2user(cm2line(1:5), side = 2), xpd = TRUE)
abline(v = line2user(cm2line(1:5), side = 3), xpd = TRUE)

enter image description here

Community
  • 1
  • 1
dayne
  • 7,504
  • 6
  • 38
  • 56
  • Any idea how to implement a similar thing for `mgp` parameter? For example in positioning an axis like `axis(side=1, at=1:10, line=cm2line(1), mgp=c(0,0,0))`. Seems that even if I set `mgp` to zero, the top of the digits don't touch the axis line. Seems that `mgp` is in `mex` units, which are multiple of `csi` (`csi` = `par("cin")[2]`). All this seems confusing to me so any help would be appreciated. – Valentin_Ștefan Jul 24 '16 at 22:07
  • You might need to ask another question. I'm not sure what you mean. Is there any reason you need things spaced in particular locations by centimeter? – dayne Jul 24 '16 at 22:11
  • Hi @dayne, I actually need to set the axis without ticks (`tick = FALSE`) and then adjust the 'space' that remains between the axis labels and the axis line. Seems that I can adjust that space with the `mgp` parameter as I mentioned. What I need to understand is how can I convert the `mex` units of `mpg` in cm (does it make sense?) – Valentin_Ștefan Jul 24 '16 at 22:23
  • I would just use `text` and place the labels with a combination of the functions I provided and `axTicks()` – dayne Jul 25 '16 at 02:25
  • not sure if I get it correctly, but `text()` function accepts numeric vectors of coordinates, so I can't use the `cm2line()` function. `mtext()`, `axis()` and `title()` use the `line`argument and there I can put something like `line=cm2line()`, but this doesn't work with `text()` function unfortunately. – Valentin_Ștefan Jul 25 '16 at 08:04
  • 1
    IMO, you need to try a little harder with the information you've been given. The comment section is not a place to ask successive questions. The key here is combining `cm2line` and `line2user`. Try this: `par(mai = rep(5/2.54, 4)); plot.new(); text(x = axTicks(side = 1), y = line2user(cm2line(2), side = 1), labels = axTicks(side = 1), xpd = TRUE)` – dayne Jul 25 '16 at 14:54