以下大部分代码用于分析印度特定州2009年至2012年所有地区的水化学成分:2012 train是父数据Quality.Parameter提供化学品信息
state_1 <- subset(train,train$State.Name=="ANDHRA PRADESH")
ANDHRA <- as.data.frame(table(state_1$District.Name,state_1$Quality.Parameter,state_1$Year),stringsAsFactors = FALSE)
str(ANDHRA)
names(ANDHRA) <- c("District.Name","CHEMICAL","YEAR","Freq")
an <- ggplot(ANDHRA,aes(ANDHRA$CHEMICAL,ANDHRA$Freq,fill=ANDHRA$District.Name))
an+geom_bar(stat="identity",position = "dodge")+facet_grid(.~ANDHRA$YEAR)+
labs(title="TREND of Chemical Compostion in Andhra Pradesh Villages",x="Chemicals",y="Number Of Cases",fill="Districts in Andhra Pradesh")
我有大约27个印度州,并希望分析代码图中水中的化学物质。
table(train$State.Name)
ANDHRA PRADESH ARUNACHAL PRADESH ASSAM BIHAR CHATTISGARH CHHATTISGARH
2888 612 79910 92336 25062 8815
GUJARAT HARYANA HIMACHAL PRADESH JAMMU AND KASHMIR JHARKHAND KARNATAKA
2092 262 88 67 3913 30824
KERALA MADHYA PRADESH MAHARASHTRA MANIPUR MEGHALAYA NAGALAND
4800 14449 12480 14 427 618
ORISSA PUDUCHERRY PUNJAB RAJASTHAN TAMIL NADU TRIPURA
68620 17 1056 131417 3164 26235
UTTAR PRADESH UTTARAKHAND WEST BENGAL
9918 57 30101
我没有为所有不同的状态反复编写代码,而是创建了用户定义的函数
district_analysis<-function(a,b,d) {
b<- subset(train,train$State.Name=="a")
d<- as.data.frame(table(b$District.Name,b$Quality.Parameter,b$Year),stringsAsFactors = FALSE)
names(d) <- c("District.Name","CHEMICAL","YEAR","Freq")
an <- ggplot(d,aes(d$CHEMICAL,d$Freq,fill=d$District.Name))
an+geom_bar(stat="identity",position = "dodge")+facet_grid(.~d$YEAR)+labs(title="TREND of Chemical Compostion in" a "Villages",x="Chemicals",y="Number Of Cases",fill="Districts in" a)
return(an) }
但是在尝试运行该功能时,我得到的错误就是这个
> district_analysis<-function(a,b,d) {
+ b<- subset(train,train$State.Name=="a")
+ d<- as.data.frame(table(b$District.Name,b$Quality.Parameter,b$Year),stringsAsFactors = FALSE)
+ names(d) <- c("District.Name","CHEMICAL","YEAR","Freq")
+ an <- ggplot(d,aes(d$CHEMICAL,d$Freq,fill=d$District.Name))
+ an+geom_bar(stat="identity",position = "dodge")+facet_grid(.~d$YEAR)+labs(title="TREND of Chemical Compostion in" a "Villages",x="Chemicals",y="Number Of Cases",fill="Districts in" a)
Error: unexpected symbol in:
" an <- ggplot(d,aes(d$CHEMICAL,d$Freq,fill=d$District.Name))
an+geom_bar(stat="identity",position = "dodge")+facet_grid(.~d$YEAR)+labs(title="TREND of Chemical Compostion in" a"
> return(an) }
Error: unexpected '}' in " return(an) }"
我无法找出原因,请建议我编写代码。
答案 0 :(得分:0)
你忘了粘贴字符串。
+labs(title="TREND of Chemical Compostion in" a "Villages"
不起作用,应该是:
+labs(title=paste0("TREND of Chemical Compostion in ", a, " Villages")
使其成为单个字符串。对于fill参数也是如此,我实际上甚至不确定你是否可以使用这种方式的参数,它不应该在你的labs()函数中。
希望这有帮助。