- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将文本标签包裹在一个矩形中。
这是一个带有标签的简单图:
x = mtcars$wt
y = mtcars$mpg
l = rownames(mtcars)
plot(x, y)
text(x, y, l, adj = .5) # i.e., the default
我可以像这样成功地使用 strwidth
和 strheight
来完成包装:
delx = strwidth(l, cex = par('cex'))
dely = strheight(l, cex = par('cex'))
rect(x - .5*delx, y - .5*dely, x + .5*delx, y + .5*dely)
注意每个标签左右两侧的空白“间隙”。现在,这很好,但在尝试考虑情节中的 adj
时会导致问题:
adj = c(0, .5)
plot(x, y)
text(x, y, l, adj = adj)
rect(
x - adj[1L]*delx, y - adj[2L]*dely,
x + (1-adj[1L])*delx, y + (1-adj[2L])*dely
)
盒子的方向调整得很好,但它看起来是一个准确的转换,我需要进一步考虑创建“缓冲区”宽度的因素。
那是什么?我还没有在 ?par 或 ?strwidth 中看到任何内容。
最佳答案
没有内部宽度缓冲区。只是你需要在当前设备的上下文中计算strwidth
。当您在绘制绘图后调整图形设备大小时,矩形将调整大小但文本不会。这将导致文本周围的明显空间随窗口大小而变化。
只要您在绘制绘图之前仔细指定设备尺寸,并重新计算该设备上文本的 strwidth
,您的代码就应该在文本周围生成舒适的框。您也可以在不影响居中的情况下添加固定边距,并且这些都不需要尚不可用的信息。
这是一个用于说明目的的函数,它允许完全控制文本周围的框:
michael_plot <- function(width = 9, height = 6, adj = c(0.5, 0.5), margin = 0)
{
dev.new(width = width, height = height, unit = "in", noRStudioGD = TRUE)
plot(x, y)
delx <- strwidth(l, cex = par('cex'))
xmarg <- strwidth("M", cex = par('cex')) * margin
dely <- strheight(l, cex = par('cex'))
ymarg <- strheight("M", cex = par('cex')) * margin / 2
text(x, y, l, adj = adj)
rect(x - adj[1] * delx - xmarg,
y - adj[2] * dely - ymarg,
x + (1 - adj[1]) * delx + xmarg,
y + (1 - adj[2]) * dely + ymarg)
}
从默认开始:
michael_plot()
盒子非常合适;事实上,它们离文本太近了,因此我们应该添加边距以使其更易读:
michael_plot(margin = 1)
但重要的是,我们可以使用 adj
安全地移动它们,同时保持它们居中:
michael_plot(margin = 1, adj = c(0, 0.5))
关于r - 什么控制 strwidth 中的表观缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61651361/
我正在尝试将文本标签包裹在一个矩形中。 这是一个带有标签的简单图: x = mtcars$wt y = mtcars$mpg l = rownames(mtcars) plot(x, y) text(
如何在不打开绘图窗口的情况下使用 strwidth,就像默认情况下那样? 因此 strwidth('this is cool', font = 12, units = 'in') 调用 plot.ne
我正在 R 2.15.1 中使用 tm 和 wordcloud 包。我正在尝试制作文字云这是代码: maruti_tweets = userTimeline("Maruti_suzuki", n=10
我在 R 2.15.1 中使用 tm 和 wordcloud 包。 我正在尝试从 DTM 制作词云。这是代码: library(wordcloud) thedtmsparse = inspect(sp
我是一名优秀的程序员,十分优秀!