# How to create a ListLinePlot with a layout analogous to ChartLayout -> “Stacked”?

I am using 3 checkboxes as desktops, portables and ipods in such a way that when I click to desktops it should shows ListLinePlot of p1, when I click to portables, it should show ListLinePlot of p2 and in the same way, when I click to ipods, it shows ListLinePlot of p3.

p1 = {.955, .914, .956, 1.516, 1.355};
p2 = {1.462, 1.357, 1.577, 1.917, 2.054};
p3 = {0,0, 1.57, 1.619, 3.997};

Column[{CheckboxBar[
Dynamic[z], {ListLinePlot[p1, Filling -> Axis,
FillingStyle -> Blue] -> Desktop,
ListLinePlot[p2, Filling -> Axis, FillingStyle -> LightBlue] ->
Portables,
ListLinePlot[p3, Filling -> Axis, FillingStyle -> Green] -> Ipods}], Spacer[5], Dynamic[z]}]


This is working correctly. But I want when I click both desktops and portables on, it should show the plots of p1 and p2 in a stacked way. Here I am explaining it with the help of BarChart because I have no idea whether 'ListLinePlotcan do this .

BarChart[Table[{p1[[i]], p2[[i]]}, {i, 1, 5, 1}], ChartLayout -> "Stacked"]


If I click Desktops, Portables and ipods, then it should dynamically take data of p1, p2 and p3 in stacked order as

BarChart[Table[{p1[[i]],p2[[i]], p3[[i]]}, {i, 1, 5, 1}], ChartLayout -> "Stacked"]


and so on. But I want stacked ListLinePlot and I don't know any function like ChartLayout in BarChart. I have seen the Accumulate function but it does not work as I wanted. For more clarification, just have a look at

http://graphics.thomsonreuters.com/12/01/US_APPLEBREAK0112_VT.html

-
By "stacked" do you mean that you need the sum of datasets shown? –  Szabolcs May 30 '12 at 8:12
Jennifer, I have cleaned up and formatted your question. Please try to do it yourself the next time. –  Mr.Wizard May 30 '12 at 8:14
Yes. By stacked, I meant that first listlineplot should begin from axis (as always) and the other listlineplot should begin from the first listlineplot. –  Jennifer May 30 '12 at 8:30
Ok. Mr. Wizard, I will do from next time. –  Jennifer May 30 '12 at 8:33
@Jennifer I'm a bit confused here. Both Szabolcs's and Mike's answer are consistent with the link in your question. In all these the lines are connecting data points belonging to the same device. Yet in your comment to Szabolcs's answer you seem to be asking for a graph where the data points belonging to the same year are connected instead which is different from the plot in the link you referred to. –  Heike May 31 '12 at 12:13

I don't believe there is a built-in function to do this automatically, so you will need to implement it from scratch. It's easy, it just takes a little bit of work.

A useful function is Accumulate, if your datasets are of equal length. If they are not, you may want to linearly interpolate them (Interpolation) and use Plot instead of ListPlot.

Another useful tool is the Filling option of plotting functions, which you are already familiar with. Look up its advanced syntax in the documentation.

This implementation is a little clumsy, but it could be a start:

Column[{Dynamic@
ListLinePlot[
Accumulate[{ConstantArray[0, Length[p1]], If[q1, p1, 0],
If[q2, p2, 0], If[q3, p3, 0]}],
Filling -> {2 -> {{1}, LightPurple}, 2 -> {{3}, LightOrange},
3 -> {{4}, LightGreen}}, PlotStyle -> Black],
Grid[{{Labeled[Checkbox@Dynamic[q1], "Desktop"],
Labeled[Checkbox@Dynamic[q2], "Portables"],
Labeled[Checkbox@Dynamic[q3], "iPods"]}}, Spacings -> 1]
}]


-
Beaten by both of you. I surely am "Fastest Gun in the West" no longer. –  Mr.Wizard May 30 '12 at 8:37
@Mr.Wizard haven't been on here much in recent weeks. I just stumbled across this at the right time. You are still the fastest gun in the west :) –  Mike Honeychurch May 30 '12 at 8:49
@Mike I'm afraid that's not it: I found the question right after it was posted. It just took me longer than you guys to get a nice working answer. I'll guard my pride by pretending that the three minutes I took to edit the question made the difference. :o) (Honestly the reordering nature of CheckboxBar was giving me fits.) –  Mr.Wizard May 30 '12 at 8:52
@Mr.Wizard I gave up on CheckboxBar quickly and went with Checkbox instead. –  Szabolcs May 30 '12 at 8:55
@Jennifer ListLinePlot[{{.955,1.462},{.914,1.357},{.956,1.577},{1.516,1.917},{1.355,2.054‌​}}] is something completely different from the BarChart you showed. Can you update your question and clarify, preferably including an image as well? –  Szabolcs May 30 '12 at 10:12

I'm assuming you want BarChart but it isn't 100% clear from your question. You might possibly be wanting a cumulative list plot?

p = {{.955, .914, .956, 1.516, 1.355}, {1.462, 1.357, 1.577, 1.917,
2.054}, {0, 0, 1.57, 1.619, 3.997}};

Column[{
CheckboxBar[Dynamic[z], {1, 2, 3}],
Spacer[{0, 20}],
Dynamic@If[z =!= {},
BarChart[p[[#]] & /@ Sort[z], ChartLayout -> "Stacked",
ImageSize -> 400],
Spacer[0]
]
}]


For a cumulative plot you could do this:

    Column[{
CheckboxBar[Dynamic[z], {1, 2, 3}],
Spacer[{0, 20}],
Dynamic@If[z =!= {},
ListLinePlot[Accumulate[p[[Sort[z]]]],
Filling -> Axis,ImageSize -> 400],
Spacer[0]
]
}]


This can be made prettier but I think it seems to capture what you are after.

-
You could use Accumulate[p[[z]]] instead of Transpose[Accumulate /@ Transpose[p[[#]] & /@ z]] –  Heike May 30 '12 at 9:09
@Heike yes you're correct. When I first tried this I hadn't defined p correctly so Accumulate[p[[z]]] didn't work. But then I fixed p` and went off on a tangent! –  Mike Honeychurch May 30 '12 at 11:49
@Heike: thanks for reply. But this is not what I actually wanted. I have updated my question and added a link to it which will more clarify my question. I wanted to make my code for product button given in the link. –  Jennifer May 31 '12 at 11:48