2
$\begingroup$

I have a 3-column dataset where I show the probabilities of winning of teams in different years (team name, year, prob.)

I need to plot it with markers so the probabilities of each team can be identified over this period of time. This is how I coded the plot:

MLSscatter = 
  MLSdata[
    ListPlot[Tooltip[#], 
      PlotMarkers -> Automatic, 
      AxesLabel -> {"Year", "Prob"}, 
      ImageSize -> 700,
      PlotStyle -> {Blue, PointSize[Large]}] &, 
    {#Year, #Prob} &]

I assume I have to give the PlotMarkers -> Automatic some extra instructions to consider the column "team name", but I do not know how. Can somebody give me a hand with this?

Here it is a sample of the dataset: https://docs.google.com/spreadsheets/d/1o6Z8TyOk8qrfDidKUUPIhSDmxGF0no0AGtBvj8oEGks/edit?usp=sharing

Team    Year    Probability
Colorado Rapids 2004    0.347327073
Dallas  2004    0.337920147
San Jose    2004    0.35962753
Los Angeles Galaxy  2004    0.39816025
Kansas City 2004    0.384930343
Average 2004    0.365593069
Chivas  2005    0.228527286
Colorado Rapids 2005    0.346717
Dallas  2005    0.405828388
San Jose    2005    0.420191143
Los Angeles Galaxy  2005    0.39360505
R. Salt Lake    2005    0.260887715
Average 2005    0.342626097
Chivas  2006    0.328711988
Colorado Rapids 2006    0.354479334
Dallas  2006    0.417429584
Houston 2006    0.393905569
Los Angeles Galaxy  2006    0.34447955
R. Salt Lake    2006    0.284803141
Average 2006    0.353968194
Chivas  2007    0.38657813
Colorado Rapids 2007    0.341129973
Dallas  2007    0.38977568
Los Angeles Galaxy  2007    0.352687067
R. Salt Lake    2007    0.265266957
Houston 2007    0.406474713
Average 2007    0.35698542
Houston 2008    0.414484773
Chivas  2008    0.377620317
R. Salt Lake    2008    0.32784385
Colorado Rapids 2008    0.346302497
Dallas  2008    0.352971193
San Jose    2008    0.281366727
Los Angeles Galaxy  2008    0.36859292
Average 2008    0.352740325
$\endgroup$
2
  • $\begingroup$ To come up with an answer, most of us, would want to run tests with your code and your data. But you give us code without a sample of your data. Do you expect us to manufacture our the data our own? I think you are unlikely to get an answer unless you edit your question to include a sample of the data needed to reproduce your problem. $\endgroup$
    – m_goldberg
    Commented May 19, 2016 at 14:13
  • $\begingroup$ It is the first time I use this website and I do not really know how it works. You should be able to access the dataset now with the link. Thanks for your time $\endgroup$
    – Karl
    Commented May 19, 2016 at 18:10

1 Answer 1

5
$\begingroup$

Assuming that you copy your data and import it, e.g. using data = ImportString["yourdatahere", "TSV"][[2;;]], then the following should work for you:

ListPlot[
 GroupBy[data, First -> (Tooltip[Rest[#], First[#]] &)], 
 PlotMarkers -> {Automatic, 14},
 AxesLabel -> {"Year", "Prob"},
 PlotStyle -> Blue
]

according to original request

In the expression data = ImportString["yourdatahere", "TSV"][[2;;]], the [[2;;]] is there to get rid of the headers, i.e. the first row of your dataset.

I could not interpret what you meant in your ListPlot syntax. The key of your problem, i.e. assigning tooltips to markers, and grouping performance by year, are taken care of by the following:

 GroupBy[data, First -> (Tooltip[Rest[#], First[#]] &)]

This parses your data, grouping it in an association object by the first element of each row (i.e. the team name); the values in this association are also decorated via tooltips bearing the name of the team. These tooltips will be honored by ListPlot, so you'll see that if you mouseover any of your plot markers in the plot above the name of the corresponding team will appear in a pop up.

Note also that PlotStyle -> {Blue, PointSize[Large]} will assign the Blue style to the first data series in your plot, the Large style to the second, and so on. If you want to assign them both to all data series, you will want to wrap them in Directive, i.e. PlotStyle -> Directive[Blue, PointSize[Large]]. Even then, size of the markers is best controlled through PlotMarkers directly, i.e. PlotMarkers -> {Automatic, yourDesiredSize}; this will also apply variable coloring, so if you want to maintain the blue coloring throughout, you want to follow that with PlotStyle -> Blue, as I did above.

Finally, I'd suggest the following to take advantage of color as well:

ListPlot[
 GroupBy[data, First -> (Tooltip[Rest[#], First[#]] &)],
 Frame -> {True, True, False, False},
 FrameLabel -> (Style[#, 14] & /@ {"Year", "Probability"}),
 PlotMarkers -> {Automatic, 18}
]

in color


In response to the comment asking to highlight one team by connecting the corresponding plot markers, here is an interactive implementation:

grouped = GroupBy[data, First -> (Tooltip[Rest[#], First[#]] &)];

Manipulate[
 ListPlot[
  grouped,
  PlotMarkers -> {Automatic, 14},
  AxesLabel -> {"Year", "Prob"},
  PlotStyle -> Blue,
  Prolog -> {
    Red, Thick,
    Line[grouped[team] /. Tooltip[a_, _] :> a]
  }
 ],
 {team, Keys@grouped}
]

highlighted team

If you simply want each team to have its own line connecting the corresponding markers, then the Joined -> True option of ListPlot is for you:

ListPlot[
 GroupBy[data, First -> (Tooltip[Rest[#], First[#]] &)], 
 PlotMarkers -> {Automatic, 14},
 AxesLabel -> {"Year", "Prob"},
 Joined -> True
]

joined

(In this case I would urge the use of colors to distinguish the lines)

$\endgroup$
8
  • $\begingroup$ this is a great clear exposition. +2 :) $\endgroup$
    – ubpdqn
    Commented May 20, 2016 at 8:49
  • $\begingroup$ This is an awesome explanation! It works perfectly! The "groupby" command was the one I was missing. $\endgroup$
    – Karl
    Commented May 20, 2016 at 9:04
  • $\begingroup$ One last thing, is it possible to connect the markers of one team with a line over the years so you can differentiate it from the rest? $\endgroup$
    – Karl
    Commented May 20, 2016 at 9:07
  • $\begingroup$ @Karl Take a look at the updated answer, and let me know if either approach fits your need. $\endgroup$
    – MarcoB
    Commented May 20, 2016 at 18:09
  • $\begingroup$ @MarcoB That was it. I'll try it now but it should work. :) Great contribution $\endgroup$
    – Karl
    Commented May 23, 2016 at 9:22

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.