r - 使用 ggplot2 网格显示不良

标签 r map-projections ggplot2

我正在尝试在天空上绘制天体(基本上坐标相当于纬度/经度)。我使用 coord_map 函数的 "aitoff" 投影成功绘制了所有点,但在这种情况下,网格显示效果很差,即仍然显示剩余水平线纬度不等于零及其正确的投影。

enter image description here

如何删除这些行?

下面是重现该行为的代码:

library(ggplot2)
library(mapproj)
sky2 = data.frame(RA=0, Dec=0)
skyplot2 <- qplot(RA,Dec,data=sky2,xlim=c(0,360),ylim=c(-89.999,89.999),
xlab="R.A.(°)", ylab="Decl. (°)",main="Source repartition on the sky")
skyplot2 + coord_map(projection="aitoff",orientation=c(89.999,180,0)) + 
scale_y_continuous(breaks=(-2:2)*30,limits=c(-89.999,89.999)) + 
scale_x_continuous(breaks=(0:8)*45,limits=c(0,360),
                   labels=c("","","","","","","","",""))

最佳答案

这肯定是 ggplot2 中的一个错误,所以您能提交这个错误吗? https://github.com/hadley/ggplot2/issues?state=open Filed as a bug .

这是一个快速而肮脏的技巧。

f <- function(x, y, ...) {
    if (any(is.na(x))) {
    id <- rle(!is.na(x))$length
    id <- rep(seq_along(id), id)
    df <- data.frame(x, y, id)
    df <- df[order(df$id, df$x), ]
  } else if (any(is.na(y))) {
    id <- rle(!is.na(y))$length
    id <- rep(seq_along(id), id)
    df <- data.frame(x, y, id)
  }
  polylineGrob(df$x, df$y, id = df$id, gp = gpar(col = "white"))
}

skyplot2 <- qplot(RA,Dec,data=sky2,xlim=c(0,360),ylim=c(-89.999,89.999),
                  xlab="R.A.(°)", ylab="Decl. (°)",main="Source repartition on the sky")
skyplot2 + coord_map(projection="aitoff",orientation=c(89.999,180,0)) + 
  scale_y_continuous(breaks=(-2:2)*30,limits=c(-89.999,89.999)) + 
  scale_x_continuous(breaks=(0:8)*45,limits=c(0,360),
                     labels=c("","","","","","","","","")) +
                    opts(panel.grid.major = f)

enter image description here

请注意,这可能仅适用于 aitoff 投影。

关于r - 使用 ggplot2 网格显示不良,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10706632/

相关文章:

r - 查找 data.table 中之前出现的较大事件

R:带有 barpot、ggplot 或 plotly 的堆积条形图

java - 简单的 map 投影

r - 使用 geom_bar 按 R 中的标准绘制值的总和

r - 如何在栅格处理中保留栅格数据类型?

r - 如何使用 ggplot2 在世界地图上绘制一个点?

image - 将 d3 地理投影应用于图像

javascript - 如何将 UTM 转换为纬度/经度?

在ggplot2中旋转 strip 文本

r - ggplot2:添加背景层