user8810618
user8810618

Reputation: 115

can't see any line figure in my graph even my code is correct

I want to plot 4 lines of sales in the same graph, each line present sales of such type of a magasin: a, b, c and d.

dataa<-data[which(StoreType=="a"),]
datab<-data[which(StoreType=="b"),]
datac<-data[which(StoreType=="c"),]
datad<-data[which(StoreType=="d"),]

for example :

datac
              Date StoreType       V1
 1: janvier_2013           c 26498298
 2: février_2013           c 24151938
 3: mars_2013              c 26881214
 4: avril_2013             c 25097237
 5: mai_2013               c 26713190
 6: juin_2013              c 26145149

then I create the horizontal axis which is the date axis:

Date<-unique(as.factor(data$Date))
unique(data$Date)
 [1] "janvier_2013  " "février_2013  " "mars_2013     " "avril_2013    " "mai_2013      " "juin_2013     " "juillet_2013  "
 [8] "août_2013     " "septembre_2013" "octobre_2013  " "novembre_2013 " "décembre_2013 " "janvier_2014  " "février_2014  "
[15] "mars_2014     " "avril_2014    " "mai_2014      " "juin_2014     " "juillet_2014  " "août_2014     " "septembre_2014"
[22] "octobre_2014  " "novembre_2014 " "décembre_2014 " "janvier_2015  " "février_2015  " "mars_2015     " "avril_2015    "
[29] "mai_2015      " "juin_2015     " "juillet_2015  "

Then I create the date frame which contains 31 rows:

Data2<-data.frame(Date,dataa,datab,datac,datad)

Finally, I used this code to plot the figure and it's fine working without any error:

ibrary(dplyr)
library(plotly)
p <- plot_ly(Data2, x = ~Date, y = ~dataa, name = 'Sales magazin a', type = 'scatter', mode = 'lines',
             line = list(color = 'rgb(205, 12, 24)', width = 4)) %>%
  add_trace(y = ~datab, name = 'Sales magazin b', line = list(color = 'rgb(22, 96, 167)', width = 4)) %>%
  add_trace(y = ~datac, name = 'Sales magazin c', line = list(color = 'rgb(205, 12, 24)', width = 4, dash = 'dash')) %>%
  add_trace(y = ~datad, name = 'Sales magazin d', line = list(color = 'rgb(22, 96, 167)', width = 4, dash = 'dash')) %>%
  layout(title = "Somme des ventes pour chaque magazin",
       xaxis = list(title = "Months_year"),
       yaxis = list (title = "Sales"))
p

But I get this empty graph:

enter image description here

How do you explain and resolve this please?

Upvotes: 2

Views: 41

Answers (1)

David Klotz
David Klotz

Reputation: 2431

Your dataframe is composed of multiple dataframes. For example, look at str(dataa). For plotting, you want to refer to columns (~ Date), not dataframes (~dataa).

You should reorganize your data in a way so that each store's data is in a column. Without changing your code too much, you could just do the following for all the subsets, then proceed as you did:

dataa<-data[which(StoreType=="a"),V1]
...

However, joining a bunch of subsets using data.frame could lead to errors if not every group is sorted by month correctly. A tidy approach would help make sure each store's Date is aligned correctly:

library(tidyr)
library(dplyr)

Data2 <- data %>%
           mutate(StoreType = paste0("data", StoreType)) %>% 
           spread(key = StoreType, value = V1)   

Then just use your plot_ly function on that version of Data2.

Upvotes: 1

Related Questions