You can find the code that retrieves the data using the specialized cancensus package here and here. You may consider this post to be a continuation of Part 6 of the Working with Statistics Canada Data in R series. To illustrate this, I will be using the ‘education’ dataset that contains education levels of people aged 25 to 64, broken down by gender, according to 2016 Canadian Census. Lets’ start with a more complex use case – making multiple plots on the same subject. Making Multiple Plots on the Same Subject states, would you copy and paste the same chunk of code 50 times?įortunately, there is a much better way – simply write a function that will iteratively run the code as many times as you need. Of course, you can simply duplicate your code (with necessary changes), but this is tedious and not optimal, putting it mildly. Yet another example of a repetitive plotting task is when you’d like to use your own custom plot theme for your plots.īoth use cases – making multiple plots on the same subject, and using the same theme for multiple plots – require the same R code to run over and over again. Same goes for a plot with all 50 states on its X axis. states, a plot made up of 50 facets would be virtually unreadable. by city or region).īut what if the data is too complex to fit into a single plot? Or maybe there are just too many levels in your grouping variable – for example, if you try to plot family income data for all 50 U.S. Another option is to create a faceted plot, broken down by whatever grouping variable you choose (e.g.Often it works just fine, especially if the data is simple and can easily fit into the plot. You can try to fit all the data into the same plot.the same economic indicator) for several states, provinces, or cities. For example, you’d like to plot the same kind of data (e.g. There are often situations when you need to perform repetitive plotting tasks. Writing Functions to Generate Multiple Plots.
0 Comments
Leave a Reply. |