0
$\begingroup$

I have the following code to generate two separate bar charts. I like to create a new bar chart by extracting the data from the following barcharts.

bc1 = BarChart[
   <|"Cambodia-Dec" -> <|"National" -> 75.4, "Urban" -> 61.9, 
      "Rural" -> 77.7|>,
    "Indonesia-May" -> <|"National" -> 79.8, "Urban" -> 78.7, 
      "Rural" -> 81.8|>,
    "Lao PDR-Jul" -> <|"National" -> 27.0, "Urban" -> 28.8, 
      "Rural" -> 26.0|>,
    "Mongolia-Dec" -> <|"National" -> 17.5, "Urban" -> 19.3, 
      "Rural" -> 14.1|>, 
    "Myanmar-Oct" -> <|"National" -> 52.8, "Urban" -> 54.9, 
      "Rural" -> 51.7|>,
    "Philippines-Dec" -> <|"National" -> 75.8, "Urban" -> 78.4, 
      "Rural" -> 72.9|>, 
    "Solomon Islands-Jun" -> <|"National" -> 32.3, "Urban" -> 33.1, 
      "Rural" -> 32.2|>|>,
   ChartLabels -> {{"Cambodia\nDec", "Indonesia\nMay", "Lao PDR\nJul",
       "Mongolia\nDec", "Myanmar\nOct", "Philippines\nDec", 
      "Solomon Isl.\nJun"}, None},
   ChartLegends -> Placed[{"National", "Urban", "Rural"}, Below],
   PlotTheme -> "Business",
   PlotLabel -> "Factor 1",
   Frame -> {{True, True}, {True, True}},
   FrameLabel -> {None, "% of households"},
   BarSpacing -> None
   ];

bc2 = BarChart[
   <|"Cambodia-Dec" -> <|"National" -> 555.4, "Urban" -> 40, 
      "Rural" -> 80|>,
    "Indonesia-May" -> <|"National" -> 25.8, "Urban" -> 80, 
      "Rural" -> 81.8|>,
    "Lao PDR-Jul" -> <|"National" -> 27.0, "Urban" -> 45, 
      "Rural" -> 26.0|>,
    "Mongolia-Dec" -> <|"National" -> 17.5, "Urban" -> 30, 
      "Rural" -> 14.1|>,
    "Myanmar-Oct" -> <|"National" -> 52.8, "Urban" -> 54.9, 
      "Rural" -> 51.7|>,
    "Philippines-Dec" -> <|"National" -> 40, "Urban" -> 25, 
      "Rural" -> 72.9|>,
    "Solomon Islands-Jun" -> <|"National" -> 32.3, "Urban" -> 33.1, 
      "Rural" -> 32.2|>|>,
   ChartLabels -> {{"Cambodia\nDec", "Indonesia\nMay", "Lao PDR\nJul",
       "Mongolia\nDec", "Myanmar\nOct", "Philippines\nDec", 
      "Solomon Isl.\nJun"}, None},
   ChartLegends -> Placed[{"National", "Urban", "Rural"}, Below],
   PlotTheme -> "Business",
   PlotLabel -> "Factor 2",
   Frame -> {{True, True}, {True, True}},
   FrameLabel -> {None, "% of households"},
   BarSpacing -> None
   ];

If I want to create a new bar chart only for Cambodia, for example, I will have to extract the Cambodia data from each bar chart manually, which I do not want to do because I have many bar charts to do this operation. Rather I like to do the data extraction using Mathematica and create the new bar chart desired.

$\endgroup$
2
  • $\begingroup$ I've been digging to the FullForm of bc1 and it doesn't look as if the data can be easily extracted from the plot. $\endgroup$
    – mikado
    Commented Jun 21, 2021 at 20:46
  • $\begingroup$ @mikado: I did not mean extracting the data from the plot. Below, please see the right answer. Thanks. $\endgroup$ Commented Jun 21, 2021 at 20:55

1 Answer 1

3
$\begingroup$

Separate the data from the bar charts.

data1 = <|"Cambodia-Dec" -> <|"National" -> 75.4, "Urban" -> 61.9, 
     "Rural" -> 77.7|>, 
   "Indonesia-May" -> <|"National" -> 79.8, "Urban" -> 78.7, "Rural" -> 81.8|>,
    "Lao PDR-Jul" -> <|"National" -> 27.0, "Urban" -> 28.8, "Rural" -> 26.0|>,
    "Mongolia-Dec" -> <|"National" -> 17.5, "Urban" -> 19.3, "Rural" -> 14.1|>,
    "Myanmar-Oct" -> <|"National" -> 52.8, "Urban" -> 54.9, "Rural" -> 51.7|>,
    "Philippines-Dec" -> <|"National" -> 75.8, "Urban" -> 78.4, 
     "Rural" -> 72.9|>, 
   "Solomon Islands-Jun" -> <|"National" -> 32.3, "Urban" -> 33.1, 
     "Rural" -> 32.2|>|>;

data2 = <|"Cambodia-Dec" -> <|"National" -> 555.4, "Urban" -> 40, 
     "Rural" -> 80|>, 
   "Indonesia-May" -> <|"National" -> 25.8, "Urban" -> 80, "Rural" -> 81.8|>, 
   "Lao PDR-Jul" -> <|"National" -> 27.0, "Urban" -> 45, "Rural" -> 26.0|>, 
   "Mongolia-Dec" -> <|"National" -> 17.5, "Urban" -> 30, "Rural" -> 14.1|>, 
   "Myanmar-Oct" -> <|"National" -> 52.8, "Urban" -> 54.9, "Rural" -> 51.7|>, 
   "Philippines-Dec" -> <|"National" -> 40, "Urban" -> 25, "Rural" -> 72.9|>, 
   "Solomon Islands-Jun" -> <|"National" -> 32.3, "Urban" -> 33.1, 
     "Rural" -> 32.2|>|>;

BarChart[{data1[#], data2[#]},
   ChartLabels -> {{"Factor1", "Factor2"}, None},
   PlotLabel -> #,
   ChartLegends -> Placed[
     {"National", "Urban", "Rural"}, Below]] &@
 "Cambodia-Dec"

enter image description here

Or with "Log" scaling

BarChart[{data1[#], data2[#]},
   ChartLabels -> {{"Factor1", "Factor2"}, None},
   PlotLabel -> #,
   ChartLegends -> Placed[
     {"National", "Urban", "Rural"}, Below],
   ScalingFunctions -> "Log"] &@
 "Cambodia-Dec"

enter image description here

EDIT: Use Manipulate to selectively view data

keys = Keys[data1];

EDIT 2: Added export button (edit file path to your desired location)

keys = Keys[data1];

Manipulate[
 barchart = BarChart[{data1[#], data2[#]},
     ChartLabels -> {
       {Tooltip["Factor1", Dataset[data1]],
        Tooltip["Factor2", Dataset[data2]]}, None},
     PlotLabel -> Dataset[<|
        <|"Factor1" -> data1[#]|>,
        <|"Factor2" -> data2[#]|>|>],
     ChartLegends -> Placed[
       {"National", "Urban", "Rural"}, Below]] &[key],
 Row[{
   Control[{{key, keys[[1]]}, keys}],
   Spacer[150],
   Button["Export",
    Export[
     "/Users/roberthanlon/Downloads/BarCharts/" <>
      key <> 
      ".jpg", barchart]]}]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Manipulate is a very good tool. When I run Manipulate for Cambodia, for example, and if I want to save the bar char generated as png or jpeg into my hard disk, how do I do that? Thank you very much. $\endgroup$ Commented Jun 23, 2021 at 12:13

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.