情节GGplot-情节敏感

时间:2018-09-22 22:28:49

标签: r ggplot2 plotly

代码输出是我希望它响应的图,可以根据窗口尺寸进行调整。 仅使用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

0 个答案:

没有答案