在R中的绘图外绘图文本块
我想在绘图旁边放一个较长的文本,但格式有问题。这是实际绘图的代码:在R中的绘图外绘图文本块,r,text,plot,alignment,R,Text,Plot,Alignment,我想在绘图旁边放一个较长的文本,但格式有问题。这是实际绘图的代码: percentile.plot <- function(value = 3, rev = FALSE, col.fade = c("snow2","snow1", "snow"), box.lwd = 3, box.col="snow4", point.col= "black"){ x <-c(1:5) y <-
percentile.plot <- function(value = 3, rev = FALSE,
col.fade = c("snow2","snow1", "snow"),
box.lwd = 3, box.col="snow4", point.col= "black"){
x <-c(1:5)
y <- rep(1, 5)
colfunc <- colorRampPalette(col.fade)
if(rev){colors <- rev(colfunc(2000))} else { colors <- colfunc(2000) }
segm <- seq(0, 5, by = 0.005)
par(mar = c(0, 10, 0, 10))
plot(x, y, type = "n", bty="n", axes=F, ylab="", xlab="",
xlim=c(0,5), ylim=c(0,1), asp=1,
xaxs ="i", yaxs = "i")
segments(x0 = segm, y0 = 0, y1=1, x1 = segm, col= colors, lty=1.2)
segments(x0 = c(0:5), y0 = 0, y1=1, x1 = c(0:5), col= box.col, lwd=box.lwd)
segments(x0 = 0, y0 = c(0:1), y1=c(0:1), x1 = 5, col= box.col, lwd=box.lwd)
if (value >= .99) {value <- .99}
if (value < .01) {value <- .01}
value.trans <- value*5
points(x = value.trans, y = 0.5, pch=4, lwd=3, cex=1.3, col=point.col)
}
percentile.plot(0.9)
其结果是:
我想要的是绘图左侧的文本块,它在绘图左侧对齐,另一个在绘图右侧,也在左侧对齐。文本应该是我计算的图的每一端的描述
此外,我在不同的计算机(13英寸和27英寸)上尝试了这一方法,并不得不更改边距大小以获得相同的结果。有没有更好的方法将文本添加到绘图中?加上实际的情节应该比图片中的要大,但我无法正确地做到这一点 使用网格图形可能更容易
percentileGrob <- function(value = 3, ...){
x <-c(1:5)
y <- rep(1, 5)
g1 <- rasterGrob(t(colorRampPalette(blues9)(100)), width=unit(1,"npc"), height=unit(1,"npc"))
h <- 1/5
g2 <- rectGrob(x=seq(h/2, 1-h/2, by=h), width=h, gp=gpar(fill=NA))
g3 <- pointsGrob(seq(h/2, 1-h/2, by=h)[5],0.5, pch=4, gp=gpar(lwd=4))
grobTree(g1, g2, g3,vp=viewport(width=unit(1,"snpc"), height=unit(1/5,"snpc")))
}
text_left <- "I would like to put a text next to my graph but unfortunately I cannot get the formatting right. Either my margins are to big to knit my plot into an html or the text is not aligned as I want it to be. I am sure there is an easy solution but I haven’t found one yet."
library(gridExtra)
tl <- textGrob(paste(strwrap(text_left, 50), collapse="\n"), hjust=0, x=0)
tr <- textGrob(paste(strwrap(text_left, 50), collapse="\n"), hjust=0, x=0)
grid.arrange(tl, percentileGrob(), tr, widths=unit.c(grobWidth(tl), unit(1,"npc") - grobWidth(tl) - grobWidth(tr), grobWidth(tr)))
percentileGrob如果您希望每个文本块的大小与绘图相当,我建议使用layout
或par(mfrow(c(1,3))
并对每段文本和绘图使用不同的绘图区域。您可能希望在函数中删除par
,然后尝试类似库的方法(gplots);par(mar=rep(0,4));布局(矩阵(c(1,1,2,3,3),nrow=1));文本图(文本左,“右”,“中”);百分位图(0.9);文本图(文本左,“左”,“中”)
。
par(lheight = 0.5)
mtext(text_left, las = 1, side = 2, outer = TRUE, adj = 0, line= 5, cex = .8)
mtext(text_right, las = 1, side = 4, outer = TRUE, adj = 0, line= -8, cex = .8)
percentileGrob <- function(value = 3, ...){
x <-c(1:5)
y <- rep(1, 5)
g1 <- rasterGrob(t(colorRampPalette(blues9)(100)), width=unit(1,"npc"), height=unit(1,"npc"))
h <- 1/5
g2 <- rectGrob(x=seq(h/2, 1-h/2, by=h), width=h, gp=gpar(fill=NA))
g3 <- pointsGrob(seq(h/2, 1-h/2, by=h)[5],0.5, pch=4, gp=gpar(lwd=4))
grobTree(g1, g2, g3,vp=viewport(width=unit(1,"snpc"), height=unit(1/5,"snpc")))
}
text_left <- "I would like to put a text next to my graph but unfortunately I cannot get the formatting right. Either my margins are to big to knit my plot into an html or the text is not aligned as I want it to be. I am sure there is an easy solution but I haven’t found one yet."
library(gridExtra)
tl <- textGrob(paste(strwrap(text_left, 50), collapse="\n"), hjust=0, x=0)
tr <- textGrob(paste(strwrap(text_left, 50), collapse="\n"), hjust=0, x=0)
grid.arrange(tl, percentileGrob(), tr, widths=unit.c(grobWidth(tl), unit(1,"npc") - grobWidth(tl) - grobWidth(tr), grobWidth(tr)))