【问题标题】:R ggplot2 combining geom_tile and coord_map("moll")R ggplot2 结合 geom_tile 和 coord_map("moll")
【发布时间】:2013-07-25 16:26:49
【问题描述】:

我使用标准投影制作了一张地图。我猜是“墨卡托”。 这是代码,

p=ggplot(output)

p=p+geom_tile(aes(x=lon,y=lat,fill=dcm))

p=p+scale_fill_gradientn("Depth of DCM (m)",colours=rgb(rgb[1:100,1],rgb[1:100,2],rgb[1:100,3]),
limits=c(15,200),labels=c(25,50,75,100,125,150,175,200),breaks=c(25,50,75,100,125,150,175,200))

p = p+guides(fill = guide_colorbar(barwidth = 0.5, barheight = 9))

p=p+layer(data=coastline.world,geom="polygon",mapping=aes(x=longitude,y=latitude))



p=p+theme(text=element_text(family="Times",size=9))

p=p+theme(legend.title = element_text(face = 'plain'))

p=p+guides(colour = guide_legend(title.hjust = 0.5))

当我尝试将投影更改为“mollweide”时

使用

p=p+coord_map("moll")

程序开始运行但从未停止。

你有什么想法吗?

谢谢 亚历克斯

data.frame 海岸线.world 来自

library(oce)  
data(coastlineWorld)  
coastline.world=data.frame(longitude=coastlineWorld[["longitude"]],latitude=coastlineWorld[["latitude"]]) 

输出的第一行:

structure(list(lon = c(-180, -179.5, -179, -178.5, -178, -177.5
), lat = c(-59.5, -59.5, -59.5, -59.5, -59.5, -59.5), dcm = c(NA, 
41.4461206739867, 45.6921865291417, 48.135154847963, 48.4013604947836, 
46.9140989480546)), .Names = c("lon", "lat", "dcm"), row.names = c(NA, 
6L), class = "data.frame")

【问题讨论】:

  • 欢迎来到 Stack Overflow!如果您向我们提供了您的数据的可重现示例(即outputcoastline.world),它将让我们重现问题并找出问题所在。您可以通过发布dput(output)dput(coastline.world) 的结果来给出一个可重现的示例,但如果它们是大型数据框,您可以改为使用dput(head(coastline.world))dput(head(output))(只要您确认问题仍然存在)跨度>
  • 有什么更新吗?我在类似的问题上苦苦挣扎,我刚刚开始认为 ggplot 还没有准备好制作这种情节。

标签: r ggplot2


【解决方案1】:

可能只是数据量太大。 coastline.world 有超过 400K 行。 我还将您的layer 更改为geom_polygongeom_points

所以我尝试了一些统一抽样(假设每 100 行选择一次,抽取总数据的 1%)。

  newcw <- coastline.world[seq(1, nrow(coastline.world), by = 100),]

现在我尝试了

 p <- p+layer(data=newcw,geom="point",mapping=aes(x=longitude,y=latitude))
 p <- p+geom_point(data=newcw, mapping=aes(x=longitude,y=latitude))

然后是coord_map,渲染正常。

 p <- p+coord_map("moll")

这是我得到的:

您可能需要修改颜色才能获得想要的效果。

希望能帮助你前进。

【讨论】:

  • 感谢您的帮助。 Coastline.world 的大小不是问题,因为我可以使用 geom_polygon 和 coord_map("moll") 绘制地图。当我尝试使用 geom_tile 绘制输出时出现问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
  • 1970-01-01
相关资源
最近更新 更多