How can I keep my x axis values in the correct order? (Not the default alphabetical order)

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!

Hi @mrax. One way to achieve this is to specify the factor as an ordered factor, with the levels listed in the desired order.

pro<-c("Q","Q","Q",
       "E","E","E",
       "M","M","M",
       "F","F","F",
       "G","G","G",
       "P","P","P"
); pro = factor(pro, levels = unique(pro), ordered = T)

1 Like

Thank you very much!! This helped me a lot.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.