Reputation: 3557
I am working on a project where I need to plot some points on a sphere. What I have are the longitude and latitude of each point, and a value at each point. For example,
longitude latitude value
123 23 1.3
75 -34 2.8
190 48 2.1
...
I want a plot like the following. I also want the size of the point to be proportional to the value in the data set. So 2.8 will have a bigger point and 1.3 will have a smaller point, etc. The world map is optional.
I wonder if there are any packages in R that can do this job? Any suggestions or codes are greatly appreciated!
Upvotes: 2
Views: 1719
Reputation: 18749
A simple example using base
plot and packages sp
, rgdal
and maptools
:
library(sp)
library(maptools)
library(rgdal)
xy <- data.frame(lon=c(-130,110,3,45),lat=c(60,-10,50,30)) #Some coordinates
value <- data.frame(value=c(1.5,0.8,2.3,2)) #Some values for the point size
df <- SpatialPointsDataFrame(xy,value,proj4string=CRS("+proj=lonlat"))
dfMoll <- spTransform(df, CRS("+proj=moll")) #Mollweide projection of the data
data(wrld_simpl) # A base world map
wrld_moll <- spTransform(wrld_simpl, CRS("+proj=moll")) # ... that we projects as well
plot(wrld_moll) #... and plot
points(dfMoll, cex=dfMoll$value, pch=20, col="red") #...with our data points
Upvotes: 7
Reputation: 49640
Look at the mapproj package. It will do various map projections for you. It does not do the plotting, but rather projects the points for you to then pass to the function of your choice.
Upvotes: 2
Reputation: 49033
Maybe a way to do it with ggplot2
:
ggplot() +
geom_polygon(data=world,aes(x=long, y=lat, group=group), fill=NA,colour="black") +
geom_point(data=d, aes(x=longitude, y=latitude, size=value), color="red") +
coord_map("mollweide")
This gives the following map, with some glitches unfortunately :
You can use coord_map
without drawing a map, by the way :
ggplot() +
geom_point(data=d, aes(x=longitude, y=latitude, size=value), color="red") +
coord_map("mollweide")
Upvotes: 6