0

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.

Peter Chen
  • 1,464
  • 3
  • 21
  • 48
  • `pie(dt$DayCounts, labels=dt$TravelArea)`? (It'll take a little beautification, but it's functional.) – r2evans Aug 07 '17 at 06:19
  • I know `pie()` function. But it cannot beautify right? – Peter Chen Aug 07 '17 at 06:27
  • 1
    Not much, but then it does not appear to have much popularity to justify improving. Even the help page recommends against using it, saying *"Pie charts are a very bad way of displaying information. The eye is good at judging linear measures and bad at judging relative areas. A bar chart or dot chart is a preferable way of displaying this type of data."* (I have yet to find a situation where a pie chart is better than anything other than deceiving the eyes, but that's my opinion.) – r2evans Aug 07 '17 at 06:44
  • Is this issue a) you can print the chart you want in the r console but you can't knit the rmarkdown file to docx, b) you can't create the pie chart at all, or c) you can't spit the pie chart out so that you can paste it into word yourself? – Scransom Aug 07 '17 at 06:44
  • @r2evans maybe I can use bar chart to show? better than pie? – Peter Chen Aug 07 '17 at 06:46
  • 1
    Use this package to create beautiful pie charts, including jpeg backgrounds: https://cloud.r-project.org/web/packages/patternplot/vignettes/patternplot-intro.html [nah just don't] – Scransom Aug 07 '17 at 06:47
  • A rough start could be `par(mar=c(5,8,2,2)); barplot(setNames(dt$DayCounts, dt$TravelArea), horiz=TRUE, las=2)`, then perhaps adding `col`ors, `grid()`, etc. Or you can go with `library(ggplot2); ggplot(dt, aes(TravelArea, DayCounts)) + geom_col()`, and that'll allow theming perhaps closer to what you are trying to do above. But either one is more readable and comparable (IMHO) than a pie chart. – r2evans Aug 07 '17 at 06:52

1 Answers1

1

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)

```