Reputation: 1215
I want to fill certain values (pressure) continuously as gradient fill on a world map and I am writing the following code:
df = data.frame(phi)
names(df) = lat
df$lon= lon
mdata = melt(df, id=c("lon"))
names(mdata) = c("lon", "lat", "x")
mdata$x = as.numeric(mdata$x)
mdata$lon = as.numeric(mdata$lon)
mdata$lat = as.numeric(as.character(mdata$lat))
wr <- map_data("world")
# Prepare a map of World
wrmap <- ggplot(wr, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "white", colour = "black") +
geom_point(data=mdata, inherit.aes=FALSE, aes(x=lon, y=lat, colour=x), size=3, shape=4) +
scale_fill_gradient("Phi", limits=c(4500,6000)) +
theme_bw() +
coord_equal()
wrmap
Unfortunately the points are coming out discreet.
Any ideas how to fix this?
Upvotes: 2
Views: 897
Reputation: 22817
I am not exactly sure what you want because you didn't give us any data, but I made some guesses and did this:
library(ggplot2)
library(maps)
library(reshape2)
# Generate some fake data
lat <- seq(-90, 90, by = 5)
lon <- seq(-180, 180, by = 10)
phi <- 1500*tcrossprod( sin( pi*lat/180 ), cos( pi*lon/180 ))^ 2 + 4500
# above thanks to @NBAtrends for turning my two ugly for loops into this elegant statement
df = data.frame(phi)
names(df) = lat
df$lon = lon
mdata = melt(df, id = c("lon"))
names(mdata) = c("lon", "lat", "x")
mdata$x = as.numeric(mdata$x)
mdata$lon = as.numeric(mdata$lon)
mdata$lat = as.numeric(as.character(mdata$lat))
wr <- map_data("world")
# Prepare a map of World
wrmap <- ggplot(wr, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "white", colour = "black") +
geom_point(data=mdata, inherit.aes=FALSE,aes(x=lon, y=lat, color=x),size=3) +
scale_color_gradient("Phi", limits = c(4500, 6000)) +
theme_bw() +
coord_equal()
wrmap
Yielding this, which seems close to what you probably want:
This leads me to conclude that the problem is with your data. By comparing it to my fake data, I think you can probably figure out your problem.
Also I changed the "x" to a circle since you couldn't see it's color very well.
Upvotes: 3