我正在尝试使用RgoogleMaps保存带有覆盖到png文件的点的地图。 我能够在绘图设备中成功创建地图和叠加点,但无法弄清楚如何将点写入我的地图的png文件。
我使用的是Mac OS X Lion 10.7.5 R版本2.15.0
使用R docs中的示例代码我得到了相同的结果:我打开的绘图设备上的行,但没有写入png文件。
以下是我正在使用的示例代码:
lat = c(40.702147,40.718217,40.711614);
lon = c(-74.012318,-74.015794,-73.998284);
center = c(mean(lat), mean(lon));
zoom <- min(MaxZoom(range(lat), range(lon)));
#this overhead is taken care of implicitly by GetMap.bbox();
MyMap <- GetMap(center=center, zoom=zoom,markers = '&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|40.718217,-73.998284', destfile = "MyTile1.png");
tmp <- PlotOnStaticMap(MyMap, lat = c(40.702147,40.711614,40.718217), lon = c(-74.015794,-74.012318,-73.998284), destfile = "MyTile1.png", cex=1.5,pch=20,col=c('red', 'blue', 'green'), add=FALSE);
#and add lines:
PlotOnStaticMap(MyMap, lat = c(40.702147,40.711614,40.718217), lon = c(-74.015794,-74.012318,-73.998284), lwd=1.5,col=c('red', 'blue', 'green'), FUN = lines, add=TRUE)
我知道png设备正在运行,因为它有效:
png('my.png')
plot(seq(1:10))
dev.off()
感谢您的帮助。
答案 0 :(得分:1)
要使用PlotOnStaticMap向png文件添加点和线,可能需要在调用PlotOnStaticMap之前为绘图设备显式设置type ='cairo-png'。在我的机器和我的R环境中,这可以工作:
png('MyTile1.png',type='cairo-png')
dev.cur()
png 3 # returned value
但这不是
png('MyTile1.png')
dev.cur()
quartz_off_screen 2 # returned value
下面的代码将点和行写入MyTile1.png:
lat = c(40.702147,40.718217,40.711614);
lon = c(-74.012318,-74.015794,-73.998284);
center = c(mean(lat), mean(lon));
zoom <- min(MaxZoom(range(lat), range(lon)));
MyMap <- GetMap(center=center, zoom=zoom,markers = '&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|40.718217,-73.998284')
# explicitly set device to png with type='cairo-png'
png('MyTile1.png',type='cairo-png')
# add points
tmp <- PlotOnStaticMap(MyMap, lat = c(40.702147,40.711614,40.718217), lon = c(-74.015794,-74.012318,-73.998284), cex=1.5,pch=20,col=c('red', 'blue', 'green'));
#and add lines:
PlotOnStaticMap(MyMap, lat = c(40.702147,40.711614,40.718217), lon = c(-74.015794,-74.012318,-73.998284), lwd=1.5,col=c('red', 'blue', 'green'), FUN = lines, add=TRUE)
dev.off()
答案 1 :(得分:0)
您可以使用
关注最后一行dev.print(png, width = 800, file = "MyTile2.png")
或类似的东西