0

I am trying to plot a heat Map in R using ggmap. In this regard, i have followed this example.

Density2d Plot using another variable for the fill (similar to geom_tile)?

But when i execute the final line . i.e.

print(pred.stat.map.final) 

in the "Setting the colorMap" section , I get the following error

Error: Discrete value supplied to continuous scale

Here's the Data :

Link to data for KAN.csv : https://www.dropbox.com/s/ainm2gjewll7xk0/KAN.csv

StationName  | Total PrecipitationCount | Latitude | Longitude

19.8 SE COLDWATER KS US 5626    37.0571 -99.09
ABBYVILLE 1.1 SSE KS US 21587   37.9556 -98.197
ABILENE 0.7 E KS US 12526   38.9197 -97.2095
ABILENE 1.1 NE KS US    7284    38.9314 -97.2073
ABILENE 11.9 N KS US    20222   39.09   -97.1823
ABILENE 12.7 S KS US    9232    38.7383 -97.261
ABILENE 2.4 WSW KS US   19335   38.9117 -97.2655
ABILENE 3.2 ENE KS US   15648   38.9393 -97.1672
ABILENE 5.4 E KS US 7431    38.9341 -97.1238
ABILENE KS US   16493   38.9266 -97.2129
ABILENE KS US   39004   38.9266 -97.213
ABILENE KS US   1196    38.92667    -97.21306
AGENDA KS US    42809   39.7083 -97.4311
AGENDA KS US    13711   39.7134 -97.4516
ALBERT 5 SE KS US   -18998  38.3899 -98.9504
ALBERT 5 SE KS US   25149   38.39   -98.95028
ALEXANDER KS US -1006972    38.4694 -99.552
ALMA 6.1 NW KS US   36538   39.0674 -96.38
ALTA VISTA KS US    43886   38.8619 -96.4725
ALTA VISTA KS US    16418   38.862  -96.4892
ALTAMONT KS US  73968   37.1944 -95.2875
ALTAMONT KS US  8160    37.1952 -95.3002
ALTON 10.0 S KS US  2344    39.3228 -98.9537
ALTON 2 SW KS US    7696    39.44806    -98.97
ALTON 2 SW KS US    3198    39.4483 -98.9702
ALTON KS US -267345 39.47667    -98.97139
ANDALE 0.4 SSE KS US    0   37.7873 -97.6257
ANDALE 6.4 WSW KS US    46338   37.7636 -97.7399
ANTHONY 0.4 NNW KS US   14754   37.1587 -98.0315
ANTHONY KS US   26363   37.155  -98.0282
ANTHONY KS US   29615   37.155  -98.02833
ANTONINO 4.5 WSW KS US  24322   38.7555 -99.4579
ANTONINO 7.5 W KS US    149 38.7845 -99.5194
ARGONIA KS US   924 37.26167    -97.76889
ARGONIA KS US   -11948  37.2618 -97.769
ARKANSAS CITY 2.0 SSE KS US 13471   37.0431 -97.0239
ARKANSAS CITY 3.1 N KS US   17606   37.1142 -97.0385
ARKANSAS CITY 3.7 S KS US   15199   37.0146 -97.0428
ARKANSAS CITY 3.9 SSW KS US 6219    37.0141 -97.0615
ARKANSAS CITY KS US 58326   37.0631 -97.0399
ARLINGTON 1.7 ESE KS US 26159   37.8859 -98.15
ARLINGTON 5.5 SSE KS US 36858   37.8186 -98.1545
ARLINGTON KS US 5496    37.8966 -98.2711
ARLINGTON KS US -15003  37.89667    -98.27111
ARLINGTON KS US -22910  37.933  -98.25
ARNOLD 4.8 NNW KS US    38970   38.6956 -100.0664
ASHLAND 14.6 SSE KS US  30038   37.0002 -99.6467
ASHLAND 7.5 SE KS US    22135   37.111  -99.6717
ASHLAND KS US   34626   37.1941 -99.7632
ASSARIA 4.3 SE KS US    1544    38.6454 -97.5381
ATCHISON 1 S KS US  10820   39.54833    -95.11806
ATCHISON KS US  60155   39.5755 -95.1108
ATLANTA KS US   -32405  37.4372 -96.7669
ATLANTA KS US   -4907   37.43722    -96.76694
ATWOOD 0.5 SSW KS US    18164   39.8028 -101.043
ATWOOD 1.5 E KS US  10248   39.8126 -101.0137
ATWOOD 11 N KS US   12839   39.9761 -101.0579
ATWOOD 8 SSE KS US  28815   39.69778    -100.97083
ATWOOD 8 SSE KS US  4162    39.698  -101.0039
ATWOOD 8 SSE KS US  -380    39.69806    -101.00389
ATWOOD KS US    -30204  39.7996 -101.0421
ATWOOD KS US    -91615  39.8086 -101.0777
AUBURN KS US    7900    38.9019 -95.8167
AUBURN KS US    -141001 38.9088 -95.815
AUBURN KS US    19261   38.9269 -95.8144
AUGUSTA 0.8 NW KS US    5373    37.6994 -96.9861
AUGUSTA 1.0 S KS US 28996   37.6782 -96.9723
AUGUSTA KS US   63270   37.6983 -96.9705
AURORA 7.7 SW KS US 14317   39.3595 -97.6105
AXTELL KS US    -497939 39.86778    -96.25444
AXTELL KS US    -1229101    39.8678 -96.2547
BAILEYVILLE KS US   7905    39.83083    -96.18306
BAILEYVILLE KS US   20899   39.8311 -96.1833
BARNARD 1 S KS US   247 39.1774 -98.0413
BARNARD 1 S KS US   44871   39.1886 -98.0468
BARNARD 1 S KS US   -6909   39.1897 -98.0419
BARNARD 7 W KS US   49393   39.2144 -98.1766
BARNARD 7.5 ENE KS US   36571   39.2224 -97.9108
BARNES 0.2 N KS US  52743   39.7144 -96.8729
BARTLETT KS US  -8728   37.0485 -95.2142
BARTLETT KS US  -74696  37.0485 -95.2145
BAZINE 0.4 SSW KS US    11284   38.4405 -99.6944
BEARDSLEY 1 N KS US 4346    39.8245 -101.23
BEARDSLY 7.7 S KS US    14305   39.6981 -101.2252
BEATTIE 2 NNW KS US 15095   39.89639    -96.425
BEATTIE 2 NNW KS US 17701   39.8966 -96.4247
BEATTIE 2 NNW KS US 9008    39.8966 -96.425
BEAUMONT 0.1 SW KS US   8556    37.658  -96.533
BEAUMONT 4.9 SSE KS US  36775   37.59   -96.51
BEAUMONT KS US  7100    37.65556    -96.53528
BEAUMONT KS US  -153940 37.6556 -96.5353
BEAUMONT KS US  2967    37.6557 -96.5352
BEELER 0.1 ENE KS US    4736    38.4446 -100.1952
BELLE PLAINE 4 W KS US  -69130  37.393  -97.3497
BELLEFONT 3 S KS US 10447   37.8263 -99.6542
BELLEVILLE KS US    -43995  39.81833    -97.6375
BELLEVILLE KS US    -78074  39.8186 -97.6377
    BELLEVILLE KS US    -131545 39.8233 -97.6386
    BELOIT 6.4 SW KS US 10212   39.4073 -98.2007
    BELOIT 8.1 ESE KS US    19016   39.407  -97.977

Here's my code :

  1. Reading my File .

    csvdata_Kansas  = read.csv(file ="KAN.csv")
    
  2. Using sqldf() to get reduced Subset .

    StationList_Precipitation  = sqldf("Select distinct(STATION_NAME) as Station_Name, 
                               sum(PRCP) as TotalPrecipitationCount, LATITUDE as Latitude, LONGITUDE as Longitude from csvdata_Kansas where LATITUDE <> 'unknown' and LONGITUDE <> 'unknown'
                               group by STATION_NAME, LATITUDE, LONGITUDE")
    
  3. I get following kind of result .

     head(StationList_Precipitation)
    

enter image description here

  1. I plot map .

    map.in <- get_map(location = c(lon = -94, lat = 42),
              source = c("osm"))
    ggmap(map.in)
    
  2. Map Shows up . enter image description here

  3. I then execute this code , to get a heat map on the above map .

    colormap <- c("Violet","Blue","Green","Yellow","Red","White")
    head(StationList_Precipitation)
    pred.stat.map.final <- ggmap(map.in) %+% StationList_Precipitation +
    aes(x  =Longitude,
    y = Latitude,
    z = TotalPrecipitationCount) +
    stat_summary2d(fun = median, 
             binwidth = c(.05, .05),
             alpha = 1.0) + 
     scale_fill_gradientn(name = "Median",
                   colours = colormap,
                   space = "Lab") + 
    labs(x = "Longitude",
    y = "Latitude") +
    coord_map()
    print(pred.stat.map.final)
    
  4. I get error in the last line i.e.

    print(pred.stat.map.final)
    

    The error is :

    Error: Discrete value supplied to continuous scale
    

What can be the problem?

Community
  • 1
  • 1
Mangesh Kaslikar
  • 591
  • 3
  • 13
  • 23
  • 1
    Are you running that example exactly as shown? If so I can't reproduce. If not, show us what you actually did. – Ista Feb 27 '14 at 01:40
  • Hi Ista, I have edited my answer now, please take a look . – Mangesh Kaslikar Feb 27 '14 at 02:09
  • This is not reproducible because we don't have KAN.csv. Either post it somewhere where we can download it or include the output of 'dput(StationList_Precipitation)' in your question. – Ista Feb 27 '14 at 02:32
  • Hi Ista, I have pasted 100 rows of my data. Please take a look – Mangesh Kaslikar Feb 27 '14 at 03:07
  • 1
    Please use `dput(head(StationList_Precipitation,10))` instead of just rows of data. Get rid of the screen shot! – mnel Feb 27 '14 at 03:11
  • I can't read you copy-and-pasted data, please use dput or post the file somewhere we can download it. Bottom line is we need to actually run your code, and right now we still can't do that. – Ista Feb 27 '14 at 03:12
  • Hi Ista, heres the link https://www.dropbox.com/s/ainm2gjewll7xk0/KAN.csv , you can download the file KAN.csv from here .thanks – Mangesh Kaslikar Feb 27 '14 at 07:16
  • Your Latitude and Longitude columns from KAN.csv were imported as factors. You apparently need to start with a simple introductory R tutorial to learn the basics of data import, types, classes etc. Many are available on the R website. – Ista Feb 27 '14 at 14:28
  • 1
    Thanks Ista, I read sections on factors and set the stringAsFactors = FALSE while importing the data , that did the trick. – Mangesh Kaslikar Feb 28 '14 at 07:40

1 Answers1

0

Answer (in comments by @ista):

The Latitude and Longitude columns from KAN.csv were imported as factors.

Solution

Set stringAsFactors = FALSE while importing the data.

Roman
  • 4,744
  • 2
  • 16
  • 58