如何在 R Markdown 的 PDF 中显示我的 ggtern 图?
How can I display my ggtern plots in my PDF from R Markdown?
当我编写我编写的 R Markdown 脚本时,None 的 ggtern 图出现在 PDF 中。
R/ggtern 脚本运行良好。
我在 Internet 上阅读了很多帖子,但其中 none 可以帮助我解决此问题。
这些图仍然没有出现在最终的 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
起初我无法 运行 你的代码,因为你在块选项之后添加了注释(例如删除 #open chunk
)。然后我重新排序了你的代码。该图应在创建后绘制。创建绘图的代码也应该在 r 块中。您还需要将 message=FALSE
添加到块选项中,以避免打印来自函数的消息。我还必须加载两个包 plyr
和 ggtern
.
以下代码对我有用。
```
---
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)
```
None 的 ggtern 图出现在 PDF 中。 R/ggtern 脚本运行良好。 我在 Internet 上阅读了很多帖子,但其中 none 可以帮助我解决此问题。 这些图仍然没有出现在最终的 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
起初我无法 运行 你的代码,因为你在块选项之后添加了注释(例如删除 #open chunk
)。然后我重新排序了你的代码。该图应在创建后绘制。创建绘图的代码也应该在 r 块中。您还需要将 message=FALSE
添加到块选项中,以避免打印来自函数的消息。我还必须加载两个包 plyr
和 ggtern
.
以下代码对我有用。
```
---
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)
```