R ggplot2 combining geom_tile and coord_map(“moll”

2019-07-17 04:02发布

I made a map using the standard projection. I guess it is "mercator". Here is the code,

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

When I try to change the projection to "mollweide"

using

p=p+coord_map("moll")

the program starts to run but never stops.

Do you have any ideas?

Thanks Alex

the data.frame coastline.world comes from

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

first lines of output:

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

标签: r ggplot2
1条回答
Summer. ? 凉城
2楼-- · 2019-07-17 04:39

It might just be that the data size is too large. coastline.world had over 400K rows. I also changed your layer which had geom_polygon to geom_points.

So I tried some uniform sampling (Pick say every 100th row, taking 1% of the total data).

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

Now when I tried

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

And then the coord_map, rendered okay.

 p <- p+coord_map("moll")

This is what I got:

enter image description here

You might have to tinker with the colors to get what you intend.

Hope that helps you move forward.

查看更多
登录 后发表回答