您好我正在尝试从2个几乎完全相同的数据集创建一堆图表。因此,我使用相同的列标题或受控变量对数据帧进行标准化。问题是当我然后调用末尾的图以导出到PDF(或只是可视化)时:一些生成相同的图形,一些因为调用不应该的变量名而失败(指的是应该只在第二个循环中的值)但是在第一个),或似乎工作正常......
我认为我需要的是一种将全文分配给变量的方法,而不是对另一个变量的引用。这样,绘图变量不会随着其他变量的变化而变化。
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