[R] Automatically adjust text size in plot

Gustaf Rydevik gustaf.rydevik at gmail.com
Fri Oct 24 17:29:07 CEST 2008


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



More information about the R-help mailing list