【问题标题】:How to change default font size in R chart如何更改 R 图表中的默认字体大小
【发布时间】:2015-01-09 23:49:26
【问题描述】:

我正在使用 R 包 cooccur,但无法弄清楚如何更改相关图形中的字体大小。 par() 方法似乎不起作用。

下面是包给出的例子:

data(finches)
cooccur.finches <- cooccur(mat=finches,
type="spp_site",
thresh=TRUE,
spp_names=TRUE)
plot(cooccur.finches)

我正在尝试更改物种的字体大小、标题和图例,但在生成的热图上无济于事。任何帮助将非常感激。谢谢!

【问题讨论】:

    标签: r


    【解决方案1】:

    不幸的是作者没有在函数中使用定义的主题,所以如果你不想弄乱其他自定义,这应该可以:

    p <- plot(cooccur.finches)
    p + theme_bw(base_size = 28) +
        theme(axis.text = element_blank(), 
              axis.ticks = element_blank(), 
              plot.title = element_text(vjust = -4, face = "bold"), 
              panel.background = element_rect(fill = "white", colour = "white"), 
              panel.grid = element_blank()
              legend.position = c(0.9, 0.5))
    

    您也可以使用此代码独立设置图例或标题的大小,例如

    p + theme(plot.title = element_text(vjust = -4, face = "bold", size = 36))
    

    最不幸的是,这不会改变物种标签的大小,因为它们是用geom_text() 设置的。要更改它们,您必须自己破解函数cooccur:::plot.cooccur。只需要修改最后一行:

    p + geom_text(data = dfids, aes(label = X1), hjust = 1, vjust = 0, 
            angle = -22.5)
    # change to
    p + geom_text(data = dfids, aes(label = X1), hjust = 1, vjust = 0, 
            angle = -22.5, size = 24)
    

    【讨论】:

      【解决方案2】:

      这是一个 ggplot2 图,不是基础图。所以par 将不起作用。

      p <- plot(cooccur.finches)
      p + theme(text = element_text(size = 10))  ## change text font size
      

      p + theme_grey(base_size = 18)             ## chnage all font size.
      

      【讨论】:

      • 感谢您的帮助!
      【解决方案3】:

      这里是 Cooccur 的作者。很抱歉文字大小难以调整带来的麻烦。有机会我会处理的。

      不是永久的解决方案,但比每次更改物种标签的功能更容易,只是直接重新分配 ggplot 对象中的值:

      p$layers[[2]]$geom_params$size <- 10
      

      希望对您有所帮助。我可能来晚了一点……

      【讨论】:

        猜你喜欢
        • 2012-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-28
        • 2012-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多