简体   繁体   English

Rmarkdown docx中的R饼图

[英]R pie chart in rmarkdown docx

I have a data and want to create a pie chart in Rmarkdown word_document. 我有一个数据,想要在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: 我在Rmarkdown中的代码是:

```{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 . 但是,这不能输出到docx How can I create a pie chart that can output and present properly to word_document . 如何创建一个饼形图,可以将其正确输出并呈现给word_document

you can try solution given on this link: Plotly as png in knitr/rmarkdown . 您可以尝试在此链接上给出的解决方案: 在knitr / rmarkdown中以png格式绘制

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

```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM