# Line is covered by PlotMarkers in ListLinePlot

Suppose I plot the data and fitting line with black point and red line

imgPlotMarkers00 =
ListLinePlot[{Range[60] + RandomReal[{-1, 1}/2, 60], Range[60]},
PlotMarkers -> {Automatic, None}, PlotStyle -> {Black, Red},
Joined -> {False, True}]


However, I found in the output figure

the black points cover up the red line. How could I move the red line to the top layer of figure? Is their a option for this?

This is a simple example. In real case I have more than one data-fit line pairs to plot and their legends to placed.

I prefer to control what goes over what with Prolog vs Epilog (another way is to use different orders inside Show but it is more cumbersome in this case, as it requires to split 1 plot into 2):

data={#,#+RandomReal[{-1,1}/2]}&/@Range[40];

ListLinePlot[data,PlotStyle->{Red,Thickness[.01]},
Prolog->{Black,PointSize[.02],Point[data]}]

ListLinePlot[data,PlotStyle->{Red,Thickness[.01]},
Epilog->{Black,PointSize[.02],Point[data]}]


If you need PlotLegends to work for multiple data you can put lines in Epilog/Prolog and points in PlotMarkers:

data1={#,#+RandomReal[{-1,1}/2]}&/@Range[40];
data2={#,5+#+RandomReal[{-1,1}/2]}&/@Range[40];

ListPlot[{data1,data2},
PlotLegends->{"data1","data2"},
PlotMarkers->{Automatic,Scaled[0.05]},
Epilog->{{Red,Thickness[.01],Line[data1]},
{Red,Thickness[.01],Line[data2]}}]


• emm...this is nice..but data in Prolog/Epilog would not show in PlotLegend.... Commented Jun 18, 2020 at 4:28
• @Harry You can put your line in Prolog and Epilog, and depict points with PlotMarkers. Then it will shoe up in PlotLegend doe multiple data. Commented Jun 18, 2020 at 4:32
• That's a good idea, I can plot points with PlotMarkers, plot line both in PlotMarkers/None and Epilog, then all thing will show up in PlotLegend! Commented Jun 18, 2020 at 4:55
• @Harry I just added a version of this in the post. Commented Jun 18, 2020 at 4:59
• @Harry and of course on top of that you can still use opacity for any of these components. Commented Jun 18, 2020 at 5:00

How could I move the red line to the top layer of figure?

May be change the opacity of the black points?

imgPlotMarkers00 =
ListLinePlot[{Range[60] + RandomReal[{-1, 1}/2, 60], Range[60]},
PlotMarkers -> {Automatic, None},
PlotStyle -> {{Opacity[.3], Black}, Red},
Joined -> {False, True}
]


You can post-process the ListLinePlot output to sort the graphics primitives so that lines come after markers:

ClearAll[linesInFront]
linesInFront = SubsetMap[Reverse, #, Join @@
Table[Position[#, {___, {___,  i}, ___}], {i, {_Line, _GeometricTransformation}}]] &;


Example:

data = {Range[40] + RandomReal[{-1, 1}/2, 40], Range[40]};

llp = ListLinePlot[Join[data, 10 + data],
PlotMarkers -> {{Automatic, Medium}, None, {Automatic, Medium}, None},
PlotStyle -> {Black, {Red, Thick}, Black, {Green, Thick}},
Joined -> {False, True, False, True},
ImageSize -> Medium, PlotLegends -> Automatic];

Row[{llp, linesInFront @ llp}, Spacer[10]]


Update: An alternative/simpler approach: Extract markers and use them as Prolog:

ClearAll[markersInBack]
markersInBack = Module[{markers = Cases[#, {___,_GeometricTransformation}, All]},
Show[DeleteCases[#, Alternatives @@ markers, All], Prolog -> markers]]&;

markersInBack @ llp


• This seems a really high level trick.... Commented Jun 21, 2020 at 15:14

Put the red line over the black points

v={Range[60] + RandomReal[{-1, 1}/2, 60], Range[60]};
Show[
ListLinePlot[v,PlotStyle->Black,Joined->False],
ListLinePlot[v,PlotStyle->Red,Joined->True]
]

Show[ListPlot[Range[60] + RandomReal[{-1, 1}/2, 60], PlotStyle -> Black],
ListPlot[Range[60], PlotStyle -> Red, Joined -> True]]