0

I have 106 weeks data for 5 different LOB (Line of Business). The variables are Traffic, Spend, Clicks, etc. In total there will be 106*5 = 530 rows.

Dataframe looks like:

LOB Week Traffic Spend Clicks
A   1   34  12  5
A   2   37  32  6
A   3   41  57  7
A   4   52  42  12
A   5   27  37  8

 ... 106 weeks
B...106 weeks
C...106 weeks
D...106 weeks


E   1   43  22  12
E   2   65  16  14
E   3   76  18  9
E   4   25  14  11
E   5   53  15  15
... 106 weeks

I want to generate line chart for Traffic for all the 5 different LOB on the same chart, similarly for other metrics also. For this I have written a function but it is not doing what I want.

Code:

for ( i in seq(1,length( data),1) ) plot(data[,i],ylab=names(data[i]),type="l", col = "red", xlab = "Week", main = "")

Kindly suggest me how this can be done.

Ankita Patnaik
  • 271
  • 1
  • 7
  • Can you add in a description of what you want this to look like? Each LOB has its own panel of each metric, or each metric has a panel showing lines for each LOB, etc? – camille Jun 26 '18 at 14:21

1 Answers1

0

You can use ggplot2 :

ggplot(data, aes(x = Week, y = Traffic, color = LOB)) +
geom_line()

Please try to submit a toy example of your data so we can reproduce the code. See Here.

Edit: as suggested by @Axeman, you may want to plot all metrics together. Here is his solution for visibility:

d <- gather(data, metric, value, -Week, -LOB)
ggplot(d, aes(Week, value, color = LOB)) + 
geom_line() + 
facet_wrap(~metric, scales = 'free_y')
RLave
  • 8,144
  • 3
  • 21
  • 37
  • 2
    Possibly OP wants to plot the different metrics too. One could do `d <- gather(data, metric, value, -Week, -LOB); ggplot(d, aes(Week, value, color = LOB)) + geom_line() + facet_wrap(~metric, scales = 'free_y')` – Axeman Jun 26 '18 at 12:59