2010-11-22 219 views
102

我很困惑。增加劇情的標題,標籤和其他地方的文字字體大小的正確方法是什麼?如何在R圖中增加字體大小?

例如

x <- rnorm(100) 
hist(x, xlim=range(x), xlab= "Variable Label", 
    ylab="density", main="Title of plot", prob=TRUE, ps=30) 

ps參數不改變字體大小(但R中幫助說爲?par這是「文本(但不是符號的點尺寸)」。

也有可能分開更改字體大小從繪圖功能,如hist

+0

[如何更改R圖的標籤大小]的可能重複(https://stackoverflow.com/questions/13046323/how-to-change-the-label-size-of-an-r-plot ) – 2017-06-01 08:47:26

回答

103

你想要的東西像cex=1.5參數縮放字體150百分比,但看到help(par)因爲也有cex.labcex.axis ...

+1

謝謝! 「ps = 1.5」有什麼區別? – Tim 2010-11-22 02:44:47

+2

爲什麼cex = 1.5不起作用?但是必須根據cex.lab,cex.axis,cex.main來指定每個部分? cex = 1.5是什麼? – Tim 2010-11-22 02:49:54

+2

你讀過關於'ps'的幫助(par)嗎?據我所知,似乎與文字無關。 – 2010-11-22 02:51:30

96

因此,總結已有的討論,增加

cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5

你的情節,其中1.5可能是2,3等,以及1的值是默認會增加字體大小。

x <- rnorm(100) 

CEX不會改變的東西

hist(x, xlim=range(x), 
    xlab= "Variable Lable", ylab="density", main="Title of plot", prob=TRUE) 

hist(x, xlim=range(x), 
    xlab= "Variable Lable", ylab="density", main="Title of plot", prob=TRUE, 
    cex=1.5) 

enter image description here

添加cex.lab = 1.5,cex.axis = 1.5,cex.main = 1.5,cex.sub = 1.5

hist(x, xlim=range(x), 
    xlab= "Variable Lable", ylab="density", main="Title of plot", prob=TRUE, 
    cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5) 

enter image description here

+2

順便說一句,如果你想修改條形圖中的座標軸(比如randomForest或GBM中的變量重要性圖),你需要使用'cex.names'(如果你是一個人,直立的位置,你可能也想'las = 2') – geneorama 2016-01-12 20:13:00

20

請注意,「cex」確實會在文字製作情節時改變內容。例如,凝聚層次聚類的情節:

library(cluster) 
data(votes.repub) 
agn1 <- agnes(votes.repub, metric = "manhattan", stand = TRUE) 
plot(agn1, which.plots=2) 

會產生一個情節與正常大小的文字:

enter image description here

plot(agn1, which.plots=2, cex=0.5)會產生這樣一條:

enter image description here

+0

也可以在'faces2'中工作(來自[Chernoff faces](http://cran.r-project.org/web/packages/TeachingDemos/index.html)) – Galled 2015-01-09 01:32:24

+0

在我的示例中,我必須將_cex_ ** not **應用於繪圖,但直接爲效果應用內部對象:'plot(ci(roc(data $ a,data $ b,auc = TRUE,of =「auc」, print.auc = TRUE,print.auc.cex = 1.5,plot = TRUE),of =「thresholds」,thresholds =「best」)))' – Tapper 2017-09-10 18:26:00

15

通過試驗和錯誤,我確定需要設置以下字體大小:

  1. cexhist()中不起作用。對於標籤,使用cex.axis作爲軸上的數字,cex.lab
  2. cex也不在axis()工作。對軸上的數字使用cex.axis
  3. 代替使用hist()設置標籤,您可以使用mtext()進行設置。您可以使用cex來設置字體大小,但使用值1 實際上會將字體設置爲默認值的1.5倍!您需要使用cex=2/3才能獲取默認的字體大小。至少,使用PDF輸出的情況在Mac OS X的R 3.0.2下是這種情況。
  4. 您可以在pdf()中使用pointsize更改PDF輸出的默認字體大小。

我想如果期望R(a)實際上做它的文檔應該做的事情,(b)以預期的方式行事,那就太合乎邏輯了。

1

如果您想設置標籤時增加直方圖的標籤的字體= TRUE

bp=hist(values, labels = FALSE, 
main='Histogram', 
xlab='xlab',ylab='ylab', cex.main=2, cex.lab=2,cex.axis=2) 

text(x=bp$mids, y=bp$counts, labels=bp$counts ,cex=2,pos=3) 
1

我碰到這個時候我想使軸標籤較小,但離開一切同尺寸。爲我工作的命令是:

par(cex.axis=0.5) 

在plot命令之前。請記住:

par(cex.axis=1.0) 

確定字體回到默認大小後的情節。