r - unexpected numeric constant in: "ggplot( -
this question has answer here:
i trying plot trends in age of university applicants. various databases use data build following dataframe:
> agegroup <- c("year", "17","18","19","20", "21", "22", "23", "24", "25to29", "30to39", "40plus"); agegroup [1] "year" "17" "18" "19" "20" "21" "22" "23" "24" [10] "25to29" "30to39" "40plus" > agegroups <- as.data.frame(cbind(a,h,i,j, k, l, m, n, o, p, q, r)); agegroups h j k l m n o p q r 1 2004 1053 160450 74600 25778 14317 9761 6995 5589 15902 17171 8351 2 2005 1115 175406 77751 28368 15191 10551 7778 6107 18153 18695 9686 ... 9 2012 743 199213 93669 37214 21240 14651 10962 8781 26387 27246 15308 10 2013 702 201821 103356 39185 21557 15242 11226 8707 27326 26887 15442 > colnames(agegroups) <- agegroup > agegroups year 17 18 19 20 21 22 23 24 25to29 30to39 40plus 1 2004 1053 160450 74600 25778 14317 9761 6995 5589 15902 17171 8351 ... 10 2013 702 201821 103356 39185 21557 15242 11226 8707 27326 26887 15442 then plot graph using ggplot2 library:
> ggplot(agegroups,aes(x=year, y=numerofapplicants, fill=age.range)) + + geom_area(data = agegroups, aes(x=year, y=h, fill="17 yrs"))+ + geom_area(data = agegroups, aes(x=year, y=i, fill="18 yrs"))+ + geom_area(data = agegroups, aes(x=year, y=j, fill="19 yrs"))+ ...
and receive graph, looks ok (though tried customise colours , failed , though cannot see not have enough reputation points), but... 5 age groups plotted instead of 11...
when try plot them separately using:
ggplot(agegroups,aes(x=year, y=numerofapplicants, fill=age.range)) + geom_area(data = agegroups, aes(x=year, y=l, fill="21 yrs")) the majority work fine, when plot:
ggplot(agegroups,aes(x=year, y=numerofapplicants, fill=age.range)) + geom_area(data = agegroups, aes(x=year, y=m, fill="22 yrs")) which missing group, error message:
error: unexpected numeric constant in: "ggplot(agegroups,aes(x=year, y=numerofapplicants, fill=age.range)) + geom_area(data = agegroups, aes(x=year, y=m, fill="22" i have been looking @ both code lines , can see no difference in syntax. 'm' vector gets displayed on command. ideas why might happening?
i not unexpected numeric constant error today after restarting computer, means old "switch on/off" technique solves @ least 50% of problems;)
still, graph displays 5 instead of 11 variables. suggested dput(head(agegroups)) yields following output:
structure(list(year = 2004:2009, `17` = c(1053l, 1115l, 937l, 1023l, 1273l, 1236l), `18` = c(160450l, 175406l, 173806l, 176306l, 187802l, 197090l), `19` = c(74600l, 77751l, 71285l, 83706l, 89462l, 97544l), `20` = c(25778l, 28368l, 27003l, 29955l, 36255l, 38451l ), `21` = c(14317l, 15191l, 15464l, 16550l, 19745l, 22110l), `22` = c(9761l, 10551l, 10287l, 11498l, 13384l, 15132l), `23` = c(6995l, 7778l, 7664l, 8054l, 9801l, 11080l), `24` = c(5589l, 6107l, 5948l, 6150l, 7470l, 8810l), `25to29` = c(15902l, 18153l, 18001l, 18833l, 23578l, 27299l), `30to39` = c(17171l, 18695l, 17818l, 17861l, 22643l, 26781l), `40plus` = c(8351l, 9686l, 9854l, 10141l, 13183l, 15888l)), .names = c("year", "17", "18", "19", "20", "21", "22", "23", "24", "25to29", "30to39", "40plus"), row.names = c(na, 6l), class = "data.frame")
i still can't code above run because it's missing single-letter variables , don't want define manually can't reproduce error.
but better way plot data melt first.
library(reshape2) mm<-melt(agegroups, id.vars="year") then plot with
ggplot(mm,aes(x=year, y=value, fill=variable)) + geom_area() + ylab("number of applicants") + scale_fill_hue(name = "age range", labels=c(paste(17:24, "yrs"),"25 29", "30 39", "40+")) which produces

here label plot using more standard assignments rather relying on side effects of using imaginary variables in aesthetics. make intention of code clearer.
Comments
Post a Comment