0

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?

4

1 回答 1

1

好吧,我尝试了很多东西,这是唯一有效的东西。显然 plotmeans() 创建了一个您无法以任何方式修改的图。我唯一能做的就是在 plotmeans 图的顶部将文本作为新的 only-text-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.

在此处输入图像描述

您可能应该能够将 y 轴固定为具有标准值,然后在文本函数的 y 参数中使用该数字的最小值来制作通用函数,或者每次计算 y 轴的最小值。

希望有帮助!

于 2014-11-30T13:33:25.580 回答