[R-sig-Geo] colorkey print order is incorrect??

Roger Bivand Roger.Bivand at nhh.no
Tue Jan 20 08:20:36 CET 2009


On Mon, 19 Jan 2009, Jim Burke wrote:

> Question. When I use the code below to print
> a list of values to the right of my graph,
> the colorkey list appears unusual.  At the top
> the list of percentages tops out at 90.87%
> instead of the expected 100%. The 100% appears
> at the bottom below the lowest percent 14.29%.

Yes, very nice! Your variable CK_PCT is clearly a categorical variable, 
with a % in it, and has been sorted correctly, as "100.00%" < "14.29%" in 
alphanumeric sort order. Maybe:

tx3_sp$nCK_PCT <- (100*tx3_sp$CK_TOT)/tx3_sp$TOT_VOTE

then map "nCK_PCT" (assuming that CK_TOT and TOT_VOTE are numeric - some 
software makes all fields in say shapefiles into strings)? If they aren't 
numeric (see below), coerce them using as.numeric() first.

You can check the way in which the data are represented by using the str() 
command, here

str(as(tx3_sp, "data.frame"))

will show the representation of your variables (str(tx3_sp) would show the 
geometries too, which you don't need here).

Roger

>
> The numbers of tx3_sp CK_PCT are read in as follows.
> ID is the PCT and CK_PCT is what's plotted.
>> tx3_sp
> An object of class "SpatialPolygonsDataFrame"
> Slot "data":
>   PCT    CD   COMM   JP     LD    SB    SS CK_TOT  CK_PCT TOT_VOTE
> 1  1120 CD-03 COMM01 JP-3 LD-102 SB-12 SS-16   1693  90.87%     1863
> 2  1121 CD-05 COMM01 JP-3 LD-102 SB-12 SS-16    634  54.28%     1168
> 3  1124 CD-05 COMM01 JP-3 LD-102 SB-12 SS-16    584  43.39%     1346
> 4  1135 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    815  33.51%     2432
> 5  1136 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    446  52.84%      844
> 6  1719 CD-03 COMM01 JP-3 LD-102 SB-12 SS-16   1392  68.17%     2042
> 7  1720 CD-03 COMM01 JP-3 LD-102 SB-12 SS-16    523  66.20%      790
> 8  1723 CD-03 COMM01 JP-3 LD-102 SB-12 SS-16    788  82.34%      957
> 9  1724 CD-03 COMM01 JP-3 LD-102 SB-12 SS-16    994  69.41%     1432
> 10 1725 CD-32 COMM01 JP-3 LD-102 SB-12 SS-16     31  59.62%       52
> 11 1802 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    999  44.20%     2260
> 12 1803 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    672  44.24%     1519
> 13 1804 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    280  39.94%      701
> 14 1805 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    654  48.12%     1359
> 15 1806 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    495  37.61%     1316
> 16 1807 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    695  40.81%     1703
> 17 1809 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    563  47.11%     1195
> 18 1810 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    461  42.92%     1074
> 19 1811 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    779  42.61%     1828
> 20 1812 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    585  37.77%     1549
> 21 1816 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    311  32.16%      967
> 22 1817 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    528  44.18%     1195
> 23 1819 CD-32 COMM01 JP-3 LD-102 SB-12 SS-08    338  47.88%      706
> 24 1820 CD-32 COMM01 JP-3 LD-102 SB-12 SS-16    305  70.11%      435
> 25 1821 CD-03 COMM01 JP-3 LD-102 SB-12 SS-16    617  74.07%      833
> 26 2115 CD-03 COMM02 JP-2 LD-102 SB-12 SS-16      1  14.29%        7
> 27 2121 CD-03 COMM02 JP-2 LD-102 SB-12 SS-16    646  50.91%     1269
> 28 2122 CD-03 COMM02 JP-2 LD-102 SB-12 SS-16    120  60.00%      200
> 29 2126 CD-03 COMM02 JP-2 LD-102 SB-12 SS-16    511  57.55%      888
> 30 2127 CD-03 COMM02 JP-2 LD-102 SB-12 SS-16    522  41.04%     1272
> 31 2128 CD-03 COMM02 JP-2 LD-102 SB-12 SS-16    390  49.49%      788
> 32 2129 CD-05 COMM02 JP-2 LD-102 SB-12 SS-16    243  41.33%      588
> 33 2150 CD-03 COMM02 JP-2 LD-102 SB-12 SS-16      2 100.00%        2
> 34 2151 CD-05 COMM02 JP-2 LD-102 SB-12 SS-16     36  38.71%       93
> 35 2200 CD-03 COMM02 JP-3 LD-102 SB-12 SS-16    959  88.39%     1085
> 36 2204 CD-05 COMM02 JP-3 LD-102 SB-12 SS-16    363  90.07%      403
> 37 2205 CD-03 COMM02 JP-3 LD-102 SB-12 SS-16    921  90.38%     1019
> 38 2206 CD-05 COMM02 JP-3 LD-102 SB-12 SS-16    704  45.77%     1538
>
>
>
> Is there any way to move the 100% to the top
> when the colorkey legend prints?
>
>   spplot(tx3_sp, c("CK_PCT"),
>       names.attr = c("Carol Kent 2008"),
>       colorkey=list(height=1.0, width=0.6),
>       main = "HD102 - Carol Kent",
>       sp.layout = list("sp.text",coordinates(tx3_sp),
>       as.character(tx3_sp$PCT,cex = .02),
>       as.table = TRUE))
>
> See the attached jpg file. I hope this goes through.
>
> Forgive my dumb question as I am new to R. I like
> R after reading the NYTIMES article last week;
> "Data Analysts Mesermized by Power of R". Query
> the NYTIMES for this. And read the "Are U Ready
> for R" article with its comments as well.
>
> Thanks,
> Jim Burke
>
>
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list