组数据条形图R.

时间:2016-08-12 14:25:05

标签: r bar-chart

可能是一个真正简单的问题,但我需要将1975年之前的几年分组作为条形图中的单个条形图,而不是单独显示每一年,如下所示。

所以我需要将1950-1975组合在一个酒吧中,然后每年都有单独的酒吧。只是不确定要使用什么。

到目前为止,这是我的代码:

yearcount <- table(data2$q19yearTrnComp, exclude="")
bp <- barplot(yearcount, main="", xlab="", ylab="", ylim=c(0, 60), las=2, cex.names=0.75)

enter image description here

> dput(data2)

structure(list(q19yearTrnComp = c(2012L, 1994L, 2012L, 2012L, 
2008L, 2013L, 2002L, 2011L, 2012L, 2000L, 2010L, 1996L, 1986L, 
2010L, 1981L, 2014L, 1974L, 2007L, 2000L, 1966L, 1995L, 2000L, 
2002L, 2007L, 2011L, 1999L, 1991L, 2003L, 2001L, 1994L, 2013L, 
1997L, 1998L, 2010L, 2002L, 2001L, 2010L, 2000L, 1972L, 2001L, 
1992L, 1982L, 2012L, 1996L, 2011L, 2010L, NA, 2010L, 2014L, 2003L, 
2010L, 2012L, 1996L, 1996L, 2004L, 2002L, 2012L, 2013L, 2008L, 
1984L, 2006L, 1984L, 1984L, 2014L, 1982L, 1991L, 1984L, 2010L, 
1995L, 2004L, 2008L, 1998L, 1978L, 1997L, 2013L, 2009L, 1999L, 
2006L, 2012L, 1979L, 2012L, 1989L, 2013L, 2012L, 2014L, 2014L, 
2013L, 1989L, 2014L, 1990L, 1995L, 2005L, 1982L, 2004L, 1993L, 
2011L, 2005L, 1982L, 1984L, 2013L, 1961L, 1982L, 1998L, 1990L, 
1982L, 2013L, 1985L, 2001L, 1993L, 2006L, 2006L, 1995L, 1986L, 
1989L, 2008L, 1987L, 2014L, 1988L, 1968L, 2009L, 1990L, 2004L, 
1981L, 2004L, 1986L, 1976L, 2011L, 1978L, 2013L, 1997L, 1970L, 
1986L, 1986L, 2014L, 1987L, 1981L, 1978L, 2013L, 1987L, 2013L, 
2010L, 1998L, 1982L, 2003L, 2010L, 1974L, 1999L, 1978L, 1990L, 
1998L, 1992L, 2013L, 2000L, 1990L, 1981L, 1988L, 1990L, 1978L, 
1976L, 1986L, 1982L, 2011L, 1987L, 1978L, 1998L, 2007L, 2010L, 
1993L, 1983L, 2011L, 2009L, 2009L, 1983L, 2014L, 2011L, 2005L, 
1976L, 2005L, 1989L, 2000L, 2012L, 1973L, 2013L, 2013L, 1983L, 
2003L, 1982L, 2013L, NA, 1979L, 1986L, 2007L, 1995L, 1970L, 1988L, 
1983L, 2012L, 1981L, 2012L, 1999L, 2008L, 2011L, 2002L, 2009L, 
2011L, 1986L, 2004L, 1971L, 1981L, 1984L, 1999L, 1984L, 1983L, 
2005L, 1985L, 2008L, 1982L, 1980L, 1990L, 1996L, 2012L, 1961L, 
2014L, 2011L, 1982L, 1997L, 2007L, 1989L, 1996L, 1994L, 1989L, 
1996L, 2008L, 1999L, 2009L, NA, 2002L, NA, 1983L, 1985L, 1996L, 
1980L, 1981L, 2002L, 1997L, 1997L, NA, 1985L, 1992L, 1989L, 2000L, 
1980L, 2013L, 2009L, 1984L, 1994L, 1978L, 2014L, 1989L, 1997L, 
1982L, 2010L, 2014L, 2004L, 1999L, 2009L, 1980L, NA, 1998L, 2011L, 
1985L, 2013L, 2005L, 1999L, 1986L, 1996L, 1982L, 2001L, 1991L, 
1980L, 1990L, 1999L, 1985L, 1997L, 1994L, 1988L, 2004L, 1980L, 
2011L, 2012L, 2007L, 1990L, 1970L, 2014L, 1991L, 1987L, 1973L, 
2010L, 1988L, 2010L, 2000L, 1984L, 1990L, 1976L, 2003L, 2005L, 
1987L, 2008L, 1994L, 1997L, 1982L, 1987L, 1977L, 1997L, 2011L, 
1990L, 1987L, 2011L, 2008L, 2012L, 2002L, 1977L, 1994L, 2004L, 
1972L, 1988L, 2007L, 2010L, 2001L, 2013L, 2013L, 1983L, 2002L, 
1977L, 2010L, 1977L, 1992L, 1980L, 1989L, 2003L, 1997L, 2002L, 
1983L, 1996L, 2008L, 2014L, 2006L, 1973L, 2013L, 2012L, NA, 2003L, 
2011L, 2011L, 1978L, 1993L, 2011L, 1971L, 2010L, 2013L, 1975L, 
2009L, 1983L, 2013L, 2009L, 2012L, 1992L, 1986L, 2011L, 2001L, 
2005L, 2000L, 1999L, 1982L, 2009L, 1997L, 2014L, 1966L, 2009L, 
2006L, 2012L, 2008L, 1985L, 2013L, 1986L, 2014L, 1990L, 1986L, 
2006L, 2009L, 2011L, 2005L, 1994L, 2007L, 2000L, 2004L, 2012L, 
2007L, 2012L, 2013L, 2014L, 2010L, 2005L, 2006L, 2003L, 2008L, 
2011L, 2013L, 1995L, 1988L, 2014L, 2013L, 1987L, 2011L, 2010L, 
2012L, 2011L, 1992L, 2011L, 1986L, 2005L, 2003L, 2012L, 2014L, 
1982L, 2010L, 1987L, 1977L, 1986L, 1995L, 2010L, 2004L, 2011L, 
1975L, 1976L, 2006L, 1987L, 1995L, 1995L, 2010L, 2011L, 1991L, 
2000L, 1979L, 2011L, 2012L, 2002L, 2009L, 1995L, 2013L, 2014L, 
2003L, 2012L, 2002L, 1986L, 2008L, 1983L, 1971L, 1974L, 1981L, 
2013L, 2004L, 2007L, 2014L, 1986L, 1981L, 2013L, 2002L, 2008L, 
1990L, 1976L, 1969L, 1991L, 2013L, 2010L, 1988L, 2012L, 2013L, 
2010L, 1994L, 2006L, 1984L, 2003L, 2005L, 1993L, 2007L, 2002L, 
1983L, 2008L, 1991L, 1997L, 1993L, 1996L, 1997L, 2009L, 1985L, 
1981L, 2007L, 1973L, 2009L, 1992L, 2009L, 2006L, 1984L, 1992L, 
1991L, 2009L, 1992L, 1986L, 2007L, 2014L, 2009L, 2006L, 1978L, 
1992L, NA, 2011L, 2005L, 1983L, 2007L, 2008L, 1993L, 1976L, 1997L, 
2014L, 1994L, 1954L, 2009L, 1990L, NA, 1978L, 1996L, 1995L, 1988L, 
2012L, 1982L, 2009L, 1986L, 2000L, 2006L, 1999L, 2008L, 1987L, 
1983L, 1991L, 1995L, 2011L, 1980L, 2014L, 1981L, 1983L, 2012L, 
2013L, 1984L, 2004L, 2010L, 2012L, 2001L, 1991L, 1985L, 1976L, 
2009L, 1989L, 2005L, 2013L, 2008L, 2008L, 2013L, 2012L, 1994L, 
1987L, 2001L, 2009L, 2011L, 1979L, 1965L, 2004L, 2005L, 1977L, 
1997L, 2013L, 2013L, 2012L, 2000L, 1978L, 2011L, 2012L, 2005L, 
1970L, 1995L, 1976L, 2011L, 1975L, 2006L, 2004L, 2011L, 1986L, 
1992L, 2006L, 1996L, 2009L, 1987L, 2012L, 1983L, 1998L, 2002L, 
2004L, 2004L, 1977L, 1997L, 2000L, 1986L, 1998L, 2005L, 2013L, 
1988L, 2009L, 1996L, 2012L, 1983L, 2009L, 1983L, 2001L, 1968L, 
2009L, 1987L, 2006L, 1999L, 2012L, 1992L, 2008L, 2014L, 2012L, 
1983L, 1985L, 2013L, 1989L, 2007L, 1982L, 1996L, 2000L, 1981L, 
1982L, 1995L, 2013L, 2013L, 1984L, 1991L, 1984L, 1980L, 1977L, 
1998L, 2010L, 1980L, 1985L, 2012L, 1972L, 2009L, 1999L, 1980L, 
1986L, 1993L, 1979L, 2013L, 1986L, 2003L, 2013L, 2010L, 1972L, 
1979L, 1979L, 2012L, 1980L, NA, 2010L, 1992L, 2003L, 1978L, 2006L, 
NA, 2012L, NA, 1977L, 2011L, 1989L, 2003L, 2000L, 1979L, 2013L, 
1964L, 1982L, 2002L, 1976L, 1999L, 1987L, 1977L, 2007L, 1994L, 
1988L, 2009L, 2014L, 1995L, 1989L, 2012L, 2009L, 2004L, 2013L, 
2007L, 1995L, 2013L, 1996L, 2014L, 1992L, 2008L, NA, 2013L, 2009L, 
1981L, 1990L, 1980L, 2011L, 2013L, 2002L, 1989L, 1983L, 1969L, 
2013L, 1992L, 2007L, 2013L, 2012L, 1963L, 1984L, 1974L, 1973L, 
1983L, 1994L, 2010L, 1986L, 2011L, 2013L, NA, 2014L, 2012L, 1984L, 
2004L, 1974L, 2008L, 1972L, 2010L, 2004L, 1985L, 2012L, 2010L, 
2007L, 1974L, 2012L, 1994L, 1987L, 2012L, 2012L, 2000L, 1999L, 
1993L, 1971L, 2011L, 2011L, 2008L, 2004L, 1994L, 1979L, 2001L, 
1987L, 2010L, 2001L, 1993L, 1980L, 1950L, 2014L, 2014L, 1986L, 
1967L, 1979L, 1998L, 1997L, 1982L, 2013L, 2009L, 2000L, 2014L, 
1999L, 2009L, 1982L, 2003L, 2001L, 2014L, 2007L, 1990L, 2008L, 
1975L, 2013L, 2005L, 2012L, 1989L, NA, NA, NA, NA, NA, 2003L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2006L, 
NA, NA, NA, NA, NA, 2002L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 1999L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2006L, NA, NA, 
NA, 2001L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 1989L, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 1982L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 2010L, NA, 2010L, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 1985L, NA, NA, NA, NA, NA, 2001L, NA, NA, NA, NA, NA, NA, 
2008L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2011L, NA, NA, NA, 
NA, NA, NA, NA, NA, 1981L, NA, NA, NA, NA, NA, NA, NA, NA, NA
)), .Names = "q19yearTrnComp", row.names = c(NA, 989L), class = "data.frame")

欢迎任何想法,谢谢!

2 个答案:

答案 0 :(得分:0)

我会使用hist并设置中断

with(data2[!is.na(data2$q19yearTrnComp), , drop = FALSE], 
     hist(q19yearTrnComp, breaks = c(min(q19yearTrnComp), 1975:max(q19yearTrnComp)))
)

答案 1 :(得分:0)

使用@bouncyball提供的代码变体。

data2$q19yearTrnComp2 <- ifelse((data2$q19yearTrnComp <= 1975 & data2$q19yearTrnComp > 0), "1950 to 1975", data2$q19yearTrnComp)
yearcount <- table(data2$q19yearTrnComp2, exclude=NA)
bp <- barplot(yearcount, main="", xlab="Year", ylab="Graduates, No.", ylim=c(0, 60), las=2, cex.names=0.75)

enter image description here