Opaque Legend Box
You have to use bg="white" (or any other color, as you already did) and you have to draw the legend after you plot the lines/points/grid/etc.
For example:
plot(...)
lines(...)
grid(...)
legend(...) # legend has to be the last command!