Yang Yang Yang Yang - 3 months ago 20
R Question

Plotting error with ggplot2

I have data with three variables and I am now trying to plot two of them using ggplot2.

library(ggplot2)
x=1:50
y=x^2
z=rep(c("p","sp","n","sn","nt"),each=10)
mydata=as.data.frame(cbind(x,y,z))
ggplot(mydata, aes(x=x, y=y)) + geom_point()


According to the data, the plot should be a curve. However, when I run the code, I get the following figure which makes no sense. Is there any mistake in the code? Thanks a lot.
enter image description here

Answer

When you create a matrix using cbind it coerces to the most common type, in this case, character. When you call as.data.frame.matrix It will by default create factors, giving you this unusual ordering based on the increasing levels of the factor. The correct function to use is data.frame with several arguments of the columns of your data.

library(ggplot2)
x=1:50
y=x^2
z=rep(c("p","sp","n","sn","nt"),each=10)
mydata= data.frame(x,y,z)
ggplot(mydata, aes(x=x, y=y)) + geom_line()

Have a look at str(data.frame(x,y,z)) instead of str(as.data.frame(cbind(x,y,z)) and note the coercion to factor from integer and numeric modes.

Comments