0

Any help would be greatly appreciated!!

I'm trying to create a choropleth map in R that shows the counties of texas, color-coded by their population ranges.

My problem is that the range of populations is too large. The highest population is over 4 million, but most of the counties have a population under 50,000. The criteria for the fill is: (0-1mil), (1-2mil), (2-3mil), (3-4mil), (4-5mil) but almost all fall under 0-1mil.

How can I change the legend to account for different ranges of numbers? For example, maybe: (0-1,000), (1,000-10,000), (10,000-100,000), (100,000-1mil), (1mil-5mil)

Here's the code I wrote to plot the data:

txplot <- ggplot(txczpop, aes(fill=pop2014)) + geom_map(txmap)
tm_shape(txmap) +
tm_fill("pop2014", title="TX County Population", palette = "PRGn") +
tm_borders(alpha=.5) +
tm_style_beaver()

Here's the result: [![enter image description here][1]][1]

I'm using a census county shapefile and population also retrieved from a census file.

Here's the output of my population data:

txczpop <-     structure(list(county_fips = c(48001L, 48003L, 48005L, 48007L, 
48009L, 48011L, 48013L, 48015L, 48017L, 48019L, 48021L, 48023L, 
48025L, 48027L, 48029L, 48031L, 48033L, 48035L, 48037L, 48039L, 
48041L, 48043L, 48045L, 48047L, 48049L, 48051L, 48053L, 48055L, 
48057L, 48059L, 48061L, 48063L, 48065L, 48067L, 48069L, 48071L, 
48073L, 48075L, 48077L, 48079L, 48081L, 48083L, 48085L, 48087L, 
48089L, 48091L, 48093L, 48095L, 48097L, 48099L, 48101L, 48103L, 
48105L, 48107L, 48109L, 48111L, 48113L, 48115L, 48117L, 48119L, 
48121L, 48123L, 48125L, 48127L, 48129L, 48131L, 48133L, 48135L, 
48137L, 48141L, 48139L, 48143L, 48145L, 48147L, 48149L, 48151L, 
48153L, 48155L, 48157L, 48159L, 48161L, 48163L, 48165L, 48167L, 
48169L, 48171L, 48173L, 48175L, 48177L, 48179L, 48181L, 48183L, 
48185L, 48187L, 48189L, 48191L, 48193L, 48195L, 48197L, 48199L, 
48201L, 48203L, 48205L, 48207L, 48209L, 48211L, 48213L, 48215L, 
48217L, 48219L, 48221L, 48223L, 48225L, 48227L, 48229L, 48231L, 
48233L, 48235L, 48237L, 48239L, 48241L, 48243L, 48245L, 48247L, 
48249L, 48251L, 48253L, 48255L, 48257L, 48259L, 48261L, 48263L, 
48265L, 48267L, 48269L, 48271L, 48273L, 48275L, 48283L, 48277L, 
48279L, 48281L, 48285L, 48287L, 48289L, 48291L, 48293L, 48295L, 
48297L, 48299L, 48301L, 48303L, 48305L, 48313L, 48315L, 48317L, 
48319L, 48321L, 48323L, 48307L, 48309L, 48311L, 48325L, 48327L, 
48329L, 48331L, 48333L, 48335L, 48337L, 48339L, 48341L, 48343L, 
48345L, 48347L, 48349L, 48351L, 48353L, 48355L, 48357L, 48359L, 
48361L, 48363L, 48365L, 48367L, 48369L, 48371L, 48373L, 48375L, 
48377L, 48379L, 48381L, 48383L, 48385L, 48387L, 48389L, 48391L, 
48393L, 48395L, 48397L, 48399L, 48401L, 48403L, 48405L, 48407L, 
48409L, 48411L, 48413L, 48415L, 48417L, 48419L, 48421L, 48423L, 
48425L, 48427L, 48429L, 48431L, 48433L, 48435L, 48437L, 48439L, 
48441L, 48443L, 48445L, 48447L, 48449L, 48451L, 48453L, 48455L, 
48457L, 48459L, 48461L, 48463L, 48465L, 48467L, 48469L, 48471L, 
48473L, 48475L, 48477L, 48479L, 48481L, 48483L, 48485L, 48487L, 
48489L, 48491L, 48493L, 48495L, 48497L, 48499L, 48501L, 48503L, 
48505L, 48507L), county_name = c("Anderson", "Andrews", "Angelina", 
"Aransas", "Archer", "Armstrong", "Atascosa", "Austin", "Bailey", 
"Bandera", "Bastrop", "Baylor", "Bee", "Bell", "Bexar", "Blanco", 
"Borden", "Bosque", "Bowie", "Brazoria", "Brazos", "Brewster", 
"Briscoe", "Brooks", "Brown", "Burleson", "Burnet", "Caldwell", 
"Calhoun", "Callahan", "Cameron", "Camp", "Carson", "Cass", "Castro", 
"Chambers", "Cherokee", "Childress", "Clay", "Cochran", "Coke", 
"Coleman", "Collin", "Collingsworth", "Colorado", "Comal", "Comanche", 
"Concho", "Cooke", "Coryell", "Cottle", "Crane", "Crockett", 
"Crosby", "Culberson", "Dallam", "Dallas", "Dawson", "Deaf Smith", 
"Delta", "Denton", "DeWitt", "Dickens", "Dimmit", "Donley", "Duval", 
"Eastland", "Ector", "Edwards", "El Paso", "Ellis", "Erath", 
"Falls", "Fannin", "Fayette", "Fisher", "Floyd", "Foard", "Fort Bend", 
"Franklin", "Freestone", "Frio", "Gaines", "Galveston", "Garza", 
"Gillespie", "Glasscock", "Goliad", "Gonzales", "Gray", "Grayson", 
"Gregg", "Grimes", "Guadalupe", "Hale", "Hall", "Hamilton", "Hansford", 
"Hardeman", "Hardin", "Harris", "Harrison", "Hartley", "Haskell", 
"Hays", "Hemphill", "Henderson", "Hidalgo", "Hill", "Hockley", 
"Hood", "Hopkins", "Houston", "Howard", "Hudspeth", "Hunt", "Hutchinson", 
"Irion", "Jack", "Jackson", "Jasper", "Jeff Davis", "Jefferson", 
"Jim Hogg", "Jim Wells", "Johnson", "Jones", "Karnes", "Kaufman", 
"Kendall", "Kenedy", "Kent", "Kerr", "Kimble", "King", "Kinney", 
"Kleberg", "Knox", "La Salle", "Lamar", "Lamb", "Lampasas", "Lavaca", 
"Lee", "Leon", "Liberty", "Limestone", "Lipscomb", "Live Oak", 
"Llano", "Loving", "Lubbock", "Lynn", "Madison", "Marion", "Martin", 
"Mason", "Matagorda", "Maverick", "McCulloch", "McLennan", "McMullen", 
"Medina", "Menard", "Midland", "Milam", "Mills", "Mitchell", 
"Montague", "Montgomery", "Moore", "Morris", "Motley", "Nacogdoches", 
"Navarro", "Newton", "Nolan", "Nueces", "Ochiltree", "Oldham", 
"Orange", "Palo Pinto", "Panola", "Parker", "Parmer", "Pecos", 
"Polk", "Potter", "Presidio", "Rains", "Randall", "Reagan", "Real", 
"Red River", "Reeves", "Refugio", "Roberts", "Robertson", "Rockwall", 
"Runnels", "Rusk", "Sabine", "San Augustine", "San Jacinto", 
"San Patricio", "San Saba", "Schleicher", "Scurry", "Shackelford", 
"Shelby", "Sherman", "Smith", "Somervell", "Starr", "Stephens", 
"Sterling", "Stonewall", "Sutton", "Swisher", "Tarrant", "Taylor", 
"Terrell", "Terry", "Throckmorton", "Titus", "Tom Green", "Travis", 
"Trinity", "Tyler", "Upshur", "Upton", "Uvalde", "Val Verde", 
"Van Zandt", "Victoria", "Walker", "Waller", "Ward", "Washington", 
"Webb", "Wharton", "Wheeler", "Wichita", "Wilbarger", "Willacy", 
"Williamson", "Wilson", "Winkler", "Wise", "Wood", "Yoakum", 
"Young", "Zapata", "Zavala"), pop2014 = c(57627L, 17477L, 87750L, 
24972L, 8811L, 1955L, 47774L, 29114L, 6910L, 20892L, 78069L, 
3592L, 32863L, 329140L, 1855866L, 10812L, 652L, 17780L, 93275L, 
338124L, 209152L, 9173L, 1536L, 7194L, 37653L, 17253L, 44943L, 
39810L, 21797L, 13513L, 420392L, 12621L, 6013L, 30261L, 7781L, 
38145L, 50902L, 7089L, 10370L, 2935L, 3254L, 8430L, 885241L, 
3017L, 20719L, 123694L, 13550L, 4050L, 38761L, 75562L, 1415L, 
4950L, 3812L, 5899L, 2266L, 7135L, 2518638L, 13372L, 19195L, 
5238L, 753363L, 20684L, 2218L, 11089L, 3543L, 11533L, 18176L, 
153904L, 1879L, 833487L, 159317L, 40147L, 16989L, 33752L, 24833L, 
3831L, 5949L, 1275L, 685345L, 10600L, 19762L, 18531L, 19425L, 
314198L, 6435L, 25520L, 1291L, 7549L, 20462L, 23044L, 123534L, 
123204L, 27172L, 147250L, 34720L, 3147L, 8199L, 5509L, 3928L, 
55621L, 4441370L, 67336L, 6089L, 5769L, 185025L, 4180L, 79290L, 
831073L, 34848L, 23577L, 53921L, 35921L, 22741L, 36651L, 3211L, 
88493L, 21773L, 1574L, 8855L, 14739L, 35552L, 2204L, 252235L, 
5255L, 41353L, 157456L, 19936L, 14906L, 111236L, 38880L, 400L, 
785L, 50562L, 4438L, 262L, 3526L, 32190L, 3858L, 7474L, 49523L, 
13574L, 20156L, 19721L, 16742L, 16861L, 78117L, 23524L, 3553L, 
12091L, 19510L, 86L, 293974L, 5771L, 13861L, 10149L, 5460L, 4071L, 
36519L, 57023L, 8199L, 243441L, 805L, 47894L, 2147L, 155830L, 
24256L, 4870L, 9076L, 19416L, 518947L, 22148L, 12743L, 1153L, 
65301L, 48195L, 14138L, 15093L, 356221L, 10758L, 2070L, 83433L, 
28096L, 23769L, 123164L, 9908L, 15893L, 46079L, 121627L, 6976L, 
11032L, 128220L, 3755L, 3371L, 12446L, 14349L, 7302L, 928L, 16500L, 
87809L, 10416L, 53923L, 10350L, 8610L, 27099L, 66915L, 5622L, 
3162L, 17328L, 3343L, 25515L, 3084L, 218842L, 8694L, 62955L, 
9405L, 1339L, 1403L, 3972L, 7581L, 1945360L, 135143L, 927L, 12739L, 
1608L, 32506L, 116608L, 1151145L, 14224L, 21418L, 40354L, 3454L, 
27117L, 48974L, 52910L, 91081L, 69789L, 46820L, 11625L, 34438L, 
266673L, 41168L, 5714L, 132355L, 12973L, 21903L, 489250L, 46402L, 
7821L, 61638L, 42852L, 8286L, 18350L, 14319L, 12267L)), .Names = c("county_fips", 
"county_name", "pop2014"), row.names = c(5100L, 5101L, 5103L, 
5106L, 5107L, 5109L, 5112L, 5114L, 5116L, 5118L, 5120L, 5121L, 
5124L, 5126L, 5128L, 5129L, 5131L, 5133L, 5136L, 5137L, 5140L, 
5141L, 5143L, 5146L, 5147L, 5150L, 5152L, 5153L, 5156L, 5158L, 
5159L, 5161L, 5163L, 5166L, 5168L, 5170L, 5171L, 5174L, 5176L, 
5178L, 5179L, 5182L, 5183L, 5185L, 5188L, 5190L, 5192L, 5194L, 
5195L, 5198L, 5200L, 5201L, 5203L, 5205L, 5208L, 5209L, 5212L, 
5214L, 5215L, 5218L, 5219L, 5221L, 5224L, 5226L, 5228L, 5230L, 
5232L, 5233L, 5235L, 5239L, 5237L, 5242L, 5244L, 5245L, 5248L, 
5249L, 5251L, 5254L, 5256L, 5257L, 5260L, 5261L, 5264L, 5265L, 
5268L, 5270L, 5272L, 5274L, 5276L, 5278L, 5280L, 5281L, 5284L, 
5286L, 5288L, 5290L, 5292L, 5293L, 5296L, 5298L, 5300L, 5301L, 
5303L, 5306L, 5308L, 5309L, 5312L, 5314L, 5316L, 5317L, 5319L, 
5321L, 5323L, 5326L, 5327L, 5330L, 5332L, 5334L, 5335L, 5337L, 
5339L, 5341L, 5343L, 5346L, 5348L, 5349L, 5352L, 5354L, 5356L, 
5357L, 5360L, 5362L, 5364L, 5365L, 5368L, 5369L, 5372L, 5374L, 
5382L, 5376L, 5378L, 5379L, 5383L, 5385L, 5388L, 5390L, 5392L, 
5394L, 5396L, 5398L, 5400L, 5401L, 5404L, 5412L, 5413L, 5416L, 
5418L, 5419L, 5421L, 5406L, 5407L, 5409L, 5423L, 5425L, 5427L, 
5429L, 5432L, 5434L, 5435L, 5438L, 5440L, 5442L, 5443L, 5446L, 
5448L, 5449L, 5451L, 5453L, 5456L, 5457L, 5460L, 5461L, 5464L, 
5465L, 5468L, 5470L, 5472L, 5474L, 5476L, 5477L, 5480L, 5482L, 
5484L, 5486L, 5488L, 5489L, 5491L, 5494L, 5496L, 5498L, 5499L, 
5501L, 5504L, 5505L, 5508L, 5510L, 5511L, 5514L, 5516L, 5518L, 
5520L, 5522L, 5524L, 5526L, 5527L, 5530L, 5531L, 5533L, 5536L, 
5537L, 5540L, 5542L, 5544L, 5546L, 5547L, 5550L, 5552L, 5554L, 
5555L, 5558L, 5559L, 5562L, 5563L, 5566L, 5568L, 5569L, 5571L, 
5574L, 5575L, 5578L, 5579L, 5582L, 5584L, 5585L, 5587L, 5590L, 
5592L, 5594L, 5595L, 5598L, 5600L, 5602L, 5604L, 5606L), class = "data.frame")
IRTFM
  • 258,963
  • 21
  • 364
  • 487
awal23
  • 51
  • 7
  • 1
    If you don't want to cute your continuous variable into the categories you are interested in (via something like `cut`), you might be able to use `scale_fill_gradientn` along the lines shown [here](http://stackoverflow.com/a/11301413/2461552) – aosmith Oct 06 '16 at 16:31
  • Thanks! I ended up fixing using my solution from the edit, but your comment was very useful! – awal23 Oct 06 '16 at 16:49
  • @aosmith do you know how I would go about editing the order of criteria for the legend? I'd like 1 mil - 5 mil to be at the bottom. – awal23 Oct 06 '16 at 16:54
  • 1
    You need to put the levels of the new variable in the order you want them. Learning to use `cut` (or `Hmisc::cut2`) might be useful to you if you do this sort of thing periodically. In `cut`, the labels you give the levels follow the order of the categories. – aosmith Oct 06 '16 at 16:59
  • @aosmith Thank you!! Is there any other way that I'm supposed to rate your comments other than voting it as useful? Still pretty new here. – awal23 Oct 06 '16 at 17:04
  • Nope, they're just comments so no rating needed. As you solved your own problem you could put your solution as an answer. – aosmith Oct 06 '16 at 17:07
  • Perhaps you need another library or code to construct `txmap` besides the obvious ggplot2? I get `Error in is.data.frame(map) : argument "map" is missing, with no default` – IRTFM Oct 06 '16 at 20:08

1 Answers1

1

I just created a new column in the population dataframe that summarizes the population based on the ranges that I want to use, and then use that as the criteria for the fill:

txczpop$poprange[txczpop$pop2014 >= 0 & txczpop < 1000] <- "0-1,000"
txczpop$poprange[txczpop$pop2014 >= 1000 & txczpop < 10000] <- "1-10,000"
txczpop$poprange[txczpop$pop2014 >= 10000 & txczpop$pop2014  < 100000] <- "10,000-100,000"
txczpop$poprange[txczpop$pop2014 >= 100000 & txczpop$pop2014  < 1000000] <- "100,000 - 1,000,000"
txczpop$poprange[txczpop$pop2014 >= 1000000 & txczpop$pop2014  <= 5000000] <- "1,000,000 - 5,000,000"

enter image description here

awal23
  • 51
  • 7