3
$\begingroup$

For my school project I've decided to take some data from Wolfram. I have the dataset from Data Repository, and I want to create three BarCharts. The first one shows the highest number of fatalities. It's done.

The second should represent the the number of school shootings by year. The third one will represent the total number of school shooting victims by year.

Can I get some help with the second and the third charts?

dataM = ResourceData["Mass Shootings in America"]
dataM1 = dataM[All, {"Date", "Location", "Total Number of Fatalities"}]
BarChart[dataM1[All, "Total Number of Fatalities"]]
$\endgroup$
  • 1
    $\begingroup$ Check also community.wolfram.com/groups/-/m/t/873886 $\endgroup$ – Lou Jun 7 '18 at 19:24
  • $\begingroup$ Does ResourceData["Mass Shootings in America"] give only school shootings or all shootings? $\endgroup$ – QuantumDot Jun 7 '18 at 20:52
  • $\begingroup$ @QuantumDot The data set includes all shootings. The "School Related"=="Yes" field selects the school shootings. $\endgroup$ – creidhne Jun 7 '18 at 21:07
  • $\begingroup$ @Colton Does one of the answers below satisfy your requirements? If so, it is considered good practice to accept the one that best answers your question (see also What should I do when someone answers my question?). $\endgroup$ – MarcoB Jun 25 '18 at 19:22
4
$\begingroup$
  • To chart the number of school shootings by year:

First, use Select to find the school-related events, then CountsBy groups and counts the events for each year.

dataSet2 = 
  CountsBy[DateValue[#["Date"], "Year"] &][
    Select[dataM[All, {"Date", "School Related"}],
      #["School Related"] === "Yes" &]] // Normal;

BarChart[dataSet2 // Values,
  PlotLabel -> "Number of School Shootings by Year", 
  ChartLabels -> Placed[dataSet2 // Keys, Axis, Rotate[#, Pi/4] &],
  ImageSize -> Large]

school shootings by year

By way of comparison, the number of all mass shooting victims and the number of school shooting victims are easy to chart.

  • To find the number of mass shooting victims by year:

Use GroupBy to collect events by year, then Total adds up the number of victims for each year.

dataSet3 = 
  Total /@ GroupBy[dataM[All, {"Date", "Total Number of Victims"}], 
    DateValue[#["Date"], "Year"] &][All, All,"Total Number of Victims"] // Normal;

BarChart[dataSet3 // Values,
  PlotLabel -> "Number of Victims by Year",
  ChartLabels -> Placed[dataSet3 // Keys, Axis, Rotate[#, Pi/4] &],
  ImageSize -> Large]

mass shooting victims by year

  • Chart the number of school shooting victims:

Redraw the total victims chart by selecting only the school-related events.

dataSet4 =
  Total /@ GroupBy[
    Select[dataM[
      All, {"Date", "Total Number of Victims", "School Related"}],
      #["School Related"] === "Yes" &], 
    DateValue[#["Date"], "Year"] &][All, All,"Total Number of Victims"] // Normal;

BarChart[dataSet4 // Values, 
  PlotLabel -> "Number of School Shooting Victims by Year", 
  ChartLabels -> Placed[dataSet3 // Keys, Axis, Rotate[#, Pi/4] &], 
  ImageSize -> Large]

school shooting victims by year

$\endgroup$
2
$\begingroup$

For the shootings by year, consider for instance:

BarChart[
   CountsBy[First@DateList@#["Date"] &][dataM], 
   BarOrigin -> Left, ChartLabels -> Placed[Automatic, Left]
]

yearly shootings

$\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.