Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine actual width of text in plot

Tags:

text

plot

margin

r

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)?

like image 684
random_forest_fanatic Avatar asked Dec 08 '12 23:12

random_forest_fanatic


Video Answer


1 Answers

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)

like image 177
Ricardo Saporta Avatar answered Sep 20 '22 13:09

Ricardo Saporta