2016-07-22 510 views
0

使用ggplot“主题”命令的添加,可以使用R中的Likert包更改图中的字体大小。但是,当条形图和直方图同时绘制在一起时,这些更改不会影响生成的图形。有没有办法修改字体大小,并同时绘制条形图和直方图?例如,下面的代码将成功地修改轴文本:李克特包R

likert.bar.plot(items, legend.position = "none") + 
theme(text = element_text(size = rel(6), colour = "red"), 
     axis.text.y = element_text(colour = "blue", 
            family = "Courier"))` 

...但下面的代码不会:

plot(items, include.histogram=T, legend.position = "none") + 
theme(text = element_text(size = rel(6), colour = "red"), 
     axis.text.y = element_text(colour = "blue", 
            family = "Courier"))` 

这个问题解释的 How do I change the text font, size and colour of all the different texts in the R package, Likert?

回答

1

的基础知识问题是plot.likert正在使用grid pacakge来组合图。这意味着它不会返回任何可以修改的对象(自己尝试,保存输出 - 它返回NULL)。但是,只打印一个打印图时,它将返回类别为ggplot(等等)的打印图,以允许通过theme和其他ggplot函数处理它。

如果您希望在单个函数调用中工作,您可能需要自己编辑plot.likert的代码。或者,可能更强大/更灵活:您可能需要自己查看grid包以组合图。

如,:

data(pisaitems) 
items29 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST25Q'] 
names(items29) <- c("Magazines", "Comic books", "Fiction", 
        "Non-fiction books", "Newspapers") 
l29 <- likert(items29) 



a <- 
    likert.bar.plot(l29, legend.position = "none") + 
    theme(text = element_text(size = rel(6), colour = "red"), 
     axis.text.y = element_text(colour = "blue", 
            family = "Courier")) 

b <- 
    likert.histogram.plot(l29, legend.position = "none") + 
    theme(text = element_text(size = rel(6), colour = "red"), 
     axis.text.y = element_text(colour = "blue", 
            family = "Courier")) + 
    theme(axis.text.y = element_blank()) 


library(gridExtra) 
grid.arrange(a,b,widths=c(2,1)) 

(注一MWE列入这样别人也能运行的代码。)

(感谢@ eipi10为更清晰的代码把它们结合在一起)

enter image description here

+0

您可以使用'gridExtra'包进行布局,这会让事情变得更简单:'library(gridExtra); grid.arrange(A,B,宽度= C(2,1))'。 – eipi10