[R] Control of x-axis variable ordering in ggplot

sbihorel Sebastien.Bihorel at cognigencorp.com
Fri Oct 23 03:46:02 CEST 2015


Hi,

Given a certain data.frame, the lattice xyplot function will plot the 
data as.is and join the data point in the order of the data frame. It is 
my (probably flawed) understanding that, using the same data frame, 
ggplot orders the data by increasing order of the x-axis variable. Can 
one control this behavior?

Thanks

Sebastien

Code example

library(lattice)
library(ggplot2)


data <- data.frame(x=rep(1:4,each=25),
                    y=rep(1:25,times=4),
                    g=rep(1:4,each=25))
data$x <- data$x + 0.005*(data$y)^2-0.1*data$y+1

col <- 3:7

xyplot(y~x,data=data,groups=g,type='l',col=col)

ggplot(data, aes(x,y,group=g)) + geom_point(colour=col[data$g]) +
   geom_line(colour=col[data$g])



More information about the R-help mailing list