【问题标题】:How can I display my ggtern plots in my PDF from R Markdown?如何在 R Markdown 的 PDF 中显示我的 ggtern 图?
【发布时间】:2019-07-27 23:33:39
【问题描述】:

当我编织我编写的 R Markdown 脚本时,我的 ggtern 图都没有出现在 PDF 中。 R/ggtern 脚本运行良好。 我在互联网上阅读了很多帖子,但没有一个可以帮助我解决这个问题。 这些图仍未出现在最终的pdf中。 我该如何解决这个问题?

---
title: xxx
subtitle: xxx
author: "xxx"
date: "27/07/2019"
output: pdf_document
---
```{r setup, include=FALSE} #open chunk
knitr::opts_chunk$set(echo = TRUE, error = TRUE, eval = FALSE)
``` #close chunk
``` #open chunk
{r base3, echo=FALSE}
plot(base3)
``` #close chunk
#ggtern 3 zones
points3 <- data.frame(
    rbind(
        c(1,1.000,0.000,0.000),
        c(2,0.500,0.500,0.000),
        c(3,0.500,0.000,0.500),
        c(4,0.500,0.500,0.500),
        c(5,0.000,1.000,0.000),
        c(6,0.000,0.500,0.500),
        c(7,0.000,0.000,1.000)
    )
)
colnames(points3)=c("IDPoint","T","L","R")

#Give each Polygon a number
polygon.labels3 <- data.frame(Label3=c("O","P","N"))
polygon.labels3$IDLabel=1:nrow(polygon.labels3)

#Create a map of polygons to points
polygons3 <- data.frame(
    rbind(
        c(1,1),c(1,2),c(1,4),c(1,3),
        c(2,2),c(2,4),c(2,6),c(2,5),
        c(3,3),c(3,7),c(3,6),c(3,4)
    )
)
polygons3$PointOrder <- 1:nrow(polygons3)
colnames(polygons3)=c("IDLabel","IDPoint","PointOrder")

#Merge the three sets together to create a master set.
df3 <- merge(polygons3,points3)
df3 <- merge(df3,polygon.labels3)
df3 <- df3[order(df3$PointOrder),]

#Determine the Labels Data
Labs3=ddply(df3,"Label3",function(x){c(c(mean(x$T),mean(x$L),mean(x$R)))})
colnames(Labs3)=c("Label","T","L","R")

#Build the final plot
base3 <- ggtern(data=df3,aes(L,T,R)) + 
    geom_polygon(aes(group=Label3),color="black",alpha=0) +
    geom_text(data=Labs3,aes(label=Label),size=3,color="black") + 
    theme_bw() + 
    tern_limits(labels=c(10,20,30,40,50,60,70,80,90,100),breaks=seq(0.1,1,by=0.1)) +
    theme_clockwise() +
    theme_showarrows() +
    labs(title  = "Test",Tarrow = "% Oui",Larrow = "% Peut-être",Rarrow = "% Non") +
    theme(tern.axis.arrow=element_line(size=1,color="black")) +
    labs(title="Diagramme ternaire 3 zones",T="Oui",L="Peut-être",R="Non")

我希望这些图会显示在最终的 pdf 中。

这是我的 .rmd 文件的截图:

Screenshot of my .rmd document

【问题讨论】:

  • print(base3)?
  • 我运行了你的代码,情节显示在我的 pdf 输出中。
  • 您应该向我们展示一份完整的文件。您的可能太大,无法在此处发布,因此请删除不需要说明问题的部分,并仅发布显示问题的最小文档。如果您不发布可重现的示例,我们将无能为力。
  • 我已经更新了我的帖子以使其可重现。
  • 您的示例将无法运行 --- 它没有正确形成为 R Markdown 文档,出现故障等。

标签: r ggplot2 rstudio r-markdown ggtern


【解决方案1】:

起初我无法运行您的代码,因为您将 cmets 放在块选项之后(例如,删除了 #open chunk)。然后我重新排序了你的代码。情节应在创建后绘制。创建绘图的代码也应该在 r 块内。您还需要在块选项中添加message=FALSE,以避免打印来自函数的消息。我还必须加载plyrggtern这两个包。

以下代码对我有用。

```
---
title: xxx
subtitle: xxx
author: "xxx"
date: "27/07/2019"
output: pdf_document
---

```{r base3, echo=FALSE, message=FALSE}

library(plyr)
library(ggtern)

#ggtern 3 zones
points3 <- data.frame(
    rbind(
        c(1,1.000,0.000,0.000),
        c(2,0.500,0.500,0.000),
        c(3,0.500,0.000,0.500),
        c(4,0.500,0.500,0.500),
        c(5,0.000,1.000,0.000),
        c(6,0.000,0.500,0.500),
        c(7,0.000,0.000,1.000)
    )
)
colnames(points3)=c("IDPoint","T","L","R")

#Give each Polygon a number
polygon.labels3 <- data.frame(Label3=c("O","P","N"))
polygon.labels3$IDLabel=1:nrow(polygon.labels3)

#Create a map of polygons to points
polygons3 <- data.frame(
    rbind(
        c(1,1),c(1,2),c(1,4),c(1,3),
        c(2,2),c(2,4),c(2,6),c(2,5),
        c(3,3),c(3,7),c(3,6),c(3,4)
    )
)
polygons3$PointOrder <- 1:nrow(polygons3)
colnames(polygons3)=c("IDLabel","IDPoint","PointOrder")

#Merge the three sets together to create a master set.
df3 <- merge(polygons3,points3)
df3 <- merge(df3,polygon.labels3)
df3 <- df3[order(df3$PointOrder),]

#Determine the Labels Data
Labs3=ddply(df3,"Label3",function(x){c(c(mean(x$T),mean(x$L),mean(x$R)))})
colnames(Labs3)=c("Label","T","L","R")

#Build the final plot
base3 <- ggtern(data=df3,aes(L,T,R)) + 
    geom_polygon(aes(group=Label3),color="black",alpha=0) +
    geom_text(data=Labs3,aes(label=Label),size=3,color="black") + 
    theme_bw() + 
    tern_limits(labels=c(10,20,30,40,50,60,70,80,90,100),breaks=seq(0.1,1,by=0.1)) +
    theme_clockwise() +
    theme_showarrows() +
    labs(title  = "Test",Tarrow = "% Oui",Larrow = "% Peut-être",Rarrow = "% Non") +
    theme(tern.axis.arrow=element_line(size=1,color="black")) +
    labs(title="Diagramme ternaire 3 zones",T="Oui",L="Peut-être",R="Non")

plot(base3)
```

【讨论】:

  • 感谢您的帮助。您的代码在我的 .rmd 文件(在 RStudio 控制台中)中有效,但 pdf 输出中仍然缺少这些数字。我想知道设置是否有问题...
  • 但是它在 R Markdown 笔记本而不是 R Markdown 文档中工作。怎么可能?抛开这个问题,谢谢Mobody!它有效!
  • 问题是您包含的r setup 块(我在我的回答帖子中删除了)。在那里,您将编织块的全局选项设置为eval = FALSE,这意味着块不会被处理。删除块或将其设置为eval = TRUE,它应该可以工作。
  • 如果这能解决你的问题,如果你接受我的回答会很好。
  • 是的,我是在网上搜索答案后才知道的。我接受了你的回答。它被记录下来,但它没有公开出现,因为我的声望不到 15。再次感谢 Mobody。
猜你喜欢
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
  • 2015-12-09
  • 2019-06-02
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多