In this example, we shall use the worldPop1960to1997 dataset below, which contains population data for different age groups in various
countries for the years 1960-1997. Note that the dataset is already sorted based on Year, which is the first column.
countries for the years 1960-1997. Note that the dataset is already sorted based on Year, which is the first column.
In[]:=
worldPop1960to1997=
;
We shall add some computed columns to the dataset. These columns will be used as input to the BubbleChart function.
First, we create a column for the portion of the population that is in the 0 to 19 age group.
First, we create a column for the portion of the population that is in the 0 to 19 age group.
In[]:=
worldPop=Dataset[Map[Append[#,"Portion0to19"#["Age0to19"]/#["Pop"]]&,worldPop1960to1997]]
Out[]=
Next, we create an additional computed column for the proportion of the population that is in the age 60 or older group.
In[]:=
worldPop=Dataset[Map[Append[#,"Portion60orOlder"#["Age60orOlder"]/#["Pop"]]&,worldPop]]
Out[]=
Now we define a function that creates a Bubble Chart for a given year.
Our function assumes that the first input parameter is a dataset whose columns match those in the worldPop dataset above.
Our function assumes that the first input parameter is a dataset whose columns match those in the worldPop dataset above.
In[]:=
(*defineafunctionwhichcreatesaBubbleChartforagivenyear*)bubbleChartForYear::usage="bubbleChartForYear[worldPopDataIn,yearIn] creates a BubbleChart for yearIn based on the observations associated worldPopDataIn";bubbleChartForYear::invalidArg1="First input parameter must be a Dataset.";bubbleChartForYear::invalidYear="Year value is invalid.";bubbleChartForYear::colNotFound="Input Dataset must have a column named `1`.";bubbleChartForYear::noDataForYear="No data found for year specified.";bubbleChartForYear[worldPopDataIn_,yearIn_,opts___?OptionQ]:=Module[{k=0,colList,colsNeeded,worldPopDataForYearIn,xyrOnly,regionsOnly,regionsNoDups,regionColors,countriesOnly,bubbleData={},ndx},If[Head[worldPopDataIn]=!=Dataset,Message[bubbleChartForYear::invalidArg1];Abort[]];k=yearIn-1960+1;If[k<1||k>Length[worldPopDataIn],Message[bubbleChartForYear::invalidYear];Abort[]];(*checkcolumns*)colsNeeded={"Year","Country","Region","Pop","Portion0to19","Portion60orOlder"};colList=Keys[Normal[worldPopDataIn[[1]]]];For[k=1,k<=Length[colsNeeded],k++,If[!MemberQ[colList,colsNeeded[[k]]],Message[bubbleChartForYear::colNotFound,colsNeeded[[k]]];Abort[]];];worldPopDataForYearIn=Select[worldPopDataIn,#["Year"]yearIn&];If[Length[worldPopDataForYearIn]==0,Message[bubbleChartForYear::noDataForYear];Abort[]];xyrOnly=worldPopDataForYearIn[All,{"Portion0to19","Portion60orOlder","Pop"}];xyrOnly=Values[Values[Normal[xyrOnly]]];regionsOnly=worldPopDataForYearIn[All,"Region"];regionsOnly=Values[Normal[regionsOnly]];(*createalistofcolorsassociatedwiththeregionsinthedataset.Thislistwillbeusedtodefinealegendfortheplot.*)regionsNoDups=DeleteDuplicates[regionsOnly];regionColors={};For[k=1,k<=Length[regionsNoDups],k++,Switch[regionsNoDups[[k]],"Asia",AppendTo[regionColors,Green],"Australia",AppendTo[regionColors,Black],"North America",AppendTo[regionColors,Orange],"South America",AppendTo[regionColors,Blue],"Europe",AppendTo[regionColors,Red],_,AppendTo[regionColors,LightGray]];];(*Createa"wrapper"foreachelementinthebubbleDatalist.Whentheuserhoverstheircursoroverabubbleintheplot,thenameofthecountryassociatedwiththatbubblewillappearinthestatusbaratthebottomofthewindow.*)countriesOnly=worldPopDataForYearIn[All,"Country"];countriesOnly=Values[Normal[countriesOnly]];bubbleData=MapIndexed[StatusArea[#1,countriesOnly[[First[#2]]]]&,xyrOnly];BubbleChart[bubbleData,BubbleScale->"Diameter",FrameTrue,PlotRange{{0.55,0.85},{0,0.16}},ChartLegends->regionsNoDups,ChartStyle->regionColors,ColorFunction->Function[{x,y,r},ndx=First[FirstPosition[xyrOnly,{x,y,r}]];Switch[regionsOnly[[ndx]],"Asia",Green,"Australia",Black,"North America",Orange,"South America",Blue,"Europe",Red,_,LightGray]],ColorFunctionScaling->False,FrameLabel{"portion 0 to 19","portion 60 or Older"},opts]]
Lastly, we use Mathematica’s Manipulate function to create a dynamic bubble chart. When you drag the
“year” slider at the top, the plot will update to reflect the world population for the year associated with the
position of the slider. As you drag the button for the“year” slider, you can see how populations change over time
for the countries involved in the data set.
The size of a particular bubble for a given year k reflects the population of the country associated with the bubble,
for year k. As time progresses, you can see that the portion of the population that is 0-19 years decreases, and the
portion of the population that is 60 years or older increases.
To see which year is being displayed for a given setting on the “year” slider, click the “+“ at the right end of the slider.
This also reveals other controls that you can use to “play” the sequence of bubble charts, control its direction and
speed, etc.
“year” slider at the top, the plot will update to reflect the world population for the year associated with the
position of the slider. As you drag the button for the“year” slider, you can see how populations change over time
for the countries involved in the data set.
The size of a particular bubble for a given year k reflects the population of the country associated with the bubble,
for year k. As time progresses, you can see that the portion of the population that is 0-19 years decreases, and the
portion of the population that is 60 years or older increases.
To see which year is being displayed for a given setting on the “year” slider, click the “+“ at the right end of the slider.
This also reveals other controls that you can use to “play” the sequence of bubble charts, control its direction and
speed, etc.