Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R “中出现意外的数值常量”;ggplot(_R_Plot_Ggplot2_Time Series_Stacked Area Chart - Fatal编程技术网

R “中出现意外的数值常量”;ggplot(

R “中出现意外的数值常量”;ggplot(,r,plot,ggplot2,time-series,stacked-area-chart,R,Plot,Ggplot2,Time Series,Stacked Area Chart,我试图描绘大学申请人年龄的趋势。从各种数据库中,我使用这些数据构建以下数据框架: > 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] "25

我试图描绘大学申请人年龄的趋势。从各种数据库中,我使用这些数据构建以下数据框架:

> 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
  a    h      i      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

然后收到一张图表,通常看起来还可以(虽然我试图定制颜色,但失败了,尽管你看不到,因为我没有足够的声誉点数),但是…只绘制了5个年龄组,而不是11个年龄组

当我尝试使用以下工具分别绘制它们时:

ggplot(AGEgroups,aes(x=Year, y=NumerOfApplicants, fill=Age.Range)) +
  geom_area(data = AGEgroups, aes(x=Year, y=l, fill="21 yrs"))
大多数都很好,但当我绘图时:

ggplot(AGEgroups,aes(x=Year, y=NumerOfApplicants, fill=Age.Range)) +
  geom_area(data = AGEgroups, aes(x=Year, y=m, fill="22 yrs"))
哪个组丢失,我收到错误消息:

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"
我一直在看这两行代码,在语法上看不出任何区别。“m”向量会显示在命令上。你知道为什么会发生这种情况吗

今天重新启动计算机后,我没有遇到意外的数字常量错误,这意味着旧的“打开/关闭”技术至少解决了50%的问题;)

尽管如此,图表显示的是5个变量,而不是11个变量。建议的dput(头(年龄组))产生以下输出:

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")

我仍然无法运行上面的代码,因为它缺少所有单字母变量,我不想手动定义这些变量,因此无法重现错误

但绘制数据的更好方法是先将其融化

library(reshape2)
mm<-melt(AGEgroups, id.vars="Year")
产生


在这里,我们使用更标准的赋值,而不是依赖于美学中使用假想变量的副作用,来清楚地标记情节。这使代码的意图更加清晰。

这其中很多都没有任何意义。比如,当这些变量似乎没有分配到任何地方时,为什么要指定
y=numberofapplications
fill=Age.Range
。当您在geom_区域命令中使用
y=m
时,即使您重命名了所有列。你到底期望
fill=“22年”做什么?这不是一个变量名。但我想你只看到5的原因是它们相互覆盖,因为你把它们作为单独的层来绘制。你真的需要融化你的数据。也许有
dput(head(AGEgroups))`使这个例子重现。变量相互覆盖的建议似乎是合理的。我会调查的。我把dput的结果包括在帖子里。这些荒谬的事情没有意义,但它们对我有用:1。不知何故,y=numberOfAppenders将标题分配给y轴。我现在把它改为“申请人数”。它的标准公式会被我的RStudio忽略,如果我不包括它,“h”字母会显示在那里。2.如果我写y=22,这是变量名,它会被视为一个数字,你可以在y轴上看到22。使用“m”可以很好地工作。3.f=“22年”在图例栏上为每种颜色命名。谢谢!现在可以了。只要我有15点声望,我会投票给你的答案!
library(reshape2)
mm<-melt(AGEgroups, id.vars="Year")
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 to 29", "30 to 39", "40+"))