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)
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?
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.