【问题标题】:Using R and ggplot to draw separate lines between GPS coordinates使用 R 和 ggplot 在 GPS 坐标之间绘制单独的线
【发布时间】:2016-02-29 18:12:36
【问题描述】:

我正在努力使用 R 中的 ggplot 绘制一些线。我需要连接每一对(按 INDEX 分组)。 我使用的数据是:

INDEX | LATITUDE | LONGITUDE | VALUE
1       53,071118  -6,063786   40.3
1       53,076478  -6,067592   40.3
2       53,071118  -6,063786   60.7
2       53,099204  -6,067235   60.7

还有代码:

require(ggmap);
require(ggplot2);
options(warn=-1)

mydata = read.delim2("mydata.csv", TRUE, "\t")
df=as.data.frame(mydata)

City="Dublin, Ireland"

baseMap = get_map(location = City, zoom = 11, maptype = "terrain")
map <- ggmap(baseMap) +
  geom_path(aes(x=LONGITUDE, y=LATITUDE, group=INDEX), data=df, alpha=0.2)
map

在最后两行,错误是“错误:提供给连续刻度的离散值”。 我该如何克服呢?

编辑: 我插入了

df$LATITUDE=as.numeric(as.character(df$LATITUDE))
df$LONGITUDE=as.numeric(as.character(df$LONGITUDE))
df$INDEX=as.numeric(as.character(df$INDEX))

它开始工作了。请记住,坐标必须是点,而不是逗号。

【问题讨论】:

  • 我想你想检查 df 中的类。 str(df) 会告诉你课程。尽管 ggplot 期望数字,但您可能有因素。
  • 谢谢,原来如此!我将 df$LATITUDE=as.numeric(df$LATITUDE) df$LONGITUDE=as.numeric(df$LONGITUDE) df$INDEX=as.numeric(df$INDEX) 添加到代码中,现在它运行了。现在我必须看看为什么没有任何东西被绘制到地图上。

标签: r plot ggplot2 maps ggmap


【解决方案1】:

我只需要添加

df$LATITUDE=as.numeric(as.character(df$LATITUDE))
df$LONGITUDE=as.numeric(as.character(df$LONGITUDE))
df$INDEX=as.numeric(as.character(df$INDEX))

并将数据中的“,”替换为“.”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    相关资源
    最近更新 更多