【发布时间】:2018-01-29 05:56:43
【问题描述】:
我以前没有使用过 R,并且对 stats 课程的介绍进行了评估。我找到了我的数据,我需要绘制它。我将年份作为类别与每年人口消耗的酒精升数进行对比。我的数据看起来有点像这样:
Year Litres Per Capita
1960-61 67,703 9.34
1961-62 69,408 9.38
1962-63 71,657 9.47
1963-64 75,590 9.79
1964-65 79,674 10.10
1965-66 80,866 10.00
1966-67 85,015 10.29
1967-68 90,946 10.78
1968-69 95,782 11.12
1969-70 101,951 11.58
1970-71 105,595 11.59
1971-72 109,156 11.58
1972-73 116,682 12.15
我的问题是,在尝试绘制它时,它根本没有按照我的需要出现。此外,我正在为一些事情苦苦挣扎,感觉自己正在以漫长/艰难的方式做事。这是我到目前为止所做的:
> View(Alcohol_consumption_2013_14)
> Year <- Alcohol_consumption_2013_14$Year
> Litres <- Alcohol_consumption_2013_14$`Litres Pure Alcohol`
> Capita <- Alcohol_consumption_2013_14$`Per Capita Consumption`
> x=c(Year)
> y=c(Litres)
> plot(x,y)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
> Year <- as.numeric(Year)
Warning message:
NAs introduced by coercion
> barplot(Litres,Year)
Error in plot.window(xlim, ylim, log = log, ...) :
need finite 'xlim' values
> x=c(1960-61,1961-62,1962-63,1963-64,1964-65,1965-66,1966-67,1967-68,1968-
69,1969-70,1970-71,1971-72,1972-73,1973-74,1974-75,1975-76,1976-77,1977-
78,1978-79,1979-80,1980-81,1981-82,1982-83,1983-84,1984-85,1985-86,1986-
87,1987-88,1988-89,1989-90,1990-91,1991-92,1992-93,1993-94,1994-95,1995-
96,1996-97,1997-98,1998-99,1999-2000,2000-01,2001-02,2002-03,2003-04,2004-
05,2005-06,2006-07,2007-08,2008-09,2009-10,2010-11,2011-12,2012-13,2013-14)
> plot(x,y)
然后出现了graph starting at 0, rather than in year categories
我该如何解决这个问题?
【问题讨论】:
-
您的 x 变量不是数字,它是一个字符:它包括两个数字和一个连字符。您必须将其转换为数字。
-
@ephy_92 如果以下任何答案解决了您的问题,请考虑通过单击答案开头旁边的向上和向下投票按钮下方的
v符号来接受答案.谢谢。
标签: r plot graph axis-labels categorical-data