问题
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