Skip to main content

plotting - Building a nine - region cluster chart but, getting inaccurate plot-legend when empty subregions are present



This is a follow up question to the one submitted at the following: Stack exchange Link


I quickly get to the subject:


mockdataWithNAICS = {{"29-1141", 186, 112}, {"41-2031", 123, 92}, {"41-1011", 65, 404}, {"43-4051", 108, 646}, {"31-1014", 643, 246},
{"49-9071", 356, 363}, {"43-1011", 543, 381}, {"43-5081", 268, 674}, {"53-3032", 416, 653}, {"37-3011", 514, 428}, {"37-2012", 501, 58},
{"33-9032", 441, 598}, {"35-2014", 633, 138}, {"29-2061", 414, 590}, {"53-3033", 98, 155}, {"35-3031", 179, 431}, {"49-3023", 93, 623},
{"35-3021", 37, 578}, {"41-2011", 256, 237}, {"37-2011", 302, 50}, {"47-1011", 518, 2}, {"11-9111", 313, 294}, {"31-9092", 698, 136},
{"43-3031", 608, 610}, {"43-6013", 562, 515}, {"13-2011", 415, 327}, {"21-1093", 191, 72}, {"41-4012", 212, 92}, {"41-3031", 546, 418},
{"31-1011", 591, 49}, {"47-2031", 405, 526}, {"41-3021", 191, 297}, {"15-1151", 442, 445}, {"43-6011", 118, 185}, {"49-1011", 472, 402},
{"53-7062", 348, 552}, {"43-4171", 409, 396}, {"43-6014", 348, 247}, {"53-1031", 629, 59}, {"47-2061", 589, 534}, {"27-1026", 22, 377},
{"29-1069", 445, 74}, {"49-9021", 647, 539}, {"43-9061", 25, 543}, {"11-1021", 19, 165}, {"41-1012", 482, 199}, {"29-1062", 1, 68},

{"43-4081", 217, 112}, {"41-3099", 663, 66}, {"11-3031", 329, 392}, {"53-7061", 357, 515}, {"35-2021", 488, 245}, {"31-9091", 318, 679},
{"51-1011", 650, 349}, {"11-9051", 38, 145}, {"53-3031", 166, 691}, {"39-9021", 561, 127}, {"39-5012", 665, 362}, {"47-2111", 397, 532},
{"43-3071", 326, 271}, {"29-2034", 361, 450}, {"35-9011", 12, 366}, {"29-1123", 16, 211}, {"15-1142",162, 662}, {"11-2021", 520, 164},
{"29-2031", 339, 619}, {"25-2011", 263, 564}, {"41-4011", 551, 301}, {"29-2055", 76, 549}, {"29-2052", 285, 640}, {"13-2072", 370, 542},
{"35-2012", 512, 547}, {"11-2022", 130, 154}, {"15-1132", 188, 274}, {"13-2052", 338, 587}, {"15-1199", 455, 5}, {"35-9031", 595, 472},
{"17-2051", 648, 481}, {"11-9199", 430, 189}, {"39-3091", 29, 396}, {"47-2152", 412, 342}, {"29-1122", 582, 20}, {"11-9141", 276, 4},
{"25-2021", 666, 617}, {"15-1134", 236, 334}, {"37-1011", 407, 664}, {"29-1063", 260, 278}, {"49-3031", 161, 354}, {"41-9022", 185, 144},
{"41-9041", 551, 628}, {"25-2031", 529, 505}, {"29-2071", 548, 296}, {"29-1127", 373, 124}, {"21-1023", 473, 71}, {"29-1067", 489, 569},
{"29-1071", 539, 277}, {"11-3121", 390, 348}, {"11-9021", 634, 20}, {"53-3041", 589, 258}, {"49-3021", 47, 206}};


Following MelaGo at the above Stack Exchange link:


minx = Round[Min[mockdataWithNAICS[[All, 2]]]];
maxx = Round[Max[mockdataWithNAICS[[All, 2]]]];
dx = Round[Subdivide[minx, maxx, 3]];

miny = Round[Min[mockdataWithNAICS[[All, 3]]]];
maxy = Round[Max[mockdataWithNAICS[[All, 3]]]];
dy = Round[Subdivide[miny, maxy, 3]];

gridpts = Tuples[{dx, dy}];


regions = {Rectangle[gridpts[[1]], gridpts[[6]]],
Rectangle[gridpts[[5]], gridpts[[10]]],
Rectangle[gridpts[[9]], gridpts[[14]]],
Rectangle[gridpts[[2]], gridpts[[7]]],
Rectangle[gridpts[[6]], gridpts[[11]]],
Rectangle[gridpts[[10]], gridpts[[15]]],
Rectangle[gridpts[[3]], gridpts[[8]]],
Rectangle[gridpts[[7]], gridpts[[12]]],
Rectangle[gridpts[[11]], gridpts[[16]]]};


pts = Cases[
Table[Select[mockdataWithNAICS, #[[{2, 3}]] \[Element] r &], {r,
regions}], Except[{}]];

colors = {Magenta, Orange, Cyan, Red, Blue, Green, LightGray, Yellow,
Black};

plt1 = ListPlot[Table[Tooltip[#[[{2, 3}]], #[[1]]] & /@ p, {p, pts}],
Frame -> True, PlotStyle -> colors, GridLines -> {dx, dy},

PlotRange -> {{minx, maxx}, {miny, maxy}}, AspectRatio -> 1]

colornames = {"Magenta", "Orange", "Cyan", "Red", "Blue", "Green",
"Gray", "Yellow", "Black"};

regionlabels = {"Low Ads - Low Openings", "Medium Ads - Low Openings",
"High Ads - Low Openings", "Low Ads - Medium Openings",
"Medium Ads - Medium Openings", "High Ads - Medium Openings",
"Low Ads - High Openings", "Medium Ads - High Openings",
"High Ads - High Openings"};


table[pairs_] :=
TableForm[pairs,
TableHeadings -> {colornames, {"Colors:", "Cluster Region:"}},
TableAlignments -> Center]

SwatchLegend[colors, regionlabels, LegendLayout -> table]

plt2 = ListPlot[Table[Tooltip[#[[{2, 3}]], #[[1]]] & /@ p, {p, pts}],
Frame -> True, PlotStyle -> colors, GridLines -> {dx, dy},

PlotRange -> {{minx, maxx}, {miny, maxy}}, AspectRatio -> 1,
FrameLabel -> {"Job Ads", "Job Openings"},
LabelStyle -> Directive[Black, 16, Bold], ImageSize -> 600,
PlotLabel ->
Style[Framed["Macondo Region"], 16, Black, Bold,
Background -> Lighter[Yellow]],
PlotLegends ->
SwatchLegend[colors, regionlabels, LegendLayout -> table,
LegendMarkers -> {"Bubble", 10}]]


The above plt2 works fine because all the cluster regions involved contain at least one point by design.


Next, I delete:


(1.) the Orange points in the [Medium Ads - Low Openings] region;


(2.) the Red points in the [Low Ads - Medium Openings] region; and


(3.) the Gray points in the [Low Ads - High Openings] region


to show you the problem that I' m experiencing.


MockdataWithEmptyRegions = {{"29-1141", 186, 112}, {"41-2031", 123, 92},
{"53-3033", 98, 155}, {"21-1093", 191, 72}, {"41-4012", 212, 92},
{"43-6011", 118, 185}, {"11-1021", 19, 165}, {"29-1062", 1, 68},
{"43-4081", 217, 112}, {"11-9051", 38, 145}, {"29-1123", 16, 211},

{"11-2022", 130, 154}, {"41-9022", 185, 144}, {"49-3021", 47, 206},
{"37-2012", 501, 58}, {"35-2014", 633, 138}, {"47-1011", 518, 2},
{"31-9092", 698, 136}, {"31-1011", 591, 49}, {"53-1031", 629, 59},
{"41-1012", 482, 199}, {"41-3099", 663, 66}, {"39-9021", 561, 127},
{"11-2021", 520, 164}, {"29-1122", 582, 20}, {"21-1023", 473, 71},
{"11-9021", 634, 20}, {"49-9071", 356, 363}, {"41-2011", 256, 237},
{"11-9111", 313, 294}, {"13-2011", 415, 327}, {"15-1151", 442, 445},
{"43-4171", 409, 396}, {"53-7062", 348, 552}, {"11-3031", 329, 392},
{"43-3071", 326, 271}, {"29-2034", 361, 450}, {"47-2152", 412, 342},
{"15-1134", 236, 334}, {"29-1063", 260, 278}, {"11-3121", 390, 348},

{"31-1014", 643, 246}, {"43-1011", 543, 381}, {"37-3011", 514, 428},
{"41-3031", 546, 418}, {"49-1011", 472, 402}, {"35-2021", 488, 245},
{"51-1011", 650, 349}, {"39-5012", 665, 362}, {"41-4011", 551, 301},
{"29-2071", 548, 296}, {"29-1071", 539, 277}, {"53-3041", 589, 258},
{"43-5081", 268, 674}, {"53-3032", 416, 653}, {"33-9032", 441, 598},
{"29-2061", 414, 590}, {"47-2031", 405, 526}, {"53-7062", 348, 552},
{"53-7061", 357, 515}, {"31-9091", 318, 679}, {"47-2111", 397, 532},
{"29-2031", 339, 619}, {"25-2011", 263, 564}, {"29-2052", 285, 640},
{"13-2072", 370, 542}, {"13-2052", 338, 587}, {"37-1011", 407, 664},
{"43-3031", 608, 610}, {"43-6013", 562, 515}, {"47-2061", 589, 534},

{"49-9021", 647, 539}, {"35-2012", 512, 547}, {"35-9031", 595, 472},
{"17-2051", 648, 481}, {"25-2021", 666, 617}, {"41-9041", 551, 628},
{"25-2031", 529, 505}, {"29-1067", 489, 569}};


pts2 = Cases[
Table[Select[
MockdataWithEmptyRegions, #[[{2, 3}]] \[Element] r &], {r,
regions}], Except[{}]];


plt3 = ListPlot[Table[Tooltip[#[[{2, 3}]], #[[1]]] & /@ p, {p, pts2}],
Frame -> True, PlotStyle -> colors, GridLines -> {dx, dy},
PlotRange -> {{minx, maxx}, {miny, maxy}}, AspectRatio -> 1,
FrameLabel -> {"Job Ads", "Job Openings"},
LabelStyle -> Directive[Black, 16, Bold], ImageSize -> 600,
PlotLabel ->
Style[Framed["Macondo Region"], 16, Black, Bold,
Background -> Lighter[Yellow]],
PlotLegends ->
SwatchLegend[colors, regionlabels, LegendLayout -> table,

LegendMarkers -> {"Bubble", 10}]]

If you compare plt2 with plt3 you will see that the points in the [Medium Ads - Low Openings], [Low Ads - Medium Openings] and [Low Ads - High Openings] regions are gone in plt3 but, the color scheme has changed!


For instance, what used to be yellow points in plt2 are now blue points in plt3. This negates the legend in plt2 which says that the blue points should be in the [Medium Ads - Medium Opening] region.


I have two questions:


(1.) How can I fix the color scheme so that it will not change for data sets that might contain empty regions?


(2.) How can I give the same point size to all the points, in all the regions?


Thank you!



Answer



For (1) Instead of removing empty sub-lists, change your function for adding tooltips so that empty sub-lists replaced with an invisible point:



pts = Table[Select[mockdataWithNAICS, #[[{2,3}]] ∈ r &], {r, regions}] /. {}->{{}};

pts2 = Table[Select[MockdataWithEmptyRegions, #[[{2,3}]] ∈ r &], {r, regions}] /. {}->{{}};

addTooltips = Table[If[# === {}, Style[{0, 0}, Opacity[0]],
Tooltip[#[[{2, 3}]], #[[1]]]] & /@ p, {p, #}] &;

and for (2) add the option BaseStyle -> PointSize[Large].


ListPlot[addTooltips @ pts, Frame -> True, PlotStyle -> colors, 
GridLines -> {dx, dy}, Axes -> False,

PlotRange -> {{minx, maxx}, {miny, maxy}}, AspectRatio -> 1,
FrameLabel -> {"Job Ads", "Job Openings"},
LabelStyle -> Directive[Black, 16, Bold], ImageSize -> 500,
BaseStyle -> PointSize[Large],
PlotLabel -> Style[Framed["Macondo Region"], 16, Black, Bold,
Background -> Lighter[Yellow]],
PlotLegends -> SwatchLegend[colors, regionlabels, LegendLayout -> table,
LegendMarkers -> "Bubble"]]

enter image description here



Use addTooltips @ pts2 in the first argument to get


enter image description here


Comments

Popular posts from this blog

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

What is and isn't a valid variable specification for Manipulate?

I have an expression whose terms have arguments (representing subscripts), like this: myExpr = A[0] + V[1,T] I would like to put it inside a Manipulate to see its value as I move around the parameters. (The goal is eventually to plot it wrt one of the variables inside.) However, Mathematica complains when I set V[1,T] as a manipulated variable: Manipulate[Evaluate[myExpr], {A[0], 0, 1}, {V[1, T], 0, 1}] (*Manipulate::vsform: Manipulate argument {V[1,T],0,1} does not have the correct form for a variable specification. >> *) As a workaround, if I get rid of the symbol T inside the argument, it works fine: Manipulate[ Evaluate[myExpr /. T -> 15], {A[0], 0, 1}, {V[1, 15], 0, 1}] Why this behavior? Can anyone point me to the documentation that says what counts as a valid variable? And is there a way to get Manpiulate to accept an expression with a symbolic argument as a variable? Investigations I've done so far: I tried using variableQ from this answer , but it says V[1...