0

I am not sure the title is worded correctly, I am somewhat new to R. I am using a meaningless task to refresh my memory. I have some United States population data. What I want to do is make a for loop that goes through and plots the population of each state. Here is what I have done so far. Right now I am trying to figure out how to make it print the population as a test.

a <- split(data,list(state)) ##split factor state into individual states

For example str(a$'Alabama'$Population) is numeric of the populations I want to plot.

for(i in 1:53){
b <- noquote((paste(c("a","$", "'",state.list[i],"'", "$","Population"),collapse="")))
}

Now If I print(b) the ouput is a$'Alabama'$Population, however str(print(b)) says it is a character. If I could get it to be a numeric I could move on the next part of my code.

Does this make sense?

Any tips are appreciated. Since this is a learning exercise for me I'd like to know how to solve this problem. However, I am open to better ways of doing this.

dput(head(data, 10))
structure(list(State = structure(c(46L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L), .Label = c("Alabama", "Alaska", "Arizona", "Arkansas", 
"California", "Colorado", "Connecticut", "Delaware", "District of Columbia", 
"Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", 
"Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", 
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", 
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", 
"New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", 
"Oklahoma", "Oregon", "Pennsylvania", "Puerto Rico", "Rhode Island", 
"South Carolina", "South Dakota", "Tennessee", "Texas", "United States", 
"Utah", "Vermont", "Virginia", "Washington", "West Virginia", 
"Wisconsin", "Wyoming"), class = "factor"), Population = c(308.745538, 
4.779736, 0.710231, 6.392017, 2.915918, 37.253956, 5.029196, 
3.574097, 0.897934, 0.601723), Year = structure(c(6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("2,010", "2,011", "2,012", 
"2,013", "2,014", "Census"), class = "factor")), .Names = c("State", 
"Population", "Year"), row.names = c(NA, 10L), class = "data.frame")

link to full dput: http://pastebin.com/Mg16q67m

link to manual plot https://i.stack.imgur.com/A15sm.jpg

  • It sounds strange, maybe it is better if you provide a `dput` of a piece of your data. Furthermore, most likely that loop can be avoided with `lapply`. – SabDeM Sep 03 '15 at 23:24
  • @SabDeM thanks for the quick response. I'll take a look dput and lapply. I'm not familiar with those yet. – Micheal Kelley Sep 03 '15 at 23:27
  • `dput` allows you to have the "structure" of your data. With that you can copy and paste here a piece of your data, that allows us to help you by playing with your data and try to help you. For further question you should read [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It explains how to make a reproducible example. – SabDeM Sep 03 '15 at 23:29
  • Do you want me to post the result of dput(data)? – Micheal Kelley Sep 03 '15 at 23:33
  • Here is the result of dput(data) http://pastebin.com/Mg16q67m – Micheal Kelley Sep 03 '15 at 23:48
  • how are you going to plot the population of each state individually? that doesn't seem to have any efficacy. or did you mean do the plot of each over the years in individual graphs? Also, how did `Census` get in with the years? A link to the original data (not the `dput`) may help folks help you better since it seems malformed. – hrbrmstr Sep 04 '15 at 00:20
  • Here is the data. I was using. http://factfinder.census.gov/faces/tableservices/jsf/pages/productview.xhtml?pid=PEP_2014_PEPANNRES&src=pt To clarify, I wanted to have an individual plot for each of them. I just don't want to manually plot each one. For example, here is one I manually plotted. http://i.imgur.com/VYvcsqr.jpg – Micheal Kelley Sep 04 '15 at 02:48

1 Answers1

3

You may need to do the plots individually if you use base graphics, but either lattice or ggplot2 would make much easier work of this. The following gets rid of the Census values (that's not a "year"), cleans up the comma'd year column and uses ggplot to facet plot each state's population growth.

library(ggplot2)

dat <- subset(dat, Year != "Census")
dat$Year <- sub(",", "", dat$Year)

gg <- ggplot(dat)
gg <- gg + geom_line(aes(x=Year, y=Population, group=State))
gg <- gg + geom_point(aes(x=Year, y=Population, group=State))
gg <- gg + scale_x_discrete(breaks=c("2010", "2012", "2014"))
gg <- gg + facet_wrap(~State, scale="free_y", ncol=5)
gg <- gg + labs(x=NULL, y=NULL, title="Population")
gg <- gg + theme_bw()
gg

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • 2
    Thanks. I was planning on doing the same thing with ggplot. Since this is a learning exercise I wanted to learn it with whatever comes installed in rstudio. This helps a lot and is exactly what I wanted for the end result. Now I need to learn how to do it in shiny. – Micheal Kelley Sep 04 '15 at 02:53
  • How would I plot them individually but as separate files? – Micheal Kelley Sep 04 '15 at 03:45
  • ah, then your initial solution path is the way to go. you'd need a loop to subset the data for each state then use `png`, `jpeg` (etc) or `ggsave` to output each plot to a file. – hrbrmstr Sep 04 '15 at 10:08