ggplot在世界地图上的光栅图像 [英] Raster image on world map in ggplot

查看:78
本文介绍了ggplot在世界地图上的光栅图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连续填充某些值(压力)作为世界地图上的渐变,我正在编写以下代码:

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?

推荐答案

我不确定您想要什么,因为您没有给我们任何数据,但是我做了一些猜测并做到了:

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.

我也将"x"更改为圆形,因为您看不到它的颜色.

Also I changed the "x" to a circle since you couldn't see it's color very well.

这篇关于ggplot在世界地图上的光栅图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆