2016-07-22 531 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