gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:35:12 29 4
gpt4 key购买 nike

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com