Grid badly displayed using ggplot2
Asked Answered
T

2

8

I am trying to plot celestial object on the sky (basically with coordinates equivalent to latitude/longitude). I successfully plotted all my points using the "aitoff" projection of the coord_map function, but in this case, the grid is badly displayed, i.e. residual horizontal lines are still displayed for latitudes non equal to zero along with their correct projections.

enter image description here

How could I remove these lines?

Here is code that reproduces the behavior:

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("","","","","","","","",""))
Traweek answered 22/5, 2012 at 17:1 Comment(0)
M
4

Definitely this is a bug in ggplot2 so could you please file this bug? https://github.com/hadley/ggplot2/issues?state=open Filed as a bug.

Here is a quick and dirty hack.

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

Note that this may work only with the aitoff projection.

Mitinger answered 23/5, 2012 at 2:1 Comment(0)
A
2

You just need to add:

+ opts(axis.ticks = theme_blank())
Ashbey answered 22/5, 2012 at 22:8 Comment(1)
Hmm. That gets rid of the ticks at the bottom of the figure, but not the extra (straight) horizontal lines that the OP wants to remove.Jellify

© 2022 - 2024 — McMap. All rights reserved.