Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - put ggplot grid lines in foreground [duplicate]

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!

like image 796
Florian R. Klein Avatar asked Sep 27 '13 15:09

Florian R. Klein


1 Answers

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))
...
like image 199
Florian R. Klein Avatar answered Oct 23 '22 17:10

Florian R. Klein