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

最佳答案

您可以手动指定中断:

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

关于r - 如何使用 ggplot2 添加背景网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4200979/

相关文章:

r - 将 geom_text 的默认 "a"图例更改为标签字符串本身

r - 如何在R中制作3D直方图

重新排序分组变量中的因素,以便可以使用 ggplot2 按顺序绘制

r - 制作箱线图时如何在函数内部的ggplot2中对组使用aes_string

r - 在一个图中对齐两个图例

删除 geom_histogram 的基线颜色

r - 如何查看功能的源代码?

r - 在 R 中创建嵌套序列

r - 如何将两个 tibbles 或 dataframe 中的值粘贴到 R 中的组合显示表中?

r - 以标签为中心对齐直方图的条形