3
$\begingroup$

I have trouble adding a second legend to this map (see picture underneath). I need the legend to that show each existing bubble size, with the respective rainbow color (from the map) and with the respective number (from 3 (purple) to 7 (red)) and the legend title "Happiness scores"?

As it is a combination of two maps, it appears to be rather complicated. I would be happy for any help!

Here are the codes, "DataACT1" being the country names and "DataACT2" the respective happiness scores (both provided in the dataset):

DataACT1Combination = Transpose[{DataACT1, DataACT2}];
DataACT1Combination$TMP = DeleteCases[DeleteCases[Map[{#[[1]], #[[2]]} &, DataACT1Combination], {"", ""}], {_, "-"}];

data = Map[Rule[Entity["Country", #[[1]]], #[[2]]] &, DataACT1Combination$TMP];

  H = GeoBubbleChart[data, BubbleSizes -> {0.02, 0.04}, 
  BubbleScale -> "Diameter", ImageSize -> 1000, 
  ColorFunction -> "Rainbow", GeoRange -> "World", 
  GeoBackground -> None]
DevelopedCountries = {Entity\["Country", "Albania"\], Entity\["Country", "Algeria"\], Entity\["Country", "Argentina"\], Entity\["Country", "Australia"\], Entity\["Country", "Austria"\], Entity\["Country", "Azerbaijan"\], Entity\["Country", "Bahamas"\], Entity\["Country", "Bahrain"\], Entity\["Country", "Barbados"\], Entity\["Country", "Belarus"\], Entity\["Country", "Belgium"\], Entity\["Country", "BosniaHerzegovina"\],Entity\["Country", "Botswana"\], Entity\["Country", "Brazil"\], Entity\["Country", "Brunei"\], Entity\["Country", "Bulgaria"\], Entity\["Country", "Canada"\], Entity\["Country", "Chile"\], Entity\["Country", "China"\], Entity\["Country", "Colombia"\], Entity\["Country", "CostaRica"\], Entity\["Country", "Croatia"\], Entity\["Country", "Cuba"\], Entity\["Country", "Cyprus"\], Entity\["Country", "CzechRepublic"\], Entity\["Country", "Denmark"\], Entity\["Country", "Dominica"\], Entity\["Country", "DominicanRepublic"\],Entity\["Country", "Ecuador"\], Entity\["Country", "Egypt"\],Entity\["Country", "EquatorialGuinea"\], Entity\["Country", "Estonia"\],Entity\["Country", "Finland"\], Entity\["Country", "France"\], Entity\["Country", "Gabon"\], Entity\["Country", "Georgia"\], Entity\["Country", "Germany"\], Entity\["Country", "Greece"\], Entity\["Country", "Hungary"\], Entity\["Country", "Iceland"\], Entity\["Country", "Indonesia"\], Entity\["Country", "Iran"\], Entity\["Country", "Iraq"\], Entity\["Country", "Ireland"\], Entity\["Country", "Israel"\], Entity\["Country", "Italy"\], Entity\["Country", "Japan"\], Entity\["Country", "Jordan"\], Entity\["Country", "Kazakhstan"\], Entity\["Country", "SouthKorea"\], Entity\["Country", "Kuwait"\], Entity\["Country", "Latvia"\], Entity\["Country", "Lebanon"\], Entity\["Country", "Liechtenstein"\], Entity\["Country", "Lithuania"\], Entity\["Country", "Luxembourg"\], Entity\["Country", "Macedonia"\], Entity\["Country", "Malaysia"\], Entity\["Country", "Maldives"\], Entity\["Country", "Malta"\], Entity\["Country", "Mauritius"\], Entity\["Country", "Mexico"\], Entity\["Country", "Mongolia"\], Entity\["Country", "Montenegro"\], Entity\["Country", "Namibia"\], Entity\["Country", "Netherlands"\], Entity\["Country", "NewZealand"\], Entity\["Country", "Norway"\], Entity\["Country", "Oman"\], Entity\["Country", "Panama"\], Entity\["Country", "Peru"\], Entity\["Country", "Poland"\], Entity\["Country", "Portugal"\], Entity\["Country", "Qatar"\], Entity\["Country", "Romania"\], Entity\["Country", "Russia"\], Entity\["Country", "SaintLucia"\], Entity\["Country", "SaintVincentGrenadines"\], Entity\["Country", "SaudiArabia"\], Entity\["Country", "Serbia"\], Entity\["Country", "Seychelles"\], Entity\["Country", "Singapore"\], Entity\["Country", "Slovakia"\], Entity\["Country", "Slovenia"\], Entity\["Country", "SouthAfrica"\], Entity\["Country", "SouthSudan"\], Entity\["Country", "Spain"\], Entity\["Country", "Sudan"\], Entity\["Country", "Suriname"\], Entity\["Country", "Sweden"\], Entity\["Country", "Syria"\], Entity\["Country", "Tanzania"\], Entity\["Country", "TrinidadTobago"\], Entity\["Country", "Tunisia"\], Entity\["Country", "Turkey"\], Entity\["Country", "Turkmenistan"\], Entity\["Country", "UnitedArabEmirates"\], Entity\["Country", "UnitedKingdom"\], Entity\["Country", "UnitedStates"\], Entity\["Country", "Uruguay"\], Entity\["Country", "Venezuela"\]}
DevelopingCountries = {Entity\["Country", "Afghanistan"\], Entity\["Country", "Angola"\], Entity\["Country", "Armenia"\], Entity\["Country", "Benin"\], Entity\["Country", "Bhutan"\], Entity\["Country", "Bolivia"\], Entity\["Country", "BurkinaFaso"\], Entity\["Country", "Burundi"\], Entity\["Country", "CapeVerde"\], Entity\["Country", "Cambodia"\], Entity\["Country", "Cameroon"\], Entity\["Country", "CentralAfricanRepublic"\], Entity\["Country", "Chad"\], Entity\["Country", "Comoros"\], Entity\["Country", "DemocraticRepublicCongo"\], Entity\["Country", "RepublicCongo"\], Entity\["Country", "IvoryCoast"\],Entity\["Country", "Djibouti"\], Entity\["Country", "ElSalvador"\], Entity\["Country", "Eritrea"\], Entity\["Country", "Swaziland"\], Entity\["Country", "Ethiopia"\], Entity\["Country", "Fiji"\], Entity\["Country", "Gambia"\], Entity\["Country", "Ghana"\], Entity\["Country", "Guatemala"\], Entity\["Country", "Guinea"\], Entity\["Country", "GuineaBissau"\], Entity\["Country", "Guyana"\], Entity\["Country", "Haiti"\], Entity\["Country", "Honduras"\], Entity\["Country", "India"\], Entity\["Country", "Jamaica"\], Entity\["Country", "Kenya"\], Entity\["Country", "Kiribati"\], Entity\["Country", "Kosovo"\], Entity\["Country", "Kyrgyzstan"\], Entity\["Country", "Laos"\], Entity\["Country", "Lesotho"\], Entity\["Country", "Liberia"\], Entity\["Country", "Libya"\], Entity\["Country", "Madagascar"\], Entity\["Country", "Malawi"\], Entity\["Country", "Mali"\], Entity\["Country", "Mauritania"\], Entity\["Country", "Micronesia"\], Entity\["Country", "Moldova"\], Entity\["Country", "Morocco"\], Entity\["Country", "Mozambique"\], Entity\["Country", "Myanmar"\], Entity\["Country", "Nepal"\], Entity\["Country", "Nicaragua"\], Entity\["Country", "Niger"\], Entity\["Country", "Nigeria"\], Entity\["Country", "Pakistan"\], Entity\["Country", "PapuaNewGuinea"\], Entity\["Country", "Paraguay"\], Entity\["Country", "Philippines"\], Entity\["Country", "Rwanda"\], Entity\["Country", "Samoa"\], Entity\["Country", "SaoTomePrincipe"\], Entity\["Country", "Senegal"\], Entity\["Country", "SierraLeone"\], Entity\["Country", "SolomonIslands"\], Entity\["Country", "SriLanka"\], Entity\["Country", "Taiwan"\], Entity\["Country", "Tajikistan"\], Entity\["Country", "EastTimor"\], Entity\["Country", "Togo"\], Entity\["Country", "Tonga"\], Entity\["Country", "Uganda"\], Entity\["Country", "Ukraine"\], Entity\["Country", "Uzbekistan"\], Entity\["Country", "Vanuatu"\], Entity\["Country", "Vietnam"\], Entity\["Country", "Yemen"\], Entity\["Country", "Zambia"\], Entity\["Country", "Zimbabwe"\]}]

FinalPlotCountries =
 Labeled[
  GeoRegionValuePlot[Join[
     Map[Rule[#, Opacity[.2, Red]] &, DevelopedCountries],
     Map[Rule[#, Opacity[.2, Blue]] &, DevelopingCountries]
     ], GeoLabels -> False, LabelStyle -> {8, Bold, Italic}, 
    ImageSize -> 1000, GeoRange -> "World", 
    Epilog -> {H[[1, 1]], 
      Inset[SwatchLegend[{Opacity[.2, Red], 
         Opacity[.2, Blue]}, {"developed countries    ", 
         "developing countries    "}, LegendLayout -> "Row"], 
       Scaled[{0.5, 0.03}]]}] // Framed, 
  Style["Development status of countries and their happiness score", 
   25, FontFamily -> "Times"], Top]

Map in which I need to add another legend

$\endgroup$

1 Answer 1

4
$\begingroup$

If you use the option ChartLegends in GeoBubbleChart and PlotLegends in GeoRegionValuePlot and combine the two plots with Show the legends are automatically combined:

SeedRandom[7777]
thirtycountries = RandomEntity["Country", 30];

population = DeleteMissing[# -> #[ "Population"] & /@ thirtycountries];
minmax1 = QuantityMagnitude@MinMax[population[[All, 2]]];

hscore = # -> RandomReal[100] & /@ thirtycountries;
minmax2 = QuantityMagnitude@MinMax[hscore[[All, 2]]];

gbc = GeoBubbleChart[population, ColorFunction -> "Rainbow", 
   BubbleSizes -> {.02, .05}, GeoRange -> "World", 
   GeoBackground -> None, 
   ChartLegends -> Placed[SwatchLegend[ColorData[{"Rainbow", minmax1}] /@ 
       QuantityMagnitude[Values[population]], 
      CommonName@Keys[population], LegendLayout -> {"Column", 2}, 
      LegendLabel -> Style["population", 16], 
      LegendMarkers -> "Bubble"], Right]];

grvp = GeoRegionValuePlot[hscore, GeoLabels -> False, 
   PlotStyle -> Opacity[.5], ImageSize -> 800, GeoRange -> "World", 
   Frame -> True, ColorFunction -> "TemperatureMap", 
   FrameTicks -> None, 
   PlotLegends -> Placed[BarLegend[Automatic, LegendLabel -> Style["hscore", 16], 
      LegendLayout -> "Row"], Above]];

Show[grvp, gbc]

enter image description here

Alternatively, create legends outside GeoBubbleChart and GeoRegionValuePlot and add them to Show[grvp, gbc] using Legended:

legend1 = SwatchLegend[ColorData[{"Rainbow", minmax1}] /@ 
    QuantityMagnitude[Values[population]], CommonName@Keys[population], 
   LegendLayout -> {"Column", 2}, 
   LegendLabel -> Style["population", 16], LegendMarkers -> "Bubble"];

legend2 = BarLegend[{"TemperatureMap", minmax2}, 
   LegendLabel -> Style["hscore", 16], LegendLayout -> "Row"];

gbc = GeoBubbleChart[population, ColorFunction -> "Rainbow", 
   BubbleSizes -> {.02, .05}, GeoRange -> "World", GeoBackground -> None];

grvp = GeoRegionValuePlot[hscore, GeoLabels -> False, 
   PlotStyle -> Opacity[.5], ImageSize -> 800, GeoRange -> "World", 
   Frame -> True, ColorFunction -> "TemperatureMap", 
   PlotLegends -> None, FrameTicks -> None];

Legended[Show[grvp, gbc], Thread[Placed[{legend1, legend2}, {After, Above}]]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you!!! Exactly what I needed, the second option worked great. $\endgroup$
    – Faye Drew
    Commented Jan 4, 2020 at 10:56

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.