Sto tentando di eseguire un'etichettatura di plottaggio non convenzionale e vorrei un modo per convertire il parametro in mtext
e axis
in coordinate utente.Ottieni posizioni linea di margine (mgp) nelle coordinate utente
In altre parole, mi piacerebbe convertire i valori in par()$mgp
in coordinate utente.
Questo illustra il problema:
setup_plot <- function() {
par(mar = c(2, 10, 2, 2), oma = rep(2, 4))
plot.new()
plot.window(xlim = c(0, 1), ylim = c(0, 1))
box(which = "plot", lwd = 2, col = "gray40")
box(which = "figure", lwd = 2, col = "darkred")
box(which = "outer", lwd = 2, col = "darkgreen")
text(x = 0.5, y = 0.5,
labels = "Plot Region",
col = "gray40", font = 2)
mtext(side = 3, text = "Figure region", line = 0.5, col = "darkred", font = 2)
mtext(side = 3, text = "Device region", line = 2.5, col = "darkgreen", font = 2)
for (i in 0:9) {
mtext(side = 2, col = "darkred", text = paste0("Line", i), line = i)
}
}
Ho provato due diversi approcci.
## Try one approach where a line is the string height of "M"
setup_plot()
xline = strheight("M", units = "user")
abline(v = par()$usr[1] - 0:9*xline,
xpd = TRUE, lty = "dashed", col = "gray40")
## Try a second approach defining a line using par()$mai & par()$mar
setup_plot()
xline = abs(grconvertX(unique(par()$mai/par()$mar), "inches", "user"))
abline(v = par()$usr[1] - 0:9*xline,
xpd = TRUE, lty = "dashed", col = "gray40")
Come si ottiene la posizione retta in coordinate utente?
NOTA: Le cifre qui sono 4 pollici per 6 pollici. Cambiare la dimensione dell'output cambia il modo in cui vengono disegnate le linee - che per me non ha senso.
Questa non è la risposta che state cercando, ma è qualcosa che si potrebbe usare se tutto il resto fallisce: 'mtext (lato = 2, col = "gray40", text = pasta (rep ("_", 45), collapse = ""), line = c (0: 9)) ' – Jota
Questo è un trucco intelligente, ma in realtà non mi interessa disegnare linee. Stavo solo usando le linee per illustrare che non so come ottenere le posizioni corrette. – dayne