2
$\begingroup$

is there any way to create a lollipop chart on the Y axis? I see the DiscretePlot function could make a lollipop chart but the chart is on the X axis. I would like to make a lollipop chart similar to https://stackoverflow.com/questions/42657949/create-a-lollipop-chart-to-compare-groups

enter image description here

Thanks a lot!

$\endgroup$
4
  • 1
    $\begingroup$ I’d probably build it up from Graphics primitives (Line, Point, ...). Can you provide a dataset to play with? $\endgroup$
    – MarcoB
    Commented Jan 27, 2021 at 5:03
  • $\begingroup$ Thanks for your reply Marco! How about "DiscretePlot[PrimePi[k], {k, 1, 10}]"? Is there any way to make this lollipop chart on Y axis? $\endgroup$
    – Frankie
    Commented Jan 27, 2021 at 5:15
  • 1
    $\begingroup$ At least for your simple example, I'd just swap coordinates: Show[MapAt[# /. v_ /; VectorQ[v, NumberQ] :> Reverse[v] &, DiscretePlot[PrimePi[k], {k, 1, 10}], 1], AspectRatio -> GoldenRatio, AxesOrigin -> {0, 0}, PlotRange -> All] $\endgroup$ Commented Jan 27, 2021 at 5:26
  • $\begingroup$ Thanks ennui! Your solution works! Thank you $\endgroup$
    – Frankie
    Commented Jan 27, 2021 at 5:48

1 Answer 1

4
$\begingroup$
dp = DiscretePlot[PrimePi[k], {k, 1, 10}, 
  PlotStyle -> PointSize[Large], Frame -> True, 
  FrameLabel -> {"A", "B"}]

enter image description here

1. To make the filling lines go from the vertical axis the the points (without changing the coordinates of points), you can post-process to modify the lines:

Show[dp /. Line[a : {__List}, c__] :> {Gray, 
    Line[a /. {{x1_, y1_}, {x2_, y2_}} :> {{x1, y1}, {0, y1}}]}, 
 PlotRange -> All, Axes -> False]

![enter image description here

2. To flip the vertical and horizontal axes, you can use

Show[MapAt[GeometricTransformation[#, ReflectionTransform[{-1, 1}]] &, dp, {1}], 
 PlotRange -> All, AxesOrigin -> {0, 0}, FrameLabel -> {"B", "A"}]

enter image description here

Update: You can also use BubbleChart and add the horizontal lines as Prolog:

SeedRandom[1]
lists = {{#, RandomInteger[{1, 5}]} & /@ 
    Sort[RandomSample[Range[15]], 10], {#, RandomInteger[{1, 5}]} & /@
     Sort[RandomSample[Range @ 15], 7]};

bcdata = lists /. {x_?NumericQ, y_?NumericQ} :> 
   Labeled[{y, x, 1}, Style[y, 16, Italic, Black], Center]; 

BubbleChart[bcdata, 
 ChartStyle -> {RGBColor[1, 0.4, 0.4], RGBColor[0., 0.75, 0.75]}, 
 ImageSize -> Large, BubbleSizes -> {.06, .06}, 
 ChartElementFunction -> "SphereBubble", 
 GridLinesStyle -> LightGray, GridLines -> All, 
 Prolog -> {AbsoluteThickness[5], Gray, Line[{{0, #[[1]]}, Reverse@#}] & /@ 
    KeyValueMap[List][GroupBy[Join @@ lists, First -> Last, Max]]}]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks for your quick reply! However, I need to switch the Y axis and the A axis so that the Y axis indicates the steps while the X axis indicates the values. I know how to make it in BarChart. "BarOrigin -> Left" works for the purpose but I don't know how to achieve this kind of effect in line plots. $\endgroup$
    – Frankie
    Commented Jan 27, 2021 at 5:45
  • $\begingroup$ @Frankie, so the second method gives what you need? $\endgroup$
    – kglr
    Commented Jan 27, 2021 at 5:47
  • $\begingroup$ Thanks kglr. It works! Respect! $\endgroup$
    – Frankie
    Commented Jan 27, 2021 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.