为什么我不能在子集之后绘制线?

时间:2014-09-21 02:29:43

标签: r plot ggplot2

我正在对文件中的数据进行子集化,然后尝试在ggplot2中绘制一条线。只设法获得积分(虽然我使用的是geom_point()+ geom_line())

d1<-structure(list(year = structure(1:10, .Label = c("2001", "2002", 
"2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", 
"2011", "2012"), class = "factor"), val1 = c(42244L, 43161L, 
42444L, 43579L, 43424L, 45116L, 48003L, 48835L, 47856L, 50024L
), val2 = c(0L, 0L, 0L, 0L, 18L, 0L, 0L, 7L, 0L, 0L), val3 = c(109467L, 
112956L, 110623L, 125657L, 127560L, 137180L, 156412L, 164861L, 
174395L, 180413L), val4 = c(20381L, 18346L, 16636L, 18119L, 17173L, 
19234L, 22113L, 22624L, 23374L, 23280L), val5 = c(7056L, 6679L, 
6287L, 6261L, 7197L, 7581L, 10321L, 10535L, 10242L, 12080L), 
    val6 = c(12823L, 12056L, 11101L, 11428L, 12665L, 11783L, 
    9861L, 8250L, 7802L, 6775L), val7 = c(220L, 101L, 55L, 68L, 
    212L, 85L, 95L, 125L, 49L, 81L), val8 = c(694L, 2527L, 1066L, 
    1700L, 2976L, 1665L, 1229L, 1086L, 879L, 958L), val9 = c(12439L, 
    12698L, 15351L, 12771L, 13192L, 12420L, 13753L, 14943L, 14368L, 
    10404L), val10 = c(17819L, 18221L, 15643L, 19250L, 19326L, 
    20967L, 23658L, 27208L, 30526L, 34250L), val11 = c(20446L, 
    21236L, 19994L, 22489L, 23212L, 23792L, 25363L, 25036L, 25845L, 
    27074L), val12 = c(243589L, 247981L, 239200L, 261322L, 266955L, 
    279823L, 310808L, 323510L, 335336L, 345339L)), .Names = c("year", 
"val1", "val2", "val3", "val4", "val5", "val6", "val7", "val8", 
"val9", "val10", "val11", "val12"), 
row.names = c(NA, 10L), class = "data.frame")

然后我跑

d2<-subset(d1[,c(1,2)]) #(here d1 is the main (csv)file)
ggplot(d2,aes(x=year,y=val1))+geom_line()+geom_point()
# geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

同样的事情对于qplot,当使用geom =&#34; line&#34;时,它显示相同的通知,但没有使用geom =&#34; line&#34;,它显示没有任何注释/错误的点

qplot(y=val1,x=year,data=d2,geom="line")
# geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

现在我手动创建数据框

d2<-data.frame(year=c(2001,2002,2003,2004,2005,2006,
2007,2008,2009,2010,2011,2012),
value=c(20446,21236,19994,22489,23212,23792,25363,
25036,25845,27074,28878,31117))

我可以绘制线条。无法弄清楚出了什么问题。 感谢

1 个答案:

答案 0 :(得分:1)

无论出于何种原因,您将自己的年份作为csv中的因素,并且在您的&#34;手动创建&#34;一。因子用于分类变量,这些变量往往具有与连续变量不同的绘图规则。

你可以做到

ggplot(d2,aes(x=as.numeric(as.character(year)),y=val1))+geom_line()+geom_point()

将年份转换回数字,但最好弄清楚为什么它首先导入R作为一个因素。你有可能有糟糕的数据。