首页 > 解决方案 > Plotly GGplot - 情节响应

问题描述

代码输出是一个我希望它响应的图,以根据窗口尺寸进行调整。使用只是ggplot给了我想要的结果,但我想使用交互式工具提示plotly,但是当我这样做时,该图没有响应。

有什么可以解决的吗?代码如下。我真的很感激任何帮助!

library(dplyr)
  library(ggplot2)
  library(lubridate)
      library(plotly)
    df <- data.frame(matrix(c("2017-09-04","2017-09-05","2017-09-06","2017-09-07","2017-09-08",103,104,105,106,107,17356,18022,17000,20100,15230),ncol = 3, nrow = 5)) 
    colnames(df) <- c("DATE","ORDER_ID","SALES")
    df$DATE <- as.Date(df$DATE, format = "%Y-%m-%d")
    df$SALES <-  as.numeric(as.character(df$SALES))
    df$ORDER_ID <-  as.numeric(as.character(df$ORDER_ID))

    TOTALSALES <- df %>% select(ORDER_ID,DATE,SALES) %>% mutate(weekday = wday(DATE, label=TRUE)) %>%  mutate(DATE=as.Date(DATE)) %>% filter(!wday(DATE) %in% c(1, 7) & !(DATE %in% as.Date(c('2017-01-02','2017-02-27','2017-02-28','2017-04-14'))) ) %>% group_by(day=floor_date(DATE,"day")) %>% summarise(sales=sum(SALES)) %>% data.frame()

 TOTALSALES <- ggplot(TOTALSALES ,aes(x=day,y=sales,text=paste('Vendas (R$):', format(sales,digits=9, decimal.mark=",",nsmall=2,big.mark = "."),'<br>Data: ',format(day,"%d/%m/%Y"))))+ geom_point(colour = "black", size = 1)+stat_smooth() +labs(title='TOTAL SALES',x='dias',y='valor')+ scale_x_date(date_minor_breaks = "1 week")
m <- list(
  l = 120,
  r = 2,
  b = 2,
  t = 50,
  pad = 4
)
TOTALSALES <- ggplotly(TOTALSALES,tooltip = c("text")) %>% config(displayModeBar = F) %>% layout(autosize = F, width = 1000, height = 500, margin = m,xaxis = list(
      zeroline = F
    ),
    yaxis = list(
      hoverformat = '.2f'
    ))

TOTALSALES

标签: rggplot2plotly

解决方案


推荐阅读