How can I keep the x values in the order I set for my vector?

26 views Asked by At

I want to create a stripchart of very simple experimental data: Six samples with three observations per sample. Here is the code I'm using.


pro<-c("Q","Q","Q",
         "E","E","E",
         "M","M","M",
         "F","F","F",
         "G","G","G",
         "P","P","P"
); as.factor(pro)

ges<-c(1000,
       1111,
       2222,
       1142,
       1111,
       1112,
       400,
       441,
       544,
       250,
       200,
       150,
       101,
       102,
       103,
       800,
       900,
       950
)

an <-data.frame(
  pro,
  rep=c(1,2,3,
        1,2,3,
        1,2,3,
        1,2,3,
        1,2,3,
        1,2,3,
        1,2,3,
        1,2,3,
        1,2,3,
        1,2,3,
        1,2,3,
        1,2,3),
  ges
);an; str(an)

stripchart(
  ges~pro,data=an,vert=T,
  pch=16,col="red",
  ylab="y axis", xlab="x axis",
  tck=.01,
  )

**I am 100 % happy with the stripchart in general. **The only thing I want to change is the order. R keeps ordering my x values alphabetically, which is nonsense in this case because my experiment belongs in the order q-e-m-f-g-p. I've found similar topics, but all of them are so overly specific or use different packages like ggplot. There must be a simple way to force the original order of the vector?! It'd be great if someone could help me with this.

Many thanks in advance!

1

There are 1 answers

0
Siobhan Schenk On

From what I can tell from your question, you can change the pro variable in your an dataframe to a factor, for which you can specify the order you want.

I added it right before your plot code and got the x-axes labels to be in the order specified in your question.

    ## new code to order x-axis values
    an$pro = factor(an$pro, levels=c("Q", "E", "M", "F", "G", "P"))
    
    ## plot code from OP question
    stripchart(
      ges~pro,data=an,vert=T,
      pch=16,col="red",
      ylab="y axis", xlab="x axis",
      tck=.01,
    )