我正在尝试绘制我在三个地点的数据的年度平均值。我经常使用plot()函数,之前从未遇到过这样的问题。出于某种原因,每次我绘制这些数据时,它都会为第一个位置数据添加类似步骤的样式。我试图将“type =”更改为每个可能的替代方案,它似乎只是忽略它。我还尝试设置type =“n”,然后使用points()添加数据,并且第一组数据的阶梯式样式仍然存在。
以下是我使用的数据集:
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
我非常感谢能够摆脱这些步骤式格式的任何帮助。谢谢!
答案 0 :(得分: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)
或者,如上所述,您可以在创建数据框时使用stringsAsFactors = FALSE
。