【问题标题】:How do I make the x axis start at a fixed point rather than start at 0?如何使 x 轴从固定点开始而不是从 0 开始?
【发布时间】: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


【解决方案1】:

为了提供一个小例子(评论太长),请考虑以下几点:

首先,您的数据:

df <- structure(list(Year = structure(1:13, .Label = 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"), class = "factor"), 
    Litres = structure(c(5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
    1L, 2L, 3L, 4L), .Label = c("101,951", "105,595", "109,156", 
    "116,682", "67,703", "69,408", "71,657", "75,590", "79,674", 
    "80,866", "85,015", "90,946", "95,782"), class = "factor"), 
    PerCapita = c(9.34, 9.38, 9.47, 9.79, 10.1, 10, 10.29, 10.78, 
    11.12, 11.58, 11.59, 11.58, 12.15), yr = 1:13), .Names = c("Year", 
"Litres", "PerCapita", "yr"), row.names = c(NA, -13L), class = "data.frame")

现在让我们看看str(df)

str(df)
'data.frame':   13 obs. of  4 variables:
 $ Year     : Factor w/ 13 levels "1960-61","1961-62",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Litres   : Factor w/ 13 levels "101,951","105,595",..: 5 6 7 8 9 10 11 12 13 1 ...
 $ PerCapita: num  9.34 9.38 9.47 9.79 10.1 ...

我们看到升和年都是因子。我要绘制Litres;但请注意,它使用逗号作为小数点。我改变这个使用:

df$Litres <- as.numeric(gsub(",", "\\.", as.character(df$Litres)))

我们看到Year 并不是真正的一年,而是存储为字符的两年范围。当 R 读取诸如“1960-61”之类的字符时,它不明白这是什么意思。我们可以在这里做很多重新格式化,或者更简单的事情:假设没有重复的时间间隔,我创建一个从 1 到数据帧结尾的简单序列:

df$yr <- 1:nrow(df)

这给了我:

head(df, 3)
     Year Litres PerCapita yr
1 1960-61 67.703      9.34  1
2 1961-62 69.408      9.38  2
3 1962-63 71.657      9.47  3

现在,我用这个来绘制你的一个变量,抑制 x 轴:

plot(df$yr, df$Litres, xaxt='n')

要获得带有适当标签(年份范围)的 x 轴,我们调用 axis

axis(1, at = df$yr, labels = df$Year)

这给了你:

这里发生的情况是,我们创建了一个带有从 1 到 n 的隐式 x 轴标签的图,但我们告诉 R 为 x 轴刻度使用不同的标签。

如果您的年份是实际年份而不是奇怪的范围,则以下是另一种选择:

# first, create the years:
df$yr <- substr(df$Year, 1,4)
# this gives us:
head(df)
     Year Litres PerCapita   yr
1 1960-61 67.703      9.34 1960
2 1961-62 69.408      9.38 1961
3 1962-63 71.657      9.47 1962
4 1963-64 75.590      9.79 1963
5 1964-65 79.674     10.10 1964
6 1965-66 80.866     10.00 1965

# now convert it to date, specifying the format:
df$yr_date <- as.Date(df$yr, format = "%Y")

head(df)
     Year Litres PerCapita   yr    yr_date
1 1960-61 67.703      9.34 1960 1960-08-21
2 1961-62 69.408      9.38 1961 1961-08-21
3 1962-63 71.657      9.47 1962 1962-08-21

现在可以绘制了:

plot(df$yr_date, df$Litres)

或者,这些年来,你也可以这样做

df$yr_num <- as.numeric(df$yr)
plot(df$yr_num, df$Litres)

【讨论】:

    【解决方案2】:

    您对 x 的定义如下:

    > 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)
    

    这是一个方程向量:1960-61 = 1899, 1961-62=1899, ..., 1999-2000 = -1, 2000-01 = 1999, ... 2013-14 = 1999。

    因此,您的绘图在 x = -1 处有一个点,在 x = 1899 处有一堆点,在 x = 1999 处有另一个点。

    改用以下方法?这不是最优化的代码,但它与您已有的代码很接近,因此应该很容易理解:

    # Year & Litres should be based on your 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多