The reason why the following piece of code did not correctly plot the data was due to the column header name c(300, 400, 500, 600, 700)
which was name of the first column in the data frame df
.
x4 <- melt(df, id=names(df)[1], measure=names(df)[c(2, 5, 6, 8)], variable = "cols")
plt <- ggplot(x4) +
geom_line(aes(x=x,y= value, color=cols), size=1) +
labs(x = "x", y = "y")
So the column header name of df
had to be changed first, according to the suggestion by Brandon Bertelsen, the column header name of c(300, 400, 500, 600, 700)
was changed to x
by the following line of code,
colnames(df)[1] <- "x"
Once the column header name is changed, then the data frame can be melted after which the plotting works correctly,
x4 <- melt(df, id=names(df)[1], measure=names(df)[c(2, 5, 6, 8)], variable = "cols")
plt <- ggplot(x4) +
geom_point(aes(x=x,y= value, color=cols), size=2) +
labs(x = "x", y = "y")
So the plot plt
looks like this,