2016-06-20 7 views
2

x軸が1975-2015年のRで、バープロットを作成しようとしています。年は変数data3$q18yearFirstSclLensです。年に何年も観測がない場合でも、x軸にラベルを付けます。

私が年を取得:

> bp <- barplot(yearcount2, main="", xlab="", ylab="", ylim=c(0,100), las=2, cex.names=0.75, xaxt="n") 

enter image description here

しかし、もちろん、私は唯一の観測がある対象の年を取得します:

> yearcount2 <- table(data3$q18yearFirstSclLens) 

> yearcount2 

1976 1982 1983 1984 1985 1986 1987 1989 1991 1992 1993 1994 1995 1997 1998 1999 2000 2001 2002 2003 
1 2 2 2 2 1 2 2 1 1 1 2 2 2 5 7 10 6 3 9 
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 
8 25 18 26 43 52 78 84 88 91 47 1 

はその後、私のbarplotをプロットします。観測がない年も含めて、1975年から2015年までのすべての年にx軸プロットを作成するにはどうすればよいですか?ここで

dput情報です:

> dput(yearcount2) 
structure(c(1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 
2L, 5L, 7L, 10L, 6L, 3L, 9L, 8L, 25L, 18L, 26L, 43L, 52L, 78L, 
84L, 88L, 91L, 47L, 1L), .Dim = 32L, .Dimnames = structure(list(
c("1976", "1982", "1983", "1984", "1985", "1986", "1987", 
"1989", "1991", "1992", "1993", "1994", "1995", "1997", "1998", 
"1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", 
"2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", 
"2015")), .Names = ""), class = "table") 

はあなたの助けをありがとう!

答えて

3

あなたは美しく働い

minyr <- min(names(yearcount2)); maxyr <- max(names(yearcount2)) 
barplot(setNames(yearcount2[as.character(minyr:maxyr)], minyr:maxyr), las=2) 
+0

まあのような何かを行うことができ、私はそのことを考えたことがないでしょう。ありがとうございました! – nchimato

関連する問題