R - put ggplot grid lines in foreground [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-30 08:09:15

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/19054092/r-put-ggplot-grid-lines-in-foreground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!