Function Repository Resource:

NutrientComparisonBarChart

Source Notebook

Create a dual bar chart comparing the calories and macronutrients in a list of foods

Contributed by: Gay Wilson

ResourceFunction["NutrientComparisonBarChart"][foods]

returns calories and macronutrient information in BarChart form for a list of foods.

ResourceFunction["NutrientComparisonBarChart"][foods,format]

returns calories and macronutrient information in the specified format.

Details and Options

NutrientComparisonBarChart returns a pair of bar charts. The first shows calories, and the second shows macronutrient content (protein, carbohydrates, fat, fiber).
Each bar represents the amount of the corresponding nutrient relative to 1 gram of that food (Cal/g and g/g). Hover over a bar to view the numeric value.
The following options are supported:
ChartLabelsBeforeplacement of food names
ChartStyleAutomaticgraphics style for charts
ChartLegendsBelowplacement of nutrient properties
BarOriginLeftorigin placement for bars
BarSpacingAutomaticspacing between bars and bar groups
FontSize10font size of chart labels and legends
PlotLabel""optional overall label for both charts
ImageSize600overall image size expressed in points
ImageMarginsAutomaticabsolute margins around image
BackgroundAutomaticbackground color for the charts
NutrientComparisonBarChart supports the following format values:
"Dataset"returns a Dataset of the calories and macronutrient content in the foods
"Data"returns associations of the foods and their calories and macronutrient content

Examples

Basic Examples (2) 

Compare the relative calories and macronutrients of raw fruits:

In[1]:=
ResourceFunction[
 "NutrientComparisonBarChart"][{"raw apple", "raw banana", "raw pear",
   "raw papaya", "raw raspberry", "raw strawberry"}]
Out[1]=

Visualize the nutrients in high-protein foods with a customized chart design:

In[2]:=
ResourceFunction[
 "NutrientComparisonBarChart"][{"braised beef", "cooked lentils", "boiled eggs", "peanut butter", "fried tofu", "grilled chicken"}, ChartStyle -> "Pastel", BarSpacing -> "None", PlotLabel -> "High-Protein Foods", FontSize -> 12]
Out[2]=

Scope (2) 

Display the nutrients data as a Dataset:

In[3]:=
ResourceFunction[
 "NutrientComparisonBarChart"][{"braised beef", "cooked lentils", "boiled eggs", "peanut butter", "fried tofu", "grilled chicken"}, "Dataset"]
Out[3]=

Display the data as associations:

In[4]:=
ResourceFunction[
 "NutrientComparisonBarChart"][{"braised beef", "cooked lentils", "boiled eggs", "peanut butter", "fried tofu", "grilled chicken"}, "Data"]
Out[4]=

Use Wolfram Language food entities instead of text strings:

In[5]:=
foodEntities = {Entity[
   "Food", {EntityProperty["Food", "FoodType"] -> ContainsExactly[{
Entity["FoodType", "Spinach"]}], EntityProperty["Food", "AddedFoodTypes"] -> ContainsExactly[{}], EntityProperty["Food", "Preparation"] -> Entity[
      "FoodPreparation", "Raw"]}], Entity[
   "Food", {EntityProperty["Food", "FoodType"] -> ContainsExactly[{
Entity["FoodType", "Quinoa"]}], EntityProperty["Food", "AddedFoodTypes"] -> ContainsExactly[{}], EntityProperty["Food", "Preparation"] -> Entity[
      "FoodPreparation", "Cooked"]}], Entity[
   "Food", {EntityProperty["Food", "FoodType"] -> ContainsExactly[{
Entity["FoodType", "Salmon"]}], EntityProperty["Food", "AddedFoodTypes"] -> ContainsExactly[{}], EntityProperty["Food", "Preparation"] -> Entity[
      "FoodPreparation", "Grilled"]}], Entity[
   "Food", {EntityProperty["Food", "FoodType"] -> ContainsExactly[{
Entity["FoodType", "Carrot"]}], EntityProperty["Food", "AddedFoodTypes"] -> ContainsExactly[{}], EntityProperty["Food", "Preparation"] -> Entity[
      "FoodPreparation", "Boiled"]}]};
In[6]:=
ResourceFunction["NutrientComparisonBarChart"][foodEntities]
Out[6]=
In[7]:=
ResourceFunction["NutrientComparisonBarChart"][foodEntities, "Dataset"]
Out[7]=
In[8]:=
ResourceFunction["NutrientComparisonBarChart"][foodEntities, "Data"]
Out[8]=

Options (1) 

Create a vertical bar chart of grains with specified image size and labels:

In[9]:=
ResourceFunction[
 "NutrientComparisonBarChart"][{"wheat", "rolled oats", "rye grain", "raw barley", "dry bulgur", "raw millet"}, BarOrigin -> Bottom, ChartLabels -> Below, ChartLegends -> Above, ImageSize -> Medium]
Out[9]=

Neat Examples (1) 

Have fun with the ChartStyle options named for foods, such as AvocadoColors, CandyColors, CherryTones, CoffeeTones, FruitPunchColors, MintColors, PlumColors and WatermelonColors:

In[10]:=
ResourceFunction[
 "NutrientComparisonBarChart"][{"cherry", "orange", "pineapple", "plum"}, ImageSize -> Medium, ChartStyle -> "FruitPunchColors"]
Out[10]=
In[11]:=
ResourceFunction[
 "NutrientComparisonBarChart"][{"brussel sprouts", "broccoli", "kale",
   "avocado"}, ImageSize -> Medium, ChartStyle -> "AvocadoColors"]
Out[11]=

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.2.0 – 01 May 2026
  • 1.1.0 – 10 May 2024
  • 1.0.0 – 04 March 2024

Related Resources

License Information