plotmeans legends formatting

3.2k views Asked by At

I am plotting means of grouped data and I'm having trouble getting the legends to be right. The text is so large that one can only see the names of two groups, not all four. I have spent a long time trying to use cex-like commands to change the size, but it doesn't work. I have tried rotating them with las=3, but it doesn't work.

enter image description here

I cannot share the data, but the code is here:

plot.question = function(number){
  #which question to plot? get ID
  question = names(sorted.by.n)[number]
  #the formula
  form = paste0("DF.scored.g.scale ~ ",question)
  #fit it to data
  fit = lm(form, DF.merged.g)
  #get ANOVA results
  fit.anova = anova(fit)
  #get ANOVA p value
  p.value = round(fit.anova[[5]][2],4) #p value
  #plot it
  plotmeans(as.formula(form), DF.merged.g,
            ylab = "4 g-items sumscore",
            xlab = "Answer",
            main = paste0(questions.unique[question,"text"],"\nANOVA p=",p.value),
            cex.main = .8,
            cex.axis = .8,
            cex.lab = .8,
            cex.sub = .8,
            las=3,) #size of main title
}

Preferably, I'd like to simply make the text smaller, so it can fit. Alternatively, I'd like to rotate it so it can fit (perhaps along with a margin change). If not what else?

One can suppress the legends with xaxt="n", but then one has to add them some other way. Can it really not be done within the plotmeans() function?

1

There are 1 answers

5
LyzandeR On BEST ANSWER

Well I tried many things and this was the only thing that worked. Apparently plotmeans() creates a plot that you cannot modify in any way. The only thing I was able to do is to overlay text as a new only-text-plot on top of the plotmeans plot.

myfactor <- factor(rep(c('cat1','cat2','cat3'),20)) #make a factor

mynum <- runif(60) #make a numeric field

plotmeans(mynum ~ myfactor,xaxt='n') #plot them

labs <- paste(names(table(myfactor)), "") #make the names

par(new=T) #create new plot

a<-rev(as.numeric(unique(myfactor))) #count the unique factors to make a vector of their numbers to serve as the positions on the x axis

text(cex=1, x=a, y=0.2, labs, xpd=TRUE, srt=35) #insert the text on the graph.
#here you need to modify y according to your data to find the best place to plot them. 
#In my case x=c(1,2,3) because I have 3 categories and y=0.2 
#because this is the lowest value of the y axis. The srt argument rotates the text.

enter image description here

You should probably be able to either fix the y axis to have standard values and then use the minimum of that number in the y argument of the text function to make a generic function, or calculate the min value of the y axis each time.

Hope that helps!