2016-11-16 63 views
0

我在绘制每年对三个位置数据的平均值。我经常使用plot()函数,从来没有像这样的问题。出于某种原因,每次我绘制这些数据时,都会为第一个位置数据添加一个类似步骤的样式。我试图将“type =”更改为所有可能的选项,并且似乎忽略它。我也尝试设置type =“n”,然后用points()添加数据,第一组数据的阶梯样式仍然存在。R:给数据绘图功能添加步骤功能

这是我所使用的数据集:

OrganicsRemoval <- data.frame(Year = c("1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", 
            "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", 
            "2015", "2016"), 
          x = c(22,28,20,30,34,31,33,45,42,43,38,50,47,50,50,47,46,44,48,55,57,50), 
          y = c(18,23,25,16,23,24,24,31,36,39,36,42,39,40,42,46,40,42,40,42,44,42), 
          z = c(15,21,22,16,36,33,31,39,38,39,39,46,42,46,45,43,43,44,42,44,45,41)) 

这里是我用来绘制数据的代码:

par(mfrow = c(1,1)) 
plot(x = OrganicsRemoval$Year, y = OrganicsRemoval$x, type = "n", main = "TOC Percent Removal", 
ylab = "TOC Percent Removal", xlab = "Year", ylim = c(0,65)) 
points(x = OrganicsRemoval$Year, y = OrganicsRemoval$x, type = "b", col = "red") 
points(x = OrganicsRemoval$Year, y = OrganicsRemoval$z, type = "b", col = "blue") 
points(x = OrganicsRemoval$Year, y = OrganicsRemoval$y, type = "b", col = "black") 
legend("topright", legend = c("x", "z", "y"), col = c("red", "blue", "black"), lwd = 1) 

这里是输出: Output Plot

我将不胜感激任何帮助,我可以摆脱这些步骤式格式。谢谢!

回答

1

当将数字OrganicsRemoval$Year作为数字时,该图看起来正确。

当创建数据帧而不使用stringsAsFactors = FALSE字符串变成因子。我认为这造成了麻烦。 当不以数字形式投射时,“阶梯状”事物已经出现在初始情节声明中。

plot(x = as.numeric(OrganicsRemoval$Year), y = OrganicsRemoval$x, type = "n", main = "TOC Percent Removal", 
ylab = "TOC Percent Removal", xlab = "Year", ylim = c(0,65)) 

points(x = OrganicsRemoval$Year, y = OrganicsRemoval$x, type = "b", col = "red") 
points(x = OrganicsRemoval$Year, y = OrganicsRemoval$z, type = "b", col = "blue") 
points(x = OrganicsRemoval$Year, y = OrganicsRemoval$y, type = "b", col = "black") 
legend("topright", legend = c("x", "z", "y"), col = c("red", "blue", "black"), lwd = 1) 

enter image description here

另外,也可以,如以上所提到的,使用stringsAsFactors = FALSE当创建数据帧。

+1

谢谢,现在它正在工作! – tbradley