【问题标题】:Set font family and color in png plot device在 png 绘图设备中设置字体系列和颜色
【发布时间】:2019-08-24 23:16:06
【问题描述】:

我正在使用base R 绘制到png 设备。我想将字体系列强制为Calibri,并带有特定的灰色阴影。我在par中设置了绘图参数。

当我在 RStudio 中以交互方式绘图时,一切正常,但当我保存到 png 时却不行。这里有什么问题?

# set plot parameters
myfont <- 'Calibri'
mycol <- '#4B4B4D'
par(family = myfont, fg = mycol, col = mycol, col.axis = mycol, col.lab = mycol, col.main = mycol, col.sub = mycol)

# plot to default device works fine 
plot(x = c(1:10), y = rep(1, 10)) 

# export to png
png('d:/working/example.png', units = "in", width = 16, height = 9,
    pointsize = 12, res = 300, family = myfont)

plot(x = c(1:10), y = rep(1, 10)) # nope, not myfont and not mycol
dev.off()

顺便说一句,通过 RStudio 绘图窗口手动导出是可行的,但这不是我要走的路。

【问题讨论】:

    标签: r plot fonts png


    【解决方案1】:

    R 在使用par(...) 时会区分设备,这意味着它必须在 png(...) 之后设置。

    来自?par中的详情

    每个设备都有自己的一组图形参数。

    这行得通:

    library(extrafont)
    loadfonts(device='win')
    myfont <- 'Calibri'
    mycol <- '#4B4B4D'
    
    # first, open png device
    png('d:/working/example.png', units = "in", width = 16, height = 9, pointsize = 12,
        res = 300, family = myfont, type = 'windows')
    
    # Then set plot parameters in par; font and colors
    par(family = myfont, fg = mycol, col = mycol, col.axis = mycol, col.lab = mycol, col.main = mycol, col.sub = mycol)
    
    # Initiate plot
    plot(x = c(1:10), y = rep(1, 10))
    dev.off()
    

    【讨论】:

      猜你喜欢
      • 2013-01-30
      • 2016-04-12
      • 2010-09-27
      • 1970-01-01
      • 2011-07-05
      • 2012-08-03
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      相关资源
      最近更新 更多