Search code examples
rggplot2rstudior-markdownggtern

How can I display my ggtern plots in my PDF from R Markdown?


None of my ggtern plots appear in the PDF when I am kniting the R Markdown script I have written. The R/ggtern script works well. I have read many posts on the Internet but none of these could help me to fix this issue. The plots still not appear in the final pdf. Hown can I fix this?

---
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")

I expect the plots to be displayed in the final pdf.

Here is a screenshot of my .rmd document:

Screenshot of my .rmd document


Solution

  • At first I couldn't run your code since you put comments after the chunk options (e.g. deleted #open chunk). Then I reordered your code. The plot should be ploted after creating it. And the code to create the plot should be inside the r chunk too. You also need to add message=FALSEto the chunk options to avoid having the message from the functions printed. I also had to load the two packages plyrand ggtern.

    The following code worked for me.

    ```
    ---
    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)
    ```