I'm plotting a factor on the x-axis, so each point is labeled with the name of the factor. Since the text would overlap if plotted horizontally, I'm plotting it all vertically. However, I then will occasionally run into issues with the x-axis margin not being large enough. So, my question: Is there a way to determine how long a text string will be when plotted (in, say, inches)?
I believe you are looking for strwidth
in the graphics
package.
To use it, first plot your graph. Then simply call strwidth("your string")
eg:
library(graphics)
plot(1:5)
strwidth("hello")
# [1] 0.2640991
strwidth(" a longer string\n with multiple lines asdfsdfjkdsrew")
# [1] 1.875732
strwidth("hello", units="inches")
# [1] 0.3521322
strwidth("hello", units="figure")
# [1] 0.05030459
If you are plotting rotated text, It's possible you may want to use strheight
(analogous functionality)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With