r - How do I make the x axis start at a fixed point rather than start at 0? -
i haven't used r before , have assessment intro stats class. have found data , need plot it. plotting years categories against litres of alcohol consumed population each year. data looks bit this:
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
my problem in trying plot it, it's not coming @ how need to. i'm struggling few things , feel i'm doing things long/hard way. i've done far:
> 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 coercion 2: in min(x) : no non-missing arguments min; returning inf 3: in max(x) : no non-missing arguments max; returning -inf > year <- as.numeric(year) warning message: nas introduced 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)
and came graph starting @ 0, rather in year categories
how fix problem?
you defined x follows:
> 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)
this vector of equations: 1960-61 = 1899, 1961-62=1899, ..., 1999-2000 = -1, 2000-01 = 1999, ... 2013-14 = 1999.
as result, plot has 1 point @ x = -1, bunch of points @ x = 1899, , bunch @ x = 1999.
try following instead? it's not optimal code, it's close have, should easy understand:
# year & litres should based on dataset. no manipulation needed. year <- alcohol_consumption_2013_14$year litres <- alcohol_consumption_2013_14$`litres pure alcohol` barplot(litres, names.arg = year) plot(factor(year), litres)
Comments
Post a Comment