Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R GGL绘图图_R_Ggplot2_Geospatial - Fatal编程技术网

R GGL绘图图

R GGL绘图图,r,ggplot2,geospatial,R,Ggplot2,Geospatial,我想用ggplot2(v.9)绘制一张世界地图,它结合了两部分信息。以下示例说明: library(rgdal) library(ggplot2) library(maptools) # Data from http://thematicmapping.org/downloads/world_borders.php. # Direct link: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip # Unpa

我想用ggplot2(v.9)绘制一张世界地图,它结合了两部分信息。以下示例说明:

library(rgdal)
library(ggplot2)
library(maptools)

# Data from http://thematicmapping.org/downloads/world_borders.php.
# Direct link: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
# Unpack and put the files in a dir 'data'

gpclibPermit()
world.map <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3")
world.ggmap <- fortify(world.map, region = "NAME")

n <- length(unique(world.ggmap$id))
df <- data.frame(id = unique(world.ggmap$id),
                 growth = 4*runif(n),
                 category = factor(sample(1:5, n, replace=T)))

## noise
df[c(sample(1:100,40)),c("growth", "category")] <- NA


ggplot(df, aes(map_id = id)) +
     geom_map(aes(fill = growth, color = category), map =world.ggmap) +
     expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
     scale_fill_gradient(low = "red", high = "blue", guide = "colorbar")
库(rgdal)
图书馆(GG2)
图书馆(地图工具)
#数据来自http://thematicmapping.org/downloads/world_borders.php.
#直接链接:http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
#解包并将文件放入目录“数据”
gpclibPermit()

world.map我会对填充和线条颜色使用不同的色调范围:

ggplot(df, aes(map_id = id)) +
  geom_map(aes(fill = growth, color = category), map =world.ggmap) +
  expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
  scale_fill_gradient(high = "red", low = "white", guide = "colorbar") +
  scale_colour_hue(h = c(120, 240))

或者,使用填充表示类别,使用透明度表示增长级别

ggplot(df, aes(map_id = id)) +
  geom_map(aes(alpha = growth, fill = category), map =world.ggmap) +
  expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
  scale_alpha(range = c(0.2, 1), na.value = 1)

这取决于你想展示什么

以防万一,以下是更改线条大小的方法:

ggplot(df, aes(map_id = id)) +
 geom_map(aes(fill = growth, color = category, size = factor(1)), map =world.ggmap) +
 expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
 scale_fill_gradient(high = "red", low = "white", guide = "colorbar") +
 scale_colour_hue(h = c(120, 240)) + 
 scale_size_manual(values = 2, guide = FALSE)

以下是HSV版本:

df$hue <- ifelse(is.na(df$category), 0, as.numeric(df$category)/max(as.numeric(df$category), na.rm=T))
df$sat <- ifelse(is.na(df$growth), 0, df$growth/max(df$growth, na.rm=T))
df$fill <- ifelse(is.na(df$category), "grey50", hsv(df$hue, df$sat))

ggplot(df, aes(map_id = id)) +
 geom_map(aes(fill = fill), map =world.ggmap) +
 expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
 scale_fill_identity(guide = "none")

df$hue一个选项是将增长映射到在多边形质心处绘制的某些点的大小

centroids <- as.data.frame(coordinates(world.map))
df <- data.frame(df,centroids)

choropleth <-ggplot() +
     geom_map(aes(fill = category, map_id = id),data = df, map =world.ggmap) +
     expand_limits(x = world.ggmap$long, y = world.ggmap$lat) + 
    scale_fill_hue(na.value=NA)
choropleth



choropleth + geom_point(aes(x=V1,y=V2,size=growth),data=df) +
    scale_area(range=c(0,3))

centroids什么是world.map?代码中没有定义它。如果我尝试加强(w,region=“NAME”),我会得到一个“无效的多字节字符”错误。请提供可复制的代码。我在加强行中遇到以下错误:“nchar(ID)中的错误:无效的多字节字符串1”我无法在UTF8系统上复制;我怀疑字体编码在这里是个问题。iconv(.)可以从一个编码系统转换到另一个编码系统。谢谢!问题是线条“足够厚”以至于可以被注意到,即使图形打印得很小。我认为你要求的太多了。我认为不可能用一张小纸显示这么多信息。你可以找到改变线条大小的方法,但实际上我认为这没有用……alpha方案非常好!谢谢这让我想,最好的方式来说明它可能是为每个类别设置一个基本颜色,然后确定作为增长函数的饱和度。这或多或少是alpha所做的,特别是如果使用空白主题(或者专门设置BG为白色)。据你所知,有没有比阿尔法方法更直接的方法来获得这个结果?我认为色调和饱和度的结合是好的。ggplot2的未来版本可能具有这样的功能,即色调、饱和度和值作为单独的aes。无论如何,您可以通过手动设置来实现。我会发布更新的。伊恩,你的包看起来不错![1] 它确实增加了一个很好的触感。添加alpha(ed)填充可能看起来很酷。顺便问一下:你知道ggmap软件包吗?基于抽象,它们听起来很相似。这个点子很好。通过使用星星或其他看起来不那么像墨水的东西,这可以变成一件美好的事情。谢谢我知道这个包裹。OpenStreetMap基本上是RGoogleMaps和ggmap的替代品。它可以做一些很酷的事情,比如平铺缓存和地图投影转换。@如果有兴趣,我只是在这里问了一个相关的问题(使用相同的示例):
library(OpenStreetMap)
library(raster)
rastermap <- openmap(c(70,-179),
        c(-70,179),zoom=2,type='bing')
rastermap <- openproj(rastermap)
autoplot(rastermap,expand=FALSE) +
     geom_map(aes(x=70,y=70,fill = category, map_id = id),data = df,
        map =world.ggmap) +
     expand_limits(x = world.ggmap$long, y = world.ggmap$lat) + 
    scale_fill_hue(na.value=NA) + 
    geom_point(aes(x=V1,y=V2,colour=growth),data=df) +
    scale_colour_gradient(low = "red", high = "blue", 
        guide = "colorbar",na.value=NA)