故障排除ggplot()/ maps()运行时问题

时间:2015-08-14 21:39:55

标签: r ggplot2 maps runtime-error

好吧,所以我在制作这张地图时有点挣扎。下面的代码给了我this map,这是我真正想要使用的地图。

map(database= "world", ylim=c(15,90), xlim=c(-180,-24), fill = TRUE, projection = 'gilbert')

这是我用来保存地图信息的代码。

map.dat <- map_data(map(database= "world", ylim=c(15,90), xlim=c(-180,-24), fill = TRUE, projection = 'gilbert'))

现在,当我运行以下代码时,它会在eval(expr,envir,enclos)中给出错误&#39;错误:object&#39; group&#39;找不到&#39;。我不确定这意味着什么。

ggplot(map.dat, aes(x=long, y=lat, group=group, fill=region)) +
  geom_polygon() +
  geom_point(data = basindf, aes(x = basindf$latitude, y = basindf$longitude)) +
  theme(legend.position = "none")

我设置了&#39; group = NULL&#39;和&#39; fill = NULL&#39;这似乎允许我绘制,但它只显示this,这不是我想要的。地图消失了!

我该怎么做才能解决这个问题?此外,我想远离点并创建线条。我如何根据某个ID制作线条?

编辑:似乎有些人需要basindf进行故障排除。我已在下面添加了前20行。

"","id","year","month","date","basin","latitude","longitude","wind speed"
"1","1902276N14266",1902,"October",1902-10-03,"EP",-93.8,14,30
"2","1902276N14266",1902,"October",1902-10-03,"EP",-94,14.5,30
"3","1902276N14266",1902,"October",1902-10-03,"EP",-94.2,15,30
"4","1902276N14266",1902,"October",1902-10-03,"EP",-94.3,15.5,30
"5","1902276N14266",1902,"October",1902-10-04,"EP",-94.4,16,30
"6","1902276N14266",1902,"October",1902-10-04,"EP",-94.5,16.5,30
"7","1902276N14266",1902,"October",1902-10-04,"EP",-94.6,17,30
"8","1902276N14266",1902,"October",1902-10-04,"EP",-94.7,17.5,30
"9","1902276N14266",1902,"October",1902-10-05,"EP",-94.8,18,30
"10","1902276N14266",1902,"October",1902-10-05,"EP",-94.9,18.5,30
"11","1902276N14266",1902,"October",1902-10-05,"NA",-94.9,18.7,35
"12","1902276N14266",1902,"October",1902-10-05,"NA",-94.7,18.8,45
"13","1902276N14266",1902,"October",1902-10-06,"NA",-94.4,18.9,55
"14","1902276N14266",1902,"October",1902-10-06,"NA",-94,19.1,60
"15","1902276N14266",1902,"October",1902-10-06,"NA",-93.7,19.3,65
"16","1902276N14266",1902,"October",1902-10-06,"NA",-93.3,19.5,75
"17","1902276N14266",1902,"October",1902-10-07,"NA",-92.9,19.7,85
"18","1902276N14266",1902,"October",1902-10-07,"NA",-92.5,20,90
"19","1902276N14266",1902,"October",1902-10-07,"NA",-92,20.3,90
"20","1902276N14266",1902,"October",1902-10-07,"NA",-91.5,20.7,90

1 个答案:

答案 0 :(得分:1)

你有两个主要问题。

首先,您得到的错误是因为您在aes()调用中选择了ggplot(),这意味着这些值会继承到所有图层。这意味着它也尝试在group=图层中设置geom_point,但您没有该图层的组。您可以使用

禁用继承的美学
ggplot(map.dat, aes(x=long, y=lat, group=group, fill=region)) +
  geom_polygon() +
  geom_point(data = basindf, aes(x = basindf$latitude, y = basindf$longitude), inherit.aes=FALSE) + 
  theme(legend.position = "none")

或者你可以选择每层的aes

ggplot(map.dat) +
  geom_polygon(aes(x=long, y=lat, group=group, fill=region)) +
  geom_point(data = basindf, aes(x = basindf$latitude, y = basindf$longitude)) + 
  theme(legend.position = "none")

您的另一个问题是您使用投影而非点数据转换了地图数据。 enter image description here

您可以使用mapproj转换数据,以便它们具有相同的比例

ggplot(map.dat) +
  geom_polygon(aes(x=long, y=lat, group=group, fill=region)) +
  geom_point(data = data.frame(mapproject(basindf$latitude, basindf$longitude, "gilbert")), aes(x = x, y = y)) +
  theme(legend.position = "none")

这给出了

enter image description here