使用Plotly

时间:2018-06-06 02:30:22

标签: r ggplot2 plotly

我使用ggplot2创建了一个相对较好的点图。我很乐意使用Plotly,但是当我这样做时 - 它最终会扰乱y轴并使传奇变得非常不稳定。我将在下面之前和之后张贴一些,但我对两者都很新,并寻找正确的方向。 ggplot2还可以,但是对于我们正在做的事情,增加的情节交互将是一个巨大的胜利。另外一个奇怪的注意 - 返回的顶部图表似乎切断了情节(最高值 - 不知道为什么)。谢谢。

代码是:

library(ggplot2)
library(dplyr)
library(plotly)
library(sqldf)
library(tidyverse)
library(lubridate)
library(rio) #lets you use "import" for any file - without using extension name
options(scipen =999) #disable scientific notation



#prepare data:

setwd("C:/Users/hayescod/Desktop/BuysToForecastTracking")
Buys_To_Forecast <- import("BuysToForecastTrack")

colnames(Buys_To_Forecast) <- c("Date", "BusinessSegment", "Material", "StockNumber", "POCreatedBy", "PlantCode", "StockCategory", "Description", "Excess", "QuantityBought", "WareHouseSalesOrders", "GrandTotal", "Comments" )
Buys_To_Forecast$PlantCode <-factor(Buys_To_Forecast$PlantCode) #update PlantCode to factor


#use SQL to filter and order the data set: 

btf <- sqldf("SELECT Date, 
                     SUM(QuantityBought) AS 'QuantityBought', 
                     Comments
              FROM   Buys_To_Forecast 
              GROUP BY Date, Comments
              ORDER BY Date")
#use ggplot:

btfnew <- ggplot(data=btf, aes(x=Date, y=QuantityBought, color=Comments, size=QuantityBought)) + 
  geom_point() + 
  facet_grid(Comments~., scales="free")+ 
  ggtitle("Buys To Forecast Review")+
  theme(plot.title = element_text(hjust = 0.5), 
        axis.title.x = element_text(color="DarkBlue", size = 18), 
        axis.title.y = element_text(color="Red", size = 14))

btfnew #display the plot in ggplot

ggplotly(btfnew) #display the plot in Plotly

0 个答案:

没有答案