首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R:在地图上绘制圆形直方图/玫瑰图

R:在地图上绘制圆形直方图/玫瑰图
EN

Stack Overflow用户
提问于 2014-02-10 20:03:11
回答 2查看 2.8K关注 0票数 5

我正在尝试在地图上的特定坐标上绘制玫瑰图/圆形直方图,类似于在mapplots包中在地图上绘制饼状图。

下面是一个用mapplots生成的示例(参见下面的代码),我想用玫瑰图代替饼图

circular包允许我绘制玫瑰图,但我无法将其与mapplots包集成。有任何替代包或代码的建议来实现这一点吗?

在回答问题时为代码制作了地图。这都是基于mapplots包的。我下载了地图的shapefile (我想是从http://www.freegisdata.org/下载的)

代码语言:javascript
复制
library(mapplots)
library(shapefiles)

xlim = c(-180, 180)
ylim = c(-90, 90)

#load shapefile
wmap = read.shapefile ("xxx")

# define x,y,z for pies
x <- c(-100, 100)
y <- c(50, -50)
z1 <- c(0.25, 0.25, 0.5)
z2 <- c(0.5, 0.2, 0.3)
z <- rbind(z1,z2)
# define radii of the pies
r <- c(5, 10)

# it's easier to have all data in a single df

plot(NA, xlim = xlim, ylim = ylim, cex = 0.75, xlab = NA, ylab = NA)
draw.shape(wmap, col = "grey", border = "NA")
draw.pie(x,y,z,radius = r, col=c("blue", "yellow", "red"))
legend.pie (x = -160, y = -70, labels = c("0", "1", "2"), radius = 5,
bty = "n", cex = 0.5, label.dist=1.5, col = c("blue", "yellow", "red"))

然后可使用legend.bubble添加饼图大小的图例

EN

回答 2

Stack Overflow用户

发布于 2014-02-10 20:55:32

看看this example,你可以使用地图作为背景,用Plotrixggplot2绘制你的玫瑰图。在任何一种情况下,你都会想要在你的地图上叠加多个这样的图表,这在ggplot中很容易做到,只需看一下示例。

票数 2
EN

Stack Overflow用户

发布于 2014-02-13 00:02:28

我在Hmisc包中发现了subplot(),它似乎完全符合我的要求。下面是我的解决方案(背景中没有地图,可以使用mapplots绘制地图)。我对如何改进这一点的建议持开放态度。

代码语言:javascript
复制
library(Hmisc)
library (circular)

dat <- data.frame(replicate(2,sample(0:360,10,rep=TRUE)))
lat <- c(50, -40)
lon <- c(-100, 20)

# convert to class circular
cir.dat <- as.circular (dat, type ='angles', units = 'degrees', template = 'geographic', modulo = 'asis', zero = 'pi/2', rotation = 'clock')

# function for subplot, plots relative frequencies, see rose.diag for how to adjust the plot
sub.rose <- function(x){
                nu <- sum(!is.na(x))
                de <- max(hist(x, breaks = (seq(0, 360, 30)), plot = FALSE)$counts)
                prop <- nu/de
                rose.diag(x, bins = 12, ticks = FALSE, axes = FALSE,
                radii.scale = 'linear',
                border = NA, 
                prop = prop,
                col = 'black'
                )
                }

plot(NA, xlim = xlim, ylim = ylim)
for(i in 1:length(lat)){
    subplot(sub.rose(cir.dat[,i]), x = lon[i], y = lat[i], size = c(1, 1))
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21676721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档