Adding a background to your ggplot
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I really enjoy using the DW-NOMINATE data for examples, as I do here. Sometimes it’s useful to indicate regions in the background of a plot — perhaps two-dimensional regions of interest, perhaps one-dimensional periods in time. It’s not always obvious how to combine data from two data.frames to form one plot in ggplot2, so here is another example.
The trick seems to be that the “second” data frame needs to include all of the same variables as you are using from the “first” data frame (in name, at least — that is, if you are plotting variables called “x”, “y”, and “z” from the “first” data frame, your second data.frame needs to include variables names ”x”, “y”, and “z,” even if you’re not plotting with those, and even if they are assigned equal to some arbitrary constant, as in df2$z <- 1).
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.