【问题标题】:Italics in title of Lattice graph格图标题中的斜体
【发布时间】:2013-05-20 22:02:35
【问题描述】:

我已阅读有关如何在图表标题中创建斜体字的帖子,但它似乎对我不起作用。

#create a list of species
sp <- c("Etelis coruscans","Etelis carbunculus","Pristipomoides sieboldii","Pristipomoides filamentosus","Pristipomoides zonatus","Epinephelus quernus","Aphareus rutilans")

#plot hisotgrams for each spp in 1cm bins
for (i in sp){
    BIN_WIDTH <- 1 #desired bin width
    print(histogram(~ Length..cm. | Method, #create and print the histogram and save to variable "graph"
    data = hist.data[hist.data$Scientific_name == i,], 
    nint = (max(hist.data$Length..cm.) - min(hist.data$Length..cm.)+1)/BIN_WIDTH,
    layout = c(1,2),
    main = paste("Length-Frequency of", italic(i), "by Gear"), sep = " ",
    xlab = "Length (cm)"))

    #save histogram to PNG file
    quartz.save(paste("*Length-Frequency of", i, "by method.png", sep = " "), type = "png")
    dev.off() #close the graphics diver
}

我收到一条错误消息:

Error in print(histogram(~Length..cm. | Method, data = hist.data[hist.data$Scientific_name ==  : 
  error in evaluating the argument 'x' in selecting a method for function 'print': Error in paste(italic("Length-Frequency of", i, "by Gear")) : 
  could not find function "italic"

谁能指出我做错了什么?

【问题讨论】:

    标签: r lattice italics


    【解决方案1】:

    您传递给main 的参数需要进行一些更改。

    • 要使用 R 的 plotmath 特殊功能(即 italic() 之类的东西),它应该是一个表达式对象而不是字符串。这意味着做这样的事情:

      main = expression(paste("Length-Freq of", italic("E. coruscans"), "by Gear"))
      

      而不是这个:

      main = paste("Length-Freq of", italic("E. coruscans"), "by Gear")
      
    • 另外,你想要斜体i而不是它的名称,但是如果你只输入italic(i),lattice会渲染@987654328 @ 的名称为每个物种的小斜体“i”。使用bquote()substitute() 替换i 的值,如下所示:

      i <- "E. coruscans"
      xyplot(1:10~1:10,
          main = substitute(expr = expression(paste("Species name: ", italic(i))), 
                            env = list(i=i)))
      

    【讨论】:

    • 完美!这是最终的工作代码: main = substitution(expr = expression(paste("Length-Frequency of ", italic(i), " by Gear")), env = list(i=i)),
    猜你喜欢
    • 2020-11-18
    • 2021-02-19
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多