如何使用 ggplot2 添加背景网格?

发布于 2024-10-03 13:50:13 字数 496 浏览 0 评论 0原文

我想将背景网格添加到绘图的中心,然后隐藏标准网格线。网格的角点存储在 pts 数据框中,我尝试使用 geom_tile,但它似乎没有使用指定的点。预先感谢您的帮助。

library(ggplot2)  
pts <- data.frame(
        x=c(170,170,170,177.5,177.5,177.5,185,185,185), 
        y=c(-35,-25,-15,-35,-25,-15,-35,-25,-15))  
ggplot(quakes, aes(long, lat)) + 
    geom_point(shape = 1) + 
    geom_tile(data=pts,aes(x=x,y=y),fill="transparent",colour="black") +
    opts(
        panel.grid.major=theme_blank(),
        panel.grid.minor=theme_blank()
    )

I'd like to add background grid to the center of the plot and then hide the standard gridlines. The corner points of the grid are stored in the pts data frame and I've tried using geom_tile, but it doesn't appear to use the specified points. Thanks in advance for your help.

library(ggplot2)  
pts <- data.frame(
        x=c(170,170,170,177.5,177.5,177.5,185,185,185), 
        y=c(-35,-25,-15,-35,-25,-15,-35,-25,-15))  
ggplot(quakes, aes(long, lat)) + 
    geom_point(shape = 1) + 
    geom_tile(data=pts,aes(x=x,y=y),fill="transparent",colour="black") +
    opts(
        panel.grid.major=theme_blank(),
        panel.grid.minor=theme_blank()
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心在旅行 2024-10-10 13:50:13

您可以手动指定中断:

ggplot(quakes, aes(long, lat)) + geom_point(shape = 1) +
  scale_x_continuous(breaks = c(170, 177.5, 185)) +
  scale_y_continuous(breaks = c(-35, -25, -15)) +
  opts(panel.grid.minor = theme_blank(), 
       panel.grid.major = theme_line("black", size = 0.1))

那么,这是您想要的吗?

pts <- data.frame(x=c(170, 170, 170, 170, 177.5, 185), 
                  y=c(-35, -25, -15, -35, -35, -35),
                  xend=c(185, 185, 185, 170, 177.5, 185),
                  yend=c(-35, -25, -15, -15, -15, -15))
ggplot(quakes, aes(long, lat)) + geom_point(shape = 1) + 
   geom_segment(data=pts, aes(x, y, xend=xend, yend=yend)) +
   opts(panel.grid.minor = theme_blank(), 
        panel.grid.major = theme_blank())

you can manually specify the breaks:

ggplot(quakes, aes(long, lat)) + geom_point(shape = 1) +
  scale_x_continuous(breaks = c(170, 177.5, 185)) +
  scale_y_continuous(breaks = c(-35, -25, -15)) +
  opts(panel.grid.minor = theme_blank(), 
       panel.grid.major = theme_line("black", size = 0.1))

then, is this what you want?

pts <- data.frame(x=c(170, 170, 170, 170, 177.5, 185), 
                  y=c(-35, -25, -15, -35, -35, -35),
                  xend=c(185, 185, 185, 170, 177.5, 185),
                  yend=c(-35, -25, -15, -15, -15, -15))
ggplot(quakes, aes(long, lat)) + geom_point(shape = 1) + 
   geom_segment(data=pts, aes(x, y, xend=xend, yend=yend)) +
   opts(panel.grid.minor = theme_blank(), 
        panel.grid.major = theme_blank())
柠檬色的秋千 2024-10-10 13:50:13

不优雅,但这是我想出的快速而肮脏的东西。不幸的是,我无法在某个点停止这条线,它只是一直延伸到边缘。

ggplot(quakes, aes(long, lat)) + geom_point(shape = 1)
 + opts(panel.grid.major=theme_blank(),
        panel.grid.minor=theme_blank())
 + geom_vline(aes(xintercept =seq(165,185,by=5)))
 + geom_hline(aes(yintercept=seq(-35,-15,by=5)))

Not elegant but this is something quick and dirty that I came up with. Unfortunately I can't stop the line at a certain point, it just goes all the way to the edge.

ggplot(quakes, aes(long, lat)) + geom_point(shape = 1)
 + opts(panel.grid.major=theme_blank(),
        panel.grid.minor=theme_blank())
 + geom_vline(aes(xintercept =seq(165,185,by=5)))
 + geom_hline(aes(yintercept=seq(-35,-15,by=5)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文