Search code examples
rggplot2lattice

How do I change x-axis values in a grouped lattice xyplot


I have following sample data in R.

d <- read.table(text = 
"Grp     Var    Col1  Col2    Col3
grp_1   8      46.8  50.0   50.6
grp_1   16     95.6  47.4   48.0
grp_1   24     45.1  45.6   46.4
grp_1   32     68.8  44.3   58.2
grp_1   40     44.6  52.2   44.3
grp_1   48     86.5  42.2   68.6
grp_2   40     63.2  95.6   63.0
grp_2   60     66.7  67.5   65.6
grp_2   80     69.6  70.7   67.9
grp_2   100    71.9  73.4   69.3
grp_2   120    73.8  75.7   48.0
grp_3   500    51.9  50.0   50.5
grp_3   1000   65.5  53.0   53.4
grp_3   5000   61.2  99.0   59.9
grp_3   10000  80.1  63.0   62.8
grp_3   30000  25.9  33.8   14.2
  ", header=T
)

The columns Col1, Col2, and Col3, for each group, need to be plotted against Var. Group 1 has 6 values each for Var, Col1 - Col3, While groups 2 and 3 have 5 values. I am using Lattice's xyplot() function to plot them in a grid. The code is below:

#Groups as factors
d$Grp <- factor(d$Grp, levels=c("grp_1","grp_2","grp_3"), order=TRUE)
#Plot the data
xyplot(
  Col1 + Col2 + Col3 ~ Var | Grp,
  data=d,
  superpose=T,
  as.table=TRUE,
  col=c("#cc0000","#008000", "#0073e6"),
  lwd=1.5, ylim = c(0,120),
  ylab = list(label=expression("My Values"), fontsize=15),
  xlab = list(label="Var",fontsize=15),
  key=list(
    text  = list(c("Col1", "Col2", "Col3")),
    lines = list( lwd=1.5, col=c("#cc0000","#008000", "#0073e6")),
    corner = c(1, 0),
    x = 0.90, 
    y = 0.17),
  par.settings = list(strip.background=list(col="white")),
  scales=list(cex=1.2),
  type=c("l","g")
)

The plot looks like this:

Code output

Clearly, the group 1 and 2 plotted values look messy because x-axis values are 500,1000,5000,10000,30000 instead of 8,16,24,32,40,48 and 40,60,80,100,120. Is there any way to fix x-axis values for group 1 and 2 using xyplot() (preferably)? I am also open for other cool suggestions. The expected output could for example be like this:

Desired output


Solution

  • You use the scales option with relation="free", I get a plot similar to @Tjebo's above.. not 3 identical plots:

    xyplot(
      Col1 + Col2 + Col3 ~ Var | Grp,
      data=d,
      superpose=T,
      as.table=TRUE,
      col=c("#cc0000","#008000", "#0073e6"),
      lwd=1.5, ylim = c(0,120),
      ylab = list(label=expression("My Values"), fontsize=15),
      xlab = list(label="Var",fontsize=15),
      key=list(
        text  = list(c("Col1", "Col2", "Col3")),
        lines = list( lwd=1.5, col=c("#cc0000","#008000", "#0073e6")),
        corner = c(1, 0),
        x = 0.90, 
        y = 0.17),
      par.settings = list(strip.background=list(col="white")),
      scales=list(cex=1.2,x=list(relation="free")),
      type=c("l","g")
    )
    

    enter image description here