[R] Automatically adjust text size in plot
Greg Snow
Greg.Snow at imail.org
Sat Oct 25 06:47:14 CEST 2008
You can use strheight and strwidth to find the height and width of a string in user coordinates, current font, cex=1. From that you can figure out how much to change the size to fit within the area of interest. You may also want to look at the textplot function in the gplots package. It adjusts text to fit a plot, looking at the code to see how that function does it may give you more ideas,
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Gustaf Rydevik
> Sent: Friday, October 24, 2008 9:29 AM
> To: r-help at r-project.org
> Subject: [R] Automatically adjust text size in plot
>
> Hi all,
>
> I'm writing a function that will automatically generate a report based
> on answers to a questionnaire. The exact questions and answers to the
> questionnaire can vary. One of the question types is in a "matrix"
> format, where the agreement to several statements can be indicated on
> a scale.
>
> I'm planning to plot this on a multilevel barplot, and only labeling
> each "bar column" once. However, I'm stuck as to how I should adjust
> text size and wrapping to fit to each column.
>
> Here's an example of what I mean:
>
> barnames<-c("I agree completely", "I agree", "I partly agree", "I do
> not agree", "I really hate this stupid question, don't you?")
> answers<-
> data.frame(question=paste("Q",1:5,sep=""),S1=sample(1:100,5),S2=sample(
> 1:100,5),S3=sample(1:100,5),S4=sample(1:100,5),S5=sample(1:100,5))
> Width<-50
> Cex<-1.5
>
> par(mfrow=c(nrow(answers)+1,1),mar=c(0,1,1,1))
> plot.new()
> plot.window(xlim=c(0,1),ylim=c(0,1))
> barnames.plot<-
> do.call("c",lapply(barnames,function(x)paste(strwrap(x,Width),collapse=
> "\n")))
> text(barnames.plot,x= (seq.int(0, 1, length.out =
> length(barnames)+1)-0.5/length(barnames))[-1],y=0.5,cex=Cex)
> for(i in 1:nrow(answers)){
> barheight<-rep(0,length(barnames))
> barheight[as.numeric(names(subQ.tables[[i]]))]<-subQ.tables[[i]]
> barplot(barheight,space=0)
> }
>
> The question is, how do I figure out the appropriate "Width" and
> "Cex" parameters as a function of barnames?
> That is, with varying text lengths of the barnames, varying number of
> alternatives etc, and independent on which device type is used?
> strwrap uses "column" as units, and I can't really figure out how to
> convert that to graph units. Same goes for cex.
>
> Many thanks in advance,
>
> Gustaf
>
>
> PS: As an alternative, if someone could come up with a better way do
> display this type of data, I'd be all ears. I'm not too happy with my
> current solution....
>
>
> --
> Gustaf Rydevik, M.Sci.
> tel: +46(0)703 051 451
> address:Essingetorget 40,112 66 Stockholm, SE
> skype:gustaf_rydevik
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list