提供给连续刻度 R 的离散值
Discrete value supplied to continous scale R
我知道这个问题已经被问过好几次了。但是我似乎收到了相同的错误消息 "Discrete value supplied to continuous scale" 即使我认为我的代码已经足够好了。我不明白这是为什么。我从 csv
文件导入经度和纬度值并构建数据框。但是我无法在地图上绘制点,我得到的只是一张没有点的欧洲地图。
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)
示例数据:
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
编辑输出
> 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")
您的 g 使用纬度和经度作为因数,而不是数字。尝试将其转换为数字:
g$lon<-as.numeric(as.character(g$lon))
g$lat<-as.numeric(as.character(g$lat))
按照user2034412的建议将lon和lat转换为数值后,结果是这样的。我感谢同一个人对 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))
地图
我知道这个问题已经被问过好几次了。但是我似乎收到了相同的错误消息 "Discrete value supplied to continuous scale" 即使我认为我的代码已经足够好了。我不明白这是为什么。我从 csv
文件导入经度和纬度值并构建数据框。但是我无法在地图上绘制点,我得到的只是一张没有点的欧洲地图。
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)
示例数据:
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
编辑输出
> 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")
您的 g 使用纬度和经度作为因数,而不是数字。尝试将其转换为数字:
g$lon<-as.numeric(as.character(g$lon))
g$lat<-as.numeric(as.character(g$lat))
按照user2034412的建议将lon和lat转换为数值后,结果是这样的。我感谢同一个人对 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))
地图