代码之家  ›  专栏  ›  技术社区  ›  Tim unnamed eng

如何在R中增加绘图中的字体大小?

  •  126
  • Tim unnamed eng  · 技术社区  · 14 年前

    我很困惑。在绘图的标题、标签和其他位置增加文本字体大小的正确方法是什么?

    例如

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

    这个 ps 参数不更改字体大小(但它在R Help中表示 ?par 这是为了“文本的点大小(但不是符号)”。

    也可以将更改字体大小与绘图功能分开,例如 hist

    7 回复  |  直到 8 年前
        1
  •  135
  •   Dirk is no longer here    14 年前

    你想要像 cex=1.5 缩放字体150%的参数。但是你看 help(par) 因为还有 cex.lab , cex.axis , ...

        2
  •  118
  •   Jeromy Anglim    13 年前

    因此,总结现有的讨论,增加

    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

    “添加”=“= 1.5”,即“轴= 1.5”,即“主= 1.5”,即“= 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

        3
  •  21
  •   Ulrich Dangel    12 年前

    请注意“ 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

        4
  •  20
  •   Urban Vagabond    11 年前

    通过反复试验,我确定设置字体大小需要以下内容:

    1. cex 不适用于 hist() . 使用 cex.axis 对于轴上的数字, cex.lab 对于标签。
    2. cex公司 不适用于 axis() 或者。使用 枢轴 轴上的数字。
    3. 代替使用设置标签 历史() ,您可以使用 mtext() . 您可以使用 cex公司 ,但使用值1 实际上将字体设置为默认值的1.5倍!!! 你需要使用 cex=2/3 以获取默认字体大小。至少,这是R 3.0.2formacosx下的情况,使用PDF输出。
    4. 您可以使用 pointsize 在里面 pdf() .

    我认为,期望R(a)实际执行其文档所说的应该执行的操作,(b)以一种预期的方式执行,这太合乎逻辑了。

        5
  •  2
  •   Odysseus Ithaca    7 年前

    当我想让轴标签更小时,我发现了这一点,但剩下的东西都一样大小。对我有用的命令是:

    par(cex.axis=0.5)
    

    在plot命令之前。请记住:

    par(cex.axis=1.0)
    

    打印后确保字体回到默认大小。

        6
  •  1
  •   yeinhorn    9 年前

    如果要在设置labels=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)
    
        7
  •  1
  •   Adam Erickson    6 年前

    为了完整性,将文本缩放150% cex = 1.5 ,以下是完整的解决方案:

    cex <- 1.5
    par(cex.lab=cex, cex.axis=cex, cex.main=cex)
    plot(...)
    par(cex.lab=1, cex.axis=1, cex.main=1)
    

    我建议用这样的包装来减少样板,例如:

    plot_cex <- function(x, y, cex=1.5, ...) {
      par(cex.lab=cex, cex.axis=cex, cex.main=cex)
      plot(x, y, ...)
      par(cex.lab=1, cex.axis=1, cex.main=1)
      invisible(0)
    }
    

    你可以这样使用:

    plot_cex(x=1:5, y=rnorm(5), cex=1.3)
    

    这个 ... 在R中称为椭圆,用于将附加参数传递给函数。因此,它们通常用于绘图。因此,以下工作如期进行:

    plot_cex(x=1:5, y=rnorm(5), cex=1.5, ylim=c(-0.5,0.5))