Reputation: 2290
I am trying to plot yearly averages for data that I have for three locations. I have used the plot() function regularly and never had a problem like this before. For some reason, every time I plot this data, it adds a step-like style to the first locations data. I have tried to change "type = " to every possible alternative and it seems to just ignore it. I have also tried to set type = "n" and then add the data with points(), and the step-like style to the first set of data is still there.
Here is the data set that I have used:
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))
Here is the code I am using to plot the data:
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)
Here is what is output: Output Plot
I would greatly appreciate any help I can get to get rid of those step-like format. Thanks!
Upvotes: 0
Views: 375
Reputation: 1817
When casting OrganicsRemoval$Year
as numeric the plot looks correct.
When creating a data frame and not using stringsAsFactors = FALSE
string becomes factor. I assume that this caused the trouble.
The 'step-like' things appear already in the initial plot statement when not casting as numeric.
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)
Alternatively, you can, as mentioned above, use stringsAsFactors = FALSE
when you create the data frame.
Upvotes: 1