I have a long text that I want to plot to an image in R, the image size 300 x 200 pixel. The actual text will vary.
Example Image
Example Code
library(raster)
myJPG <- stack("images/1.jpg") # Image with 300 x 200 pixel size
plotRGB(myJPG)
vt <- rep(1:100)
vt <- paste(vt, collapse = ' ')
text(x = 150, y = 70,
labels = vt,
adj = c(0.5,0.5),
cex = 1,
col = "white")
Example Result.
What I want to ask How to cut the text in labels automatically, and adjust it so all the text can be shown in the image?
You can insert \n
into your character vector vt
to break a line. One way to do this automatically is with strwrap
.
library(raster)
myJPG <- stack("~/Documents/1.jpg") # Image with 300 x 200 pixel size
plotRGB(myJPG)
vt <- rep(1:100)
vt <- paste(vt, collapse = ' ')
vt2 <- paste(strwrap(
x = vt,
width = 70),
collapse = "\n")
text(x = 150, y = 70,
labels = vt2,
adj = c(0.5,0.5),
cex = 1,
col = "white")
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.