0
votes

I know this question has been asked several times here . But I seem to be getting the same error message "Discrete value supplied to continuous scale" even when I think my code is good enough. I don't understand the reason. I import the longitude and latitude value from a csv file and construct a data frame. But I am not able to plot the points on the map all I am getting is a map of Europe with no points.

library(ggmap)
library(ggplot2)

g <- data.frame(lon=longitude, lat=latitude)

Europe <- get_map(location="Europe", zoom=3)

map <- ggmap(Europe)
map + geom_point(data=g, aes(x=longitude, y=latitude), color="blue", size=3)

sample data:

latitude  
28.42222222
29.76328
18.079733
19.2539846
27.66955857
23.1525609
12.958251
16.3577847
28.5912216
19.254039
25.1450198
23.15276151
19.2539879
19.253986
28.6598034
23.1530814

longitude
100.7680556
95.36327
73.418583
73.1403781
84.42628936
79.9333897
77.5543671
80.8691776
77.1237985
73.1403918
85.2033903
79.9332153
73.140418
73.1404192
77.1903172
79.9329372

EDITED DPUT

> dput(g)
structure(list(lon = structure(c(1L, 16L, 6L, 2L, 14L, 12L, 9L, 
13L, 7L, 3L, 15L, 11L, 4L, 5L, 8L, 10L), .Label = c("100.7680556", 
"73.1403781", "73.1403918", "73.140418", "73.1404192", "73.418583", 
"77.1237985", "77.1903172", "77.5543671", "79.9329372", "79.9332153", 
"79.9333897", "80.8691776", "84.42628936", "85.2033903", "95.36327"
), class = "factor"), lat = structure(c(13L, 16L, 3L, 4L, 12L, 
8L, 1L, 2L, 14L, 7L, 11L, 9L, 6L, 5L, 15L, 10L), .Label = c("12.958251", 
"16.3577847", "18.079733", "19.2539846", "19.253986", "19.2539879", 
"19.254039", "23.1525609", "23.15276151", "23.1530814", "25.1450198", 
"27.66955857", "28.42222222", "28.5912216", "28.6598034", "29.76328"
), class = "factor")), .Names = c("lon", "lat"), row.names = c(NA, 
-16L), class = "data.frame")
2
You probably need to coerce your latitude and longitude variables in the data frame to a numeric value. Can you please put your read.csv() code and perhaps give a reproducible example? That would help a lot. - Ken Yeoh
You didn't supply any sample data, so I can't tell if longitude and latitude are really numeric values or not. - cory
What's the output of str(g)? Are both columns numerics? Also, the geom_point() points to your original longitude and latitude vectors, but your columns in g are named lon and lat. - user2034412

2 Answers

0
votes

Your g is using lat and long as factors, rather than numerics. Try converting it to numeric:

g$lon<-as.numeric(as.character(g$lon))
g$lat<-as.numeric(as.character(g$lat))
0
votes

After converting lon and lat to numeric, as user2034412 suggested, this is the result. I thank the same person for the comment on as.character.

g$lon <- as.numeric(as.character(g$lon))
g$lat <- as.numeric(as.character(g$lat))

Europe <- get_map(location="Europe", zoom=3)

map <- ggmap(Europe) +
 geom_point(data=g, aes(x=lon, y=lat), color="blue", size=3)
  geom_point(data=g, aes(x=lon, y=lat))

map enter image description here