2016-11-16 61 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