for循环的plotly子图从最后一个循环创建图

时间:2019-11-08 16:42:53

标签: r plotly r-plotly

我正在学习使用以下脚本创建与ggplot使用ggplot创建的图相似的图:

library(dplyr)
library(ggplot2)
library(plotly);library(RColorBrewer)
library(tidyr)

set.seed(1)

data.df.st00 <- iris %>%
  as_tibble %>%
  mutate(year=sample(2014:2018,size = n(),replace = TRUE)) %>%
  mutate(sex=sample(c('F','M'),size = n(),replace = TRUE)) %>%
  mutate(origin=sample(c('Community','Hospital'),size = n(),replace = TRUE))

data.df.st01 <- data.df.st00 %>%
  group_by(year,sex,origin) %>%
  count(Species) %>%
  ungroup

data.df.st02 <- data.df.st01 %>%
  # mutate(Species= forcats::fct_rev(Species)) %>%
  mutate(variable=paste0(origin,'-',sex)) %>%
  select(-origin,-sex) %>%
  spread(.,key = variable,value = n,fill = as.integer(0))

combn.df <- data.df.st01 %>%
  distinct(origin,sex) %>%
  mutate(variable=paste0(origin,'-',sex))

for (loop in 1:nrow(combn.df)) {
  temp.chart.subplot <- data.df.st02 %>%
    plot_ly(type='bar',
            x=~year,
            y=~get(combn.df$variable[loop]), 
            color=~Species,
            colors=(brewer.pal(length(fill.list),'Set2')),
            legendgroup=~Species,
            showlegend=(loop==1)) %>%
    layout(yaxis = list(title='n'), barmode = 'stack')

  assign(paste0('chart.plotly.',loop),temp.chart.subplot)
}

chart.plotly.final <- subplot(chart.plotly.1,
                              chart.plotly.2,
                              chart.plotly.3,
                              chart.plotly.4,
                              nrows = 2,
                              shareY = TRUE,shareX = TRUE) %>%
  layout(legend = list(x=1.02,y=0.5),
         annotations = list(x=1.165,y=0.625,xref='paper',yref='paper',text='Species',showarrow=FALSE))

chart.plotly.final

我的对象chart.plotly.final是一个包含chart.plotly.14的子图,但是chart.plotly.final给出了一个包含chart.plotly.4的4个副本的子图,我不是希望有。

我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道您的代码中的错误是什么,但这似乎可以解决问题:

library("dplyr")
library("ggplot2")
library("plotly")
library("RColorBrewer")
library("tidyr")

set.seed(1)

data.df.st00 <- iris %>%
  as_tibble %>%
  mutate(year=sample(2014:2018,size = n(),replace = TRUE)) %>%
  mutate(sex=sample(c('F','M'),size = n(),replace = TRUE)) %>%
  mutate(origin=sample(c('Community','Hospital'),size = n(),replace = TRUE))

data.df.st01 <- data.df.st00 %>%
  group_by(year,sex,origin) %>%
  count(Species) %>%
  ungroup

data.df.st02 <- data.df.st01 %>%
  # mutate(Species= forcats::fct_rev(Species)) %>%
  mutate(variable=paste0(origin,'-',sex)) %>%
  select(-origin,-sex) %>%
  spread(.,key = variable,value = n,fill = as.integer(0))

combn.df <- data.df.st01 %>%
  distinct(origin,sex) %>%
  mutate(variable=paste0(origin,'-',sex))

plots <- lapply(seq_len(nrow(combn.df)), function(i) {
  data.df.st02 %>%
    plot_ly(type='bar',
            x=~year,
            y=~get(combn.df$variable[i]), 
            color=~Species,
            legendgroup=~Species,
            showlegend=(i==1)) %>%
    layout(yaxis = list(title='n'), barmode = 'stack')
})

chart.plotly.final <- subplot(plots,
                              nrows = 2,
                              shareY = TRUE,shareX = TRUE) %>%
  layout(legend = list(x=1.02,y=0.5),
         annotations = list(x=1.165,y=0.625,xref='paper',yref='paper',text='Species',showarrow=FALSE))

chart.plotly.final