3
$\begingroup$

A minimal working example is given via:

{{{1, 4} -> "A"}, {{2, 2} -> "B"}, {{3, 1} -> "C"}, {{3, 7} ->   "D"}, {{4, 5} -> "E"}, {{4, 5} -> "F"}, {{5, 3} -> "G"}, {{6, 6} ->
    "H"}}
ListPlot[%]

enter image description here

As can be seen, "E" and "F" have not been labeled. I am happy about a general solution but also a fix-around for this specific problem would be great.

$\endgroup$

3 Answers 3

4
$\begingroup$

This solution will Merge duplicate keys by joining their Value strings using "," as a separator.

data = {{{1, 4} -> "A"}, {{2, 2} -> "B"}, {{3, 1} -> "C"}, {{3, 7} -> 
    "D"}, {{4, 5} -> "E"}, {{4, 5} -> "F"}, {{5, 3} -> 
    "G"}, {{6, 6} -> "H"}}

data // Flatten // GatherBy[#, First] & // 
    Merge[StringJoin@StringRiffle[#, ","] &] // Normal // 
  List /@ # & // ListPlot

enter image description here

$\endgroup$
2
$\begingroup$

Is this acceptable as a work-around?

list = {{{1, 4} -> "A"}, {{2, 2} -> "B"}, {{3, 1} -> "C"}, {{3, 7} -> 
     "D"}, {Callout[Callout[{4, 5}, "E", Right], "F", 
     Left]}, {{5, 3} -> "G"}, {{6, 6} -> "H"}};
ListPlot[list, PlotRange -> All]

plot

$\endgroup$
2
$\begingroup$

Here is a procedure to achieve your aim: First we gather all parts with the same position:

(old = Select[GatherBy[lab, (#[[1, 1]]) &], Length[#] > 1 &])

Then we join the labels for points with equal positions:

new = ({#[[1, 1, 1]] -> ToString[Join[#[[All, 1, 2]]]]}) & /@ (old = 
     Select[GatherBy[lab, (#[[1, 1]]) &], Length[#] > 1 &]);

Then we replace the parts with equal positions by the joined labels:

lab = Join[DeleteElements[lab, Flatten[old, 1]], new];

All together:

lab = {{{1, 4} -> "A"}, {{2, 2} -> "B"}, {{3, 1} -> "C"}, {{3, 7} -> 
     "D"}, {{4, 5} -> "E"}, {{4, 5} -> "F"}, {{5, 3} -> 
     "G"}, {{6, 6} -> "H"}};
new = ({#[[1, 1, 1]] -> ToString[Join[#[[All, 1, 2]]]]}) & /@ (old = 
     Select[GatherBy[lab, (#[[1, 1]]) &], Length[#] > 1 &]);
lab = Join[DeleteElements[lab, Flatten[old, 1]], new];
ListPlot[lab]

enter image description here

$\endgroup$

Your Answer

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

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