繁体   English   中英

提供给连续刻度R的离散值

[英]Discrete value supplied to continous scale R

我知道这个问题在这里已经问过好几次了。 但是,即使我认为我的代码足够好,我似乎也会收到相同的错误消息“为连续刻度提供离散值”。 我不明白原因。 我从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

> 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))

将lon和lat转换为数值后,如user2034412所建议的,这就是结果。 我感谢同一个人对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))

地图 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM