我试图在地图上的特定坐标上绘制玫瑰图/圆形直方图,类似于在地图上绘制饼图,如包mapplots
。
以下是使用mapplots
生成的示例(请参阅下面的代码),我想用玫瑰图替换饼图
包circular
让我绘制玫瑰图,但我无法将其与mapplots
包集成。有关替代包或代码的任何建议吗?
回答制作地图的代码问题。这一切都基于mapplots
包。我为地图下载了一个shapefile(我想从http://www.freegisdata.org/)
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
添加饼图大小的图例答案 0 :(得分:2)
查看this example,您可以使用地图作为背景,使用Plotrix
或ggplot2
绘制玫瑰图。在任何一种情况下,您都希望在地图上叠加多个这些图表,这在ggplot中很容易,只需看一下示例。
答案 1 :(得分:2)
我在包Hmisc
中发现了subplot(),这似乎完全符合我的要求。下面是我的解决方案(背景中没有地图,可以使用mapplots
绘制)。我愿意接受有关如何改善这一点的建议......
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))
}