I have Dataset
;
jexample = {<|"country" -> "Bhutan", "subregion" -> "South Asia",
"status" -> "LDC", "indicator" -> "GDP", "time" -> 2011.`,
"data" -> 5.`|>, <|"country" -> "Bhutan",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "GDP", "time" -> 2015.`,
"data" -> 7.`|>, <|"country" -> "Bhutan",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "EVI", "time" -> 2011.`,
"data" -> 11.`|>, <|"country" -> "Bhutan",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "EVI", "time" -> 2015.`,
"data" -> 7.`|>, <|"country" -> "Angola",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "GDP", "time" -> 2011.`,
"data" -> 3.`|>, <|"country" -> "Angola",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "GDP", "time" -> 2015.`,
"data" -> 7.`|>, <|"country" -> "Angola",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "EVI", "time" -> 2011.`,
"data" -> 10.`|>, <|"country" -> "Angola",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "EVI", "time" -> 2015.`,
"data" -> 5.`|>, <|"country" -> "Bangladesh",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "GDP", "time" -> 2011.`,
"data" -> 2.`|>, <|"country" -> "Bangladesh",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "GDP", "time" -> 2015.`,
"data" -> 6.`|>, <|"country" -> "Bangladesh",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "EVI", "time" -> 2011.`,
"data" -> 7.`|>, <|"country" -> "Bangladesh",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "EVI", "time" -> 2015.`,
"data" -> 9.`|>, <|"country" -> "Cambodia",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "GDP", "time" -> 2011.`,
"data" -> 6.`|>, <|"country" -> "Cambodia",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "GDP", "time" -> 2015.`,
"data" -> 3.`|>, <|"country" -> "Cambodia",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "EVI", "time" -> 2011.`,
"data" -> 5.`|>, <|"country" -> "Cambodia",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "EVI", "time" -> 2015.`,
"data" -> 6.`|>, <|"country" -> "Nepal",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "GDP", "time" -> 2011.`,
"data" -> 7.`|>, <|"country" -> "Nepal",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "GDP", "time" -> 2015.`,
"data" -> 9.`|>, <|"country" -> "Nepal",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "EVI", "time" -> 2011.`,
"data" -> 8.`|>, <|"country" -> "Nepal",
"subregion" -> "South Asia", "status" -> "LDC",
"indicator" -> "EVI", "time" -> 2015.`,
"data" -> 5.`|>, <|"country" -> "S. Islands",
"subregion" -> "SIDS", "status" -> "LDC", "indicator" -> "GDP",
"time" -> 2011.`, "data" -> 3.`|>, <|"country" -> "S. Islands",
"subregion" -> "SIDS", "status" -> "LDC", "indicator" -> "GDP",
"time" -> 2015.`, "data" -> 9.`|>, <|"country" -> "S. Islands",
"subregion" -> "SIDS", "status" -> "LDC", "indicator" -> "EVI",
"time" -> 2011.`, "data" -> 4.`|>, <|"country" -> "S. Islands",
"subregion" -> "SIDS", "status" -> "LDC", "indicator" -> "EVI",
"time" -> 2015.`, "data" -> 7.`|>, <|"country" -> "Kiribati",
"subregion" -> "SIDS", "status" -> "LDC", "indicator" -> "GDP",
"time" -> 2011.`, "data" -> 4.`|>, <|"country" -> "Kiribati",
"subregion" -> "SIDS", "status" -> "LDC", "indicator" -> "GDP",
"time" -> 2015.`, "data" -> 5.`|>, <|"country" -> "Kiribati",
"subregion" -> "SIDS", "status" -> "LDC", "indicator" -> "EVI",
"time" -> 2011.`, "data" -> 8.`|>, <|"country" -> "Kiribati",
"subregion" -> "SIDS", "status" -> "LDC", "indicator" -> "EVI",
"time" -> 2015.`, "data" -> 1.`|>, <|"country" -> "Vanuatu",
"subregion" -> "SIDS", "status" -> "ODC", "indicator" -> "GDP",
"time" -> 2011.`, "data" -> 6.`|>, <|"country" -> "Vanuatu",
"subregion" -> "SIDS", "status" -> "ODC", "indicator" -> "GDP",
"time" -> 2015.`, "data" -> 6.`|>, <|"country" -> "Vanuatu",
"subregion" -> "SIDS", "status" -> "ODC", "indicator" -> "EVI",
"time" -> 2011.`, "data" -> 3.`|>, <|"country" -> "Vanuatu",
"subregion" -> "SIDS", "status" -> "ODC", "indicator" -> "EVI",
"time" -> 2015.`, "data" -> 3.`|>};
(* Restructure "jexample" to create a new dataset "ds" *)
(*By @kglr*)
ClearAll[reOrg, ds, indicatorList, dsgrouped];
reOrg = Query[GroupBy[{#"time", #"country"} &] /* Values /* KeyUnion,
Apply[Join], <|KeyDrop[{"indicator", "data"}]@#, #indicator -> #data|> &];
ds = reOrg@jexample;
indicatorList = Normal[Query[First /* Keys]@ds][[5 ;;]];
dsgrouped =ds[GroupBy[#country &], GroupBy[#time &], All, indicatorList];
ClearAll[barChart, bcQ];
barChart[opts : OptionsPattern[]] :=
BarChart[#, opts, ChartLegends -> Automatic,
ChartLabels -> Automatic] &;
bcQ[opts : OptionsPattern[]][var_String, countryslices_: All,
timeslices_: All, f_: Identity] :=
Query[(countryslices /. {All -> Identity,
x_List :> KeyTake[x]}) /* (barChart[opts, PlotLabel -> var][
f[#]] &), timeslices /. x_List :> KeyTake[x],
First@*First@*Values@*KeyTake[var]]
bcQ[opts : OptionsPattern[]][var_List, rest___] :=
Row[Append[#[[All, 1]], #[[1, -1, 1, 1]]] &@
Through[(bcQ[opts][#, rest] & /@ var)@#], Spacer[10]] &
ClearAll[timeList, countryList];
timeList = Normal[Query[First /* Keys]@dsgrouped];
countryList = Normal@Keys@dsgrouped;
Manipulate[
bcQ[
ImageSize -> 350,
Joined -> ({Mean@#[[1]], Max@#[[2]]} &),
ChartStyle -> "Rainbow",
PlotTheme -> "Business",
AspectRatio -> 1/2.75
][Sort@vars, countries, times, gorder]@dsgrouped,
{{vars, indicatorList[[{1}]], "measures"}, indicatorList, TogglerBar},
{{countries, countryList[[{1}]], "countries"}, countryList,
TogglerBar},
{{times, timeList, "times"}, timeList, TogglerBar},
{{gorder, Identity,
"grouping order"}, {Identity ->
Column[{"country", "time"}, Alignment -> Center],
Transpose -> Column[{"time", "country"}, Alignment -> Center]}}
]
My purpose is to calculate Mean
value of GDP
across countries for the initial year 2011, associated with purple bars across countries, and draw a line on the bar chart. Similarly, I also want to draw a line at the mean value of the final year 2015, associated with the red bars, across the countries. To accomplish this task, I revised the above Manipulate
as follows:
Manipulate[
aveInitial=Max[Normal@Mean[ds[GroupBy["country"], GroupBy["time"], All,vars][All,First]]];
aveFinal=Max[Normal@Mean[ds[GroupBy["country"], GroupBy["time"], All,vars][All,Last]]];
aveI = Max[ds[GroupBy["country"], GroupBy["time"], All, vars][All, First]];
aveF = Max[ds[GroupBy["country"], GroupBy["time"], All, vars][All, Last]];
bcQ[
ImageSize -> 350,
Joined -> ({Mean@#[[1]], Max@#[[2]]} &),
ChartStyle -> "Rainbow",
PlotTheme -> "Business",
AspectRatio -> 1/2.75,
Epilog -> {
{Dashed, Red,
Tooltip[Line[{{0, aveInitial}, {0, Max[aveI] + 0.55}}], aveInitial],
Text["Average", {-1, aveInitial}, {-3, -1}]}, {Dashed, Blue,
Tooltip[Line[{{0, aveFinal}, {0, Max[aveF] + 0.55}}],
aveFinal], Text["Average", {-1, aveFinal}, {-3, -2}]}
}
][Sort@vars, countries, times, gorder]@dsgrouped,
{{vars, indicatorList[[{1}]], "measures"}, indicatorList, TogglerBar},
{{countries, countryList[[{1}]], "countries"}, countryList,TogglerBar},
{{times, timeList, "times"}, timeList, TogglerBar},
{{gorder, Identity, "grouping order"}, {Identity ->
Column[{"country", "time"}, Alignment -> Center],
Transpose -> Column[{"time", "country"}, Alignment -> Center]}}
]
In the correctly formatted code, I should have two horizontal lines: one for the mean value of the 2011 average across countries and another for the 2015 average across countries. These averages would allow me to compare the average change over time across countries. Of course, when I switch to time/country
dialogue box, then the two mean values should be calculated for the initial year over the list of countries, as well as over the final year across the list of countries.
However, there is an error in Epilog->...
and I cannot find out what it is.