带有for循环的R标记饼图

带有for循环的R标记饼图,r,for-loop,pie-chart,plotly,data-manipulation,R,For Loop,Pie Chart,Plotly,Data Manipulation,我有一个关于使用for loop绘制饼图的问题 这是我的原始数据 type,com,year,month,value A,CH,2015,1,0 A,CH,2015,2,0 A,CH,2015,3,0 A,CH,2015,4,0 A,CH,2015,5,0 A,CH,2015,6,0 A,CH,2015,7,0 A,CH,2015,8,2200 A,CH,2015,9,111384 A,CH,2015,10,28758 A,CH,2015,11,21161 A,CH,2015,12,0 A,CH

我有一个关于使用for loop绘制饼图的问题
这是我的原始数据

type,com,year,month,value
A,CH,2015,1,0
A,CH,2015,2,0
A,CH,2015,3,0
A,CH,2015,4,0
A,CH,2015,5,0
A,CH,2015,6,0
A,CH,2015,7,0
A,CH,2015,8,2200
A,CH,2015,9,111384
A,CH,2015,10,28758
A,CH,2015,11,21161
A,CH,2015,12,0
A,CH,2016,1,0
A,CH,2016,2,0
A,CH,2016,3,0
A,CH,2016,4,0
A,CH,2016,5,0
A,CH,2016,6,0
A,CH,2016,7,0
A,CH,2016,8,0
A,CH,2016,9,453128
A,CH,2016,10,868256
A,CH,2016,11,1015080
A,CH,2016,12,650912
A,ZU,2015,1,0
A,ZU,2015,2,0
A,ZU,2015,3,0
A,ZU,2015,4,0
A,ZU,2015,5,0
A,ZU,2015,6,61273
A,ZU,2015,7,27711
A,ZU,2015,8,161780
A,ZU,2015,9,48889
A,ZU,2015,10,72805
A,ZU,2015,11,131466
A,ZU,2015,12,73756
A,ZU,2016,1,400
A,ZU,2016,2,0
A,ZU,2016,3,0
A,ZU,2016,4,0
A,ZU,2016,5,0
A,ZU,2016,6,0
A,ZU,2016,7,0
A,ZU,2016,8,10000
A,ZU,2016,9,147533
A,ZU,2016,10,64572
A,ZU,2016,11,57542
A,ZU,2016,12,0
B,CH,2015,1,100
B,CH,2015,2,0
B,CH,2015,3,0
B,CH,2015,4,0
B,CH,2015,5,0
B,CH,2015,6,0
B,CH,2015,7,0
B,CH,2015,8,4025
B,CH,2015,9,2054
B,CH,2015,10,7235
B,CH,2015,11,6421
B,CH,2015,12,3560
我使用package
plotly
绘图,下面是我的
.Rmd
代码

---
title: "R markdown Pie Chart For-Loop" 
output: 
  html_document:
    css: custom.css
    theme: journal
    toc: true
    toc_float: 
      collapsed: false
      smooth_scroll: false
    toc_depth: 4
--- 
```{r setup, echo=FALSE, warning=FALSE, message=FALSE}
library(knitr)
library(lubridate)
library(plotly)
```
```{r create-datasets, echo=FALSE}
pie <- read.csv("pie.csv" , header = T , colClasses = c("year"="factor", "month"="factor"))
pie$month <- as.numeric(as.character(pie$month))
```
```{r include = FALSE}
# If I want to use for loop to print htmltools::tagList, this chunk is very important.
# However, I don't know why must need it.
plot_ly(QQ)
```
```{r echo=FALSE, warning=FALSE, message=FALSE, results='asis'}
for (i in unique(QQ$type)){
  cat("  \n#",  i, "  \n") 
  print(htmltools::tagList(plot_ly(summarise(group_by(QQ[QQ$type == i,], com, year),sum(value)), labels = ~com, values = ~`sum(value)`, type = 'pie',
        textposition = 'inside',
        textinfo = 'label+percent',
        insidetextfont = list(color = '#FFFFFF'),
        hoverinfo = 'text',
        text = ~paste(com, `sum(value)`),
        marker = list(colors = colors,
                      line = list(color = '#FFFFFF', width = 1)),
        showlegend = T) %>%
  layout(title = '',
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)) 
  ))
  cat("  \n")
}
```

非常感谢

34 ```{r echo=FALSE, warning=FALSE, message=FALSE, results='asis'}
35 for (i in unique(QQ$type)){
       cat("  \n#",  i, "  \n")
       if(QQ$type == i & QQ$year == "2015"){
       print(htmltools::tagList(plot_ly(filter(summarise(group_by(QQ[QQ$type == i,], com, year),sum(value)), year == "2015"), labels = ~com, values = ~`sum(value)`, type = 'pie',
          textposition = 'inside',
          textinfo = 'label+percent',
          insidetextfont = list(color = '#FFFFFF'),
          hoverinfo = 'text',
          text = ~paste(com, `sum(value)`),
          marker = list(colors = colors,
                        line = list(color = '#FFFFFF', width = 1)),
          showlegend = T) %>%
    layout(title = '',
           xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
           yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)) 
     ))
     } else {
      print(htmltools::tagList(plot_ly(filter(summarise(group_by(QQ[QQ$type == i,], com, year),sum(value)), year == "2016"), labels = ~com, values = ~`sum(value)`, type = 'pie',
           textposition = 'inside',
           textinfo = 'label+percent',
           insidetextfont = list(color = '#FFFFFF'),
           hoverinfo = 'text',
           text = ~paste(com, `sum(value)`),
           marker = list(colors = colors,
                         line = list(color = '#FFFFFF', width = 1)),
           showlegend = T) %>%
     layout(title = '',
            xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
            yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)) 
      ))
    }
    cat("  \n")
  }}
  ```