符号lambda(λ)在导出图像中以r的pdf出口时不会显示?

发布于 2025-01-23 12:49:50 字数 1057 浏览 3 评论 0原文

我使用ggplot2在r中创建了一个图,并使用注释我在文本中包括了一个lambda(λ)符号。但是,当我确实出口时;另存为PDF,PDF映像没有显示lambda,而只是显示了一个“ ..”,我附上了下面的样子的示例:

“在此处输入映像说明”

这是我使用的代码:

dna.b.bae.coi <- data.frame(hours=c(1,2,24,48,96,168,672), copies=c(39,46,13,1,0,0,0))
nlsLM(copies ~ a*exp(b*hours), data=dna.b.bae.coi, start=list(a=39,b=-0.16507))
nlsplot(dna.b.bae.coi, model=6, start=c(a=45.97176,b=-0.05464))
a1<- ggplot(dna.b.bae.coi, aes(x=hours, y=copies)) + geom_point() + stat_smooth(method = 'nls', method.args = list(start = c(a=45.97176,b=-0.05464)), formula = y~a*exp(b*x), se=FALSE, linetype=2, colour="yellow") + theme_classic() + xlab("") + ylab("") + 
  annotate("text", x = 300, y = 46, label = "COI eDNA\nλ = -0.0546", color = "black", hjust = 0, vjust = 1) +
  ggtitle(expression(~italic("Baetidae")))
a1

我不知道为什么会发生这种情况。有没有办法解决此问题,以便当我保存为PDF时,λ实际上出现在我的数字中?

提前致谢!

I created a figure in R using ggplot2, and using annotate I included a lambda (λ) symbol in the text. However, when I do Export > Save as PDF, the PDF image does not show the lambda and instead just shows a ".." I have attached an example of what this looks like below:

enter image description here

This is the code I used:

dna.b.bae.coi <- data.frame(hours=c(1,2,24,48,96,168,672), copies=c(39,46,13,1,0,0,0))
nlsLM(copies ~ a*exp(b*hours), data=dna.b.bae.coi, start=list(a=39,b=-0.16507))
nlsplot(dna.b.bae.coi, model=6, start=c(a=45.97176,b=-0.05464))
a1<- ggplot(dna.b.bae.coi, aes(x=hours, y=copies)) + geom_point() + stat_smooth(method = 'nls', method.args = list(start = c(a=45.97176,b=-0.05464)), formula = y~a*exp(b*x), se=FALSE, linetype=2, colour="yellow") + theme_classic() + xlab("") + ylab("") + 
  annotate("text", x = 300, y = 46, label = "COI eDNA\nλ = -0.0546", color = "black", hjust = 0, vjust = 1) +
  ggtitle(expression(~italic("Baetidae")))
a1

I am not sure why this is happening. Is there a way around this so that the λ actually shows up in my Figure when I save as PDF?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2025-01-30 12:49:50

这与ggsave

> p <- ggplot(mtcars, aes(wt, mpg)) + geom_point() + annotate("text", x = 300, y = 46, label = "COI eDNA\nλ = -0.0546", color = "black", hjust = 0.5, vjust = 1)
> ggsave("pp.png")


​您可以使用 tikzdevice ,它将您的情节转换为乳胶。

library(tikzDevice)
plot2tikz <- function(code, filename="Rplot", outdir=getwd(),
                      overwrite=FALSE, format="pdf", lua=FALSE,
                      packages=NULL, addDefaultTikZoptions=TRUE,
                      compile=TRUE, clean=FALSE, ...){
  format <- match.arg(format, choices = c("pdf", "ps", "eps"))
  texfile <- paste0(filename, ".tex")
  owd <- setwd(outdir); on.exit(setwd(owd))
  if(overwrite || !file.exists(texfile)){
    if(is.null(packages)){
      if(format=="pdf") packages <- getOption("tikzLatexPackages")
      if(format %in% c("ps", "eps")) packages <- c("\\thispagestyle{empty}\n", "\\usepackage{tikz}\n")
    } else {
      if(!"\\usepackage{tikz}\n" %in% packages){
        packages <- c("\\usepackage{tikz}\n", packages)
        if(format=="pdf" && addDefaultTikZoptions){
          packages <- union(packages, getOption("tikzLatexPackages"))
        }
      }
    }
    tikz(texfile, standAlone=TRUE, onefile=FALSE, packages=packages, ...)
    code()
    grDevices::dev.off()
  }
  if(compile || format=="eps"){
    message("Compilation...")
    if(format=="pdf"){
      # pdf compilation
      pdffile <- stringr::str_replace(texfile, ".tex", ".pdf")
      if(overwrite || !file.exists(pdffile)){
        if(lua){
          command <- sprintf("lualatex %s", texfile)
          system(command)
        }else{
          tools::texi2dvi(texfile, pdf=TRUE, clean=clean)
        }
        message(sprintf("Output pdf file: %s.pdf", filename))
      }
    } else if(format %in% c("ps", "eps")){
      psfile <- stringr::str_replace(texfile, ".tex", ".ps")
      if(overwrite || !file.exists(psfile)){
        tools::texi2dvi(texfile, pdf=FALSE, clean=clean)
        command <- sprintf("dvips %s.dvi", filename)
        system(command)
        message(sprintf("Output ps file: %s.ps", filename))
        if(format=="eps"){
          command <- sprintf("ps2epsi %s.ps %s.epi", filename, filename)
          system(command)
          file.rename(sprintf("%s.epi", filename), sprintf("%s.eps", filename))
          message(sprintf("Output eps file: %s.eps", filename))
        }
      }
    }
  }
  #
  message(sprintf("Output tex file: %s", normalizePath(texfile, winslash=.Platform$file.sep)))
  return(invisible())
}


library(ggplot2)
plotCode <- function(){
  gg <- ggplot(mtcars, aes(wt, mpg)) + geom_point() + 
    annotate("text", x = 300, y = 46, label = "COI eDNA\n$\\lambda$ = -0.0546", 
             color = "black", hjust = 0.5, vjust = 1)
  print(gg)
}


plot2tikz(plotCode, compile=FALSE, outdir=getwd(), overwrite = TRUE,
          packages=c("\\usepackage[active,tightpage,psfixbb]{preview}\n",
                     "\\PreviewEnvironment{pgfpicture}\n",
                     "\\setlength\\PreviewBorder{10pt}\n",
                     "\\usepackage{amssymb}\n"),
          documentDeclaration ="\\documentclass[12pt]{standalone}\n",
          width=7, height=5)

This works with ggsave:

> p <- ggplot(mtcars, aes(wt, mpg)) + geom_point() + annotate("text", x = 300, y = 46, label = "COI eDNA\nλ = -0.0546", color = "black", hjust = 0.5, vjust = 1)
> ggsave("pp.png")

enter image description here


EDIT With tikzDevice:

To get a pdf, you can use tikzDevice, which converts your plot in LaTeX.

library(tikzDevice)
plot2tikz <- function(code, filename="Rplot", outdir=getwd(),
                      overwrite=FALSE, format="pdf", lua=FALSE,
                      packages=NULL, addDefaultTikZoptions=TRUE,
                      compile=TRUE, clean=FALSE, ...){
  format <- match.arg(format, choices = c("pdf", "ps", "eps"))
  texfile <- paste0(filename, ".tex")
  owd <- setwd(outdir); on.exit(setwd(owd))
  if(overwrite || !file.exists(texfile)){
    if(is.null(packages)){
      if(format=="pdf") packages <- getOption("tikzLatexPackages")
      if(format %in% c("ps", "eps")) packages <- c("\\thispagestyle{empty}\n", "\\usepackage{tikz}\n")
    } else {
      if(!"\\usepackage{tikz}\n" %in% packages){
        packages <- c("\\usepackage{tikz}\n", packages)
        if(format=="pdf" && addDefaultTikZoptions){
          packages <- union(packages, getOption("tikzLatexPackages"))
        }
      }
    }
    tikz(texfile, standAlone=TRUE, onefile=FALSE, packages=packages, ...)
    code()
    grDevices::dev.off()
  }
  if(compile || format=="eps"){
    message("Compilation...")
    if(format=="pdf"){
      # pdf compilation
      pdffile <- stringr::str_replace(texfile, ".tex", ".pdf")
      if(overwrite || !file.exists(pdffile)){
        if(lua){
          command <- sprintf("lualatex %s", texfile)
          system(command)
        }else{
          tools::texi2dvi(texfile, pdf=TRUE, clean=clean)
        }
        message(sprintf("Output pdf file: %s.pdf", filename))
      }
    } else if(format %in% c("ps", "eps")){
      psfile <- stringr::str_replace(texfile, ".tex", ".ps")
      if(overwrite || !file.exists(psfile)){
        tools::texi2dvi(texfile, pdf=FALSE, clean=clean)
        command <- sprintf("dvips %s.dvi", filename)
        system(command)
        message(sprintf("Output ps file: %s.ps", filename))
        if(format=="eps"){
          command <- sprintf("ps2epsi %s.ps %s.epi", filename, filename)
          system(command)
          file.rename(sprintf("%s.epi", filename), sprintf("%s.eps", filename))
          message(sprintf("Output eps file: %s.eps", filename))
        }
      }
    }
  }
  #
  message(sprintf("Output tex file: %s", normalizePath(texfile, winslash=.Platform$file.sep)))
  return(invisible())
}


library(ggplot2)
plotCode <- function(){
  gg <- ggplot(mtcars, aes(wt, mpg)) + geom_point() + 
    annotate("text", x = 300, y = 46, label = "COI eDNA\n$\\lambda$ = -0.0546", 
             color = "black", hjust = 0.5, vjust = 1)
  print(gg)
}


plot2tikz(plotCode, compile=FALSE, outdir=getwd(), overwrite = TRUE,
          packages=c("\\usepackage[active,tightpage,psfixbb]{preview}\n",
                     "\\PreviewEnvironment{pgfpicture}\n",
                     "\\setlength\\PreviewBorder{10pt}\n",
                     "\\usepackage{amssymb}\n"),
          documentDeclaration ="\\documentclass[12pt]{standalone}\n",
          width=7, height=5)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文