0

Hello I am trying to create a bunch of graphs from 2 data sets that are nearly identical. So I standardized the dataframes with the same column headings or controlled variables. Problem is when I then call the plots at the end to export to PDF (or just visualize): some generate identical graphs, some fail due to calling a variable name that should not (referring to a value that should only be in the 2nd loop but is the in the first), or seem to work right...

What I think I need is a way to assign full text to a variable, not a reference to another variable. This way the plot variables do not change as the other variables change.

for (x in seq(0,1)) {
     if (x==0) {
       nm=quote(Graduation)
       snm="grad"
       df.year=quote(TDSF.by_grad_year)
       df.bracket=quote(TDSF.by_grad_bracket)
     } else if (x==1) {
       nm=quote(Initiation)
       snm="init"
       df.year=quote(TDSF.by_init_year)
       df.bracket=quote(TDSF.by_init_bracket)
     }

  title=paste("Number of Donors by", nm, "Year")
  assign(paste(snm,"1",sep=""),
         ggplot(data=TDSF, aes(x=eval(nm))) + 
           geom_histogram(color="red",binwidth = 1,boundary=-.01) +
           scale_x_continuous(breaks=year_levels) +
           scale_y_continuous(breaks=number_donor_levels) +
           labs(title=title,x=paste(snm,"Year"),y="Count"),
         envir = .GlobalEnv)

  title=paste("Percent of", nm, "Year that Donated")
  assign(paste(snm,"2",sep=""),
         ggplot(data=eval(df.year), aes(x=eval(nm),weights=Percent_Donated)) + 
           geom_bar(color="red")+
           scale_x_continuous(breaks=year_levels) +
           scale_y_continuous(breaks=percent_levels) +
           labs(title=title,x=paste(snm,"Year"),y="Percent (%)"),
         envir = .GlobalEnv)

  title=paste("Number of Donors by", nm, "Year (5 Year Blocks)")
  assign(paste(snm,"3",sep=""),
         ggplot(data=TDSF, aes(x=eval(nm))) + 
           geom_histogram(color="red",binwidth = 5,boundary=-.01)+
           scale_x_continuous(breaks=year_levels) +
           scale_y_continuous(breaks=number_donor_levels) +
           labs(title=title,x=paste(snm,"Year"),y="Count"),
         envir = .GlobalEnv)

  title=paste("Percent of", nm, "Year that Donated (5 Year Blocks)")
  assign(paste(snm,"4",sep=""),
         ggplot(data=eval(df.bracket), aes(x=Year_Bracket,weights=Percent_Donated)) + 
           geom_bar(color="red")+
           scale_y_continuous(breaks=percent_levels) +
           labs(title=title,x=paste(snm,"Year"),y="Percent (%)") +
           theme(axis.text.x = element_text(angle=315,hjust=0)),
         envir = .GlobalEnv)

  title=paste("Number of Donors by", nm, "Year (5 Year Blocks)")
  assign(paste(snm,"5",sep=""),
         ggplot(eval(df.year), aes(x = factor(1), fill = Year_Bracket, y=n)) +
           geom_bar(width = 1,stat="identity") + 
           guides(fill=guide_legend(ncol=2)) +
           scale_y_continuous(breaks=seq(0,10000,10)) +
           coord_polar(theta = "y")+
           labs(title=title,x=paste(snm,"Year"),y="Count"),
         envir = .GlobalEnv)

  title=paste("Amount of Donations ($) by", nm, "Year")
  assign(paste(snm,"6",sep=""),
         ggplot(data=eval(df.year), aes(x=eval(nm),weights=Donation)) + 
           geom_bar(color="red") +
           scale_x_continuous(breaks=seq(1920,2100,10)) +
           scale_y_continuous(breaks=donation_size_levels) +
           labs(title=title,x=paste(snm,"Year"),y="Donation Amount ($)"),
         envir = .GlobalEnv)

  title=paste("Amount of Donations ($) by", nm, "Year (5 Year Blocks)")
  assign(paste(snm,"7",sep=""),
         ggplot(data=eval(df.bracket), aes(x=Year_Bracket,weights=Donation)) + 
           geom_bar(color="red") +
           scale_y_continuous(breaks=donation_size_levels) +
           labs(title=title,x=paste(snm,"Year"),y="Donation Amount ($)") +
           theme(axis.text.x = element_text(angle=315,hjust=0)),
         envir = .GlobalEnv)

  title=paste("Amount of Donations ($) by", nm, "Year (5 Year Blocks)")
  assign(paste(snm,"8",sep=""),
         ggplot(eval(df.bracket), aes(x = factor(1), fill = Year_Bracket, y=Donation)) +
           geom_bar(width = 1,stat="identity") + 
           guides(fill=guide_legend(ncol=2)) +
           scale_y_continuous(breaks=seq(0,10E6,25E3)) +
           coord_polar(theta = "y")+
           labs(title=title,x=paste(snm,"Year"),y="Count"),
         envir = .GlobalEnv)
}
#year calculations

pdf(file=paste("TDSF by ",nm," year.pdf"),paper="US",width=8,height=10.5)
grid.arrange(mapdonations,mapnumber,ncol=1)
grid.arrange(grad1,grad2,ncol=1)
grid.arrange(grad3,grad4,grad5,ncol=1)
grid.arrange(grad6,grad7,grad8,ncol=1)
grid.arrange(init1,init2,ncol=1)
grid.arrange(init3,init4,init5,ncol=1)
grid.arrange(init6,init7,init8,ncol=1)
dev.off()


grid.arrange(grad1,init1)
grid.arrange(grad2,init2)
grid.arrange(grad3,init3)
grid.arrange(grad4,init4)
grid.arrange(grad5,init5)
grid.arrange(grad6,init6)
grid.arrange(grad7,init7)
grid.arrange(grad8,init8)







head(TDSF)
             Name Graduation Initiation Graduation.Size Initiation.Size Chapter State Donation X284224.3075
1 Actives 1997-98         NA         NA              NA              NA                  150.0           NA
2          Ahrano         NA         NA              NA              NA                   17.5           NA
3   Ann S. Clough         NA         NA              NA              NA                  500.0           NA
4   Arleen French         NA         NA              NA              NA                   50.0           NA
5      Blackstone         NA         NA              NA              NA                   25.0           NA
6            Cody         NA         NA              NA              NA                 1017.5           NA

head(TDSF.by_grad_year)
# A tibble: 6 x 6
  Graduation Donation     n Graduation.Size Year_Bracket Percent_Donated
       <dbl>    <dbl> <int>           <dbl>       <fctr>           <dbl>
1       1941     17.5     1              10  (1940,1945]       10.000000
2       1949     17.5     1               1  (1945,1950]      100.000000
3       1950    700.0     1              15  (1950,1955]        6.666667
4       1952  13500.0     2              13  (1950,1955]       15.384615
5       1953   3225.0     3              21  (1950,1955]       14.285714
6       1954   1355.0     2              18  (1950,1955]       11.111111

head(TDSF.by_grad_bracket)
# A tibble: 6 x 5
  Year_Bracket     n Graduation.Size Donation Percent_Donated
        <fctr> <int>           <dbl>    <dbl>           <dbl>
1  (1940,1945]     1              10    17.50        10.00000
2  (1945,1950]     1               1    17.50       100.00000
3  (1950,1955]     8              67 18780.00        11.94030
4  (1955,1960]     6              43 63292.50        13.95349
5  (1960,1965]    10              69 24045.00        14.49275
6  (1965,1970]    11              93 21168.07        11.82796

head(TDSF.by_init_year)
# A tibble: 6 x 6
  Initiation Donation     n Initiation.Size Year_Bracket Percent_Donated
       <dbl>    <dbl> <int>           <dbl>       <fctr>           <dbl>
1       1938     17.5     1              10  (1935,1940]       10.000000
2       1946     17.5     1               3  (1945,1950]       33.333333
3       1947    700.0     1              11  (1945,1950]        9.090909
4       1948  12500.0     1               9  (1945,1950]       11.111111
5       1949   1000.0     1              10  (1945,1950]       10.000000
6       1950   3225.0     3              15  (1950,1955]       20.000000

head(TDSF.by_init_bracket)
# A tibble: 6 x 5
  Year_Bracket     n Initiation.Size Donation Percent_Donated
        <fctr> <int>           <dbl>    <dbl>           <dbl>
1  (1935,1940]     1              10    17.50        10.00000
2  (1945,1950]     4              33 14217.50        12.12121
3  (1950,1955]     8              54  7630.00        14.81481
4  (1955,1960]     7              62 66442.50        11.29032
5  (1960,1965]     8              59 18012.50        13.55932
6  (1965,1970]    16             146 22528.07        10.95890
Roland
  • 127,288
  • 10
  • 191
  • 288
atclaus
  • 1,046
  • 1
  • 9
  • 12
  • 4
    Don't use `assign(paste())`, use a `list`. – Gregor Thomas Aug 17 '16 at 20:03
  • 1
    When asking a question, you should provide a [reproduicble example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample data so we can run the code in R. Also, try to make the example as minimal as possible to isolate your problem. Basically you should learn to avoid `assign()` and embrace named lists. That's the "R way." – MrFlick Aug 17 '16 at 20:03
  • You can see [How to make a list of data frames?](http://stackoverflow.com/a/24376207/903061) - some of the points won't apply but for the most part it is analogous. – Gregor Thomas Aug 17 '16 at 20:04
  • 2
    Also, 2 data frames with the same structure is a great use case for `ggplot`'s `%+%` command. Make a plot with one data frame and then just `%+% other_data` to swap out the data. – Gregor Thomas Aug 17 '16 at 20:06
  • @Roland how did you change the formatting? So I can do it right next time.... – atclaus Aug 19 '16 at 20:03
  • Use the `{}` button for code formatting. You've used a feature for JavaScript/HTML/CSS code. You can see my changes by comparing the revisions: http://stackoverflow.com/posts/39005283/revisions – Roland Aug 19 '16 at 20:47
  • @Gregor I followed your advice on making a list. Thanks. However it led to a new question so I posted it here http://stackoverflow.com/questions/39049876/r-assigning-plot-to-variably-named-list – atclaus Aug 20 '16 at 02:25

0 Answers0