R - 将ggplot网格线放在前景中 [英] R - put ggplot grid lines in foreground

查看:185
本文介绍了R - 将ggplot网格线放在前景中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法让ggplot在绘制数据前画出网格线。据我所知,通过使用迄今为止运行良好的theme(),我可以轻松地格式化几乎所有东西。但是,我没有找到元素绘制顺序的选项。我可以预先在需要格式化的坐标图(坐标,线条组等)中引入其他线条。听起来更复杂,因为它需要。所以我希望,我可以修改剧情网格来完成这项工作。

一个例子说明我通常是如何绘制加载的shapefile <$ c $的数据框 sf.df c> sf :

 (ggplot(sf.df,aes(x = long,y = lat))
+ geom_polygon(data = sf.df,aes(group = group,fill = NFK))
+ theme(panel.grid.major = element_line(color =black))
+ coord_map(xlim = c(7.08,7.14),ylim = c(50.93,50.96))

预先感谢您!

解决方案

正如@joran建议的那样,使用 geom_hline geom_vline 绝对是一个解决方案。简单定义 yintercept xintercept ,如下所示:

<$ $ x $ b + geom_hline(xintercept = seq(7.08,7.14,by = 0.01))
+ geom_hline(yintercept = seq(50.93,50.96,by = 0.01))
...


I would like to know if there is a way to let ggplot draw its grid lines in front of the plotted data. As far as I know, I can format almost everything easily by using theme() which works well so far. However, I don't find the option in which order the elements are drawn. I could introduce additional lines to the plot which need to be formatted (coordinates, line groups, etc.) beforehand. Sounds more complicated as it need to be. So I hope, I can modify the plot-grid itself to do the job.

An example of how I am usually plotting the data frame sf.df of the loaded shapefile sf:

(ggplot(sf.df, aes(x=long, y=lat))
+ geom_polygon(data=sf.df, aes(group=group, fill=NFK))
+ theme(panel.grid.major=element_line(colour="black"))
+ coord_map(xlim=c(7.08, 7.14), ylim=c(50.93, 50.96))
)

Thank you in advance!

解决方案

As @joran suggested, using geom_hline and geom_vline is defenitely a solution. Simple define the yintercept and xintercept respectively like so:

...
+ geom_vline(xintercept=seq(7.08, 7.14, by=0.01))
+ geom_hline(yintercept=seq(50.93, 50.96, by=0.01))
...

这篇关于R - 将ggplot网格线放在前景中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆