wwl wwl - 1 year ago 93
R Question

automatically titling figures and maps in R

I'm using R to generate a map of percentage of voters that voted Democrat and Republican for each election since 1948. So I have a map of the US (broken down into states), and I want to generate two maps for each election (one for each party).

I'm using the GIStools and rgdal library, and my tentative code is like this:

variablelist = c("Dem1948", "Rep1948", ..., "Dem2012", "Rep2012")

for (variable in variablelist) {
shades <- auto.shading(USA$variable, n = 5, cols = brewer.pal(5, "Blues"))
choropleth(USA, USA,variable, shades)
choro.legend(-91, 27, shades, fmt = "%g", title = "Voting Percentage")

The problem is my title (see the last line) is very generic for every map. How can I automatically make the title for "Dem1948" to be "Percentage that voted for Democrats in 1948", and so on?

Answer Source

You could add the following to your code:

party_use <- ifelse(grepl("Dem",variable),"Democrats","Republicans")
year_use <- substr(variable,4,7)

choro.legend(-91, 27, shades, fmt = "%g", title = paste0("Percentage that voted for ", party_use," in ", year_use))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download