6
$\begingroup$

How can I plot the frequency of list items such as?: {62, 62, 62, 64, 64, 64, 64, 64, 64, 66, 66, 66, 66, 67, 67, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 91, 91, 91, 91, 91, 91, 94, 95, 95, 95, 95, 100, 100, 100, 100, 100, 100}

Also, would it be possible to have the list replace the item with a name? For example, instead of showing that 62 appears 3 times, show that "Joe" has 3 matches in the list?

I apologize if this is a trivial matter; I've been searching for a few days.

$\endgroup$
  • $\begingroup$ Possible duplicate: (9801) $\endgroup$ – Mr.Wizard Sep 20 '14 at 9:16
4
$\begingroup$
data1 = {62, 62, 62, 64, 64, 64, 64, 64, 64, 66, 66, 66, 66, 67, 67, 
   82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 91, 91, 91, 91, 91,
    91, 94, 95, 95, 95, 95, 100, 100, 100, 100, 100, 100};

names = {62 -> "Joe", 64 -> "Sharon", 66 -> "Bill",
   67 -> "Kelly", 82 -> "Jim", 91 -> "Sue",
   94 -> "Mike", 95 -> "Alice", 100 -> "Bob"};

freq1 = Tally[data1]

{{62, 3}, {64, 6}, {66, 4}, {67, 2}, {82, 12}, {91, 6}, {94, 1}, {95, 4}, {100, 6}}

ListPlot[freq1, Filling -> Axis]

enter image description here

data2 = data1 /. names;

freq2 = Tally[data2]

{{"Joe", 3}, {"Sharon", 6}, {"Bill", 4}, {"Kelly", 2}, {"Jim", 12}, {"Sue", 6}, {"Mike", 1}, {"Alice", 4}, {"Bob", 6}}

StringForm["`1` has `2` matches", ##] & @@@ freq2

enter image description here

$\endgroup$
  • $\begingroup$ I recommend that you remove the line freq2 = freq1 /. names as this could easily lead to error as it will replaces counts as well as items. +1 however since you give the better way immediately below. $\endgroup$ – Mr.Wizard Sep 20 '14 at 6:59
  • $\begingroup$ @Mr.Wizard - Modified per your recommendation. Thanks. $\endgroup$ – Bob Hanlon Sep 21 '14 at 12:25
4
$\begingroup$

Using:

data1 = {62, 62, 62, 64, 64, 64, 64, 64, 64, 66, 66, 66, 66, 67, 67, 
   82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 91, 91, 91, 91, 91,
    91, 94, 95, 95, 95, 95, 100, 100, 100, 100, 100, 100};

names = {62 -> "Joe", 64 -> "Sharon", 66 -> "Bill", 67 -> "Kelly", 
   82 -> "Jim", 91 -> "Sue", 94 -> "Mike", 95 -> "Alice", 
   100 -> "Bob"};

You can useBarChart:

BarChart[Labeled[#2, #1 /. names] & @@@ Tally[data1]]

enter image description here

Or

Needs["StatisticalPlots`"]
ParetoPlot[data1, ChartLabels -> (SortBy[Tally[data1], -#[[2]]&][[All, 1]]/. names), 
 LabelingFunction -> (Placed[
     Panel[NumberForm[N@#1, 2], FrameMargins -> 0], Above] &)]

enter image description here

You can use StringTemplate for the textual aim,e.g.

s = StringTemplate["<*`1`/.names*> has `2` matches.\n"];
StringJoin @@ (TemplateApply[s, #] & /@ Tally[data1])

produces:

Joe has 3 matches. Sharon has 6 matches. Bill has 4 matches. Kelly has 2 matches. Jim has 12 matches. Sue has 6 matches. Mike has 1 matches. Alice has 4 matches. Bob has 6 matches.

$\endgroup$
2
$\begingroup$

For the plotting part of your question:

ListPlot[Tally[lst], Filling -> Axis]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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