
这是一个简单的问题示例(我使用RStudio):
plot.new()plot.window(xlim=c(0,5),ylim=c(0,5))rect(1,1,4,4)text(2,2,"This is a long text that should fit in the rectangle")
问题是:如何在矩形中自动拟合变长字符串,如下图所示?
plot.new()plot.window(xlim=c(0,5)) # Window covers whole plot spacerect(1,4)text(2.5,3,"This is a long text")text(2.5,2.5,"that should fit")text(2.5,"in the rectangle")解决方法 结合strwIDth以获得绘图上的实际宽度并使用strwrap来包装文本.它并不完美(文本应该用像素宽度而不是字符数包裹),但在大多数情况下应该这样做.
plot.new()plot.window(c(-1,1),c(-1,1))rectangleWIDth <- .6s <- "This is a long text that should fit in the rectangle"n <- nchar(s)for(i in n:1) { wrappeds <- paste0(strwrap(s,i),collapse = "\n") if(strwIDth(wrappeds) < rectangleWIDth) break}textHeight <- strheight(wrappeds) text(0,wrappeds)rect(-rectangleWIDth/2,-textHeight/2,rectangleWIDth/2,textHeight/2) # would look better with a margin added 总结 以上是内存溢出为你收集整理的如何在R中的矩形中包装文本全部内容,希望文章能够帮你解决如何在R中的矩形中包装文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)