Search code examples
rr-markdownpie-chartdocx

R pie chart in rmarkdown docx


I have a data and want to create a pie chart in Rmarkdown word_document.
Here is my data:

dt <- data.table::fread("
        TravelArea DayCounts  
            Others     98254  
             China    298705 
               USA     39048  
         SouthAsia    127046  
            Europe    114529  
        MIDAmerica      4270  
               AUS     21917 
            ENAsia    361727 
             Local   1819977 
            Africa      2473  
 AsiaPacificIsland      2943 
            ESAsia     25208  ")

My code in Rmarkdown is:

```{r echo=FALSE, message=FALSE, warning=FALSE}
plotly::plot_ly(dt, labels = ~ TravelArea, values = ~ DayCounts, type = 'pie', 
                 textposition = 'inside', 
                 textinfo = 'label+percent', 
                 insidetextfont = list(color = '#FFFFFF'), 
                 hoverinfo = 'text', 
                 text = ~paste(TravelArea, DayCounts),
                 marker = list(colors = colors,                                                                                                                               line = list(color = '#FFFFFF', width = 1)),
                 showlegend = T) %>%
    layout(title = 'Figure C.1.1',
           xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
           yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
```

However, this cannot output to docx. How can I create a pie chart that can output and present properly to word_document.


Solution

  • you can try solution given on this link: Plotly as png in knitr/rmarkdown.

    ---
    title: "Plot"
    output: word_document
    ---
    
    ```{r echo=FALSE, message=FALSE}
    library(plotly)
    
    dt <- data.table::fread("
             TravelArea DayCounts  
                 Others     98254  
                  China    298705 
                    USA     39048  
              SouthAsia    127046  
                 Europe    114529  
             MIDAmerica      4270  
                    AUS     21917 
                 ENAsia    361727 
                  Local   1819977 
                 Africa      2473  
      AsiaPacificIsland      2943 
                 ESAsia     25208  ")
    
    
    
    p <- plot_ly(dt, labels = ~ TravelArea, values = ~ DayCounts, type = 'pie', 
                 textposition = 'inside', 
                 textinfo = 'label+percent', 
                 insidetextfont = list(color = '#FFFFFF'), 
                 hoverinfo = 'text', 
                 text = ~paste(TravelArea, DayCounts),
                 marker = list(colors = colors,                                                                
                 line = list(color = '#FFFFFF', width = 1)),
                 showlegend = T)  %>%
                 layout(title = 'Figure C.1.1',
                 xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                 yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
    
    tmpFile <- tempfile(fileext = ".png")
    export(p, file = tmpFile)
    
    ```