Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用ggplot2添加背景栅格?_R_Ggplot2 - Fatal编程技术网

R 如何使用ggplot2添加背景栅格?

R 如何使用ggplot2添加背景栅格?,r,ggplot2,R,Ggplot2,我想在绘图中心添加背景网格,然后隐藏标准网格线。网格的角点存储在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

我想在绘图中心添加背景网格,然后隐藏标准网格线。网格的角点存储在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()
    )
库(ggplot2)

pts不优雅,但这是我想出的又快又脏的东西。不幸的是,我不能在某一点停止这条线,它只是一直延伸到边缘

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)))

您可以手动指定打断:

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())

pts如果您正在为出版物编辑这样的图形,则始终可以另存为eps,然后在Adobe Illustrator中编辑多余的行。这就是我要做的。我实际上希望网格“浮动”在背景中(参见我的示例)。谢谢。这正是我想要的——谢谢。我还发现geom_路径可以工作,但绘制路径上的所有点非常麻烦:顺便说一句,这在当前版本的ggplot2(1.0.0)中不再适用。