How to get the correct order on the x-axis?

Viewed 898

I have a problem with the scale order on the x axis.

Here is the image of boxplot with the wrong x axis

My input file has the correct order after merging and matching two dataframes.

#data
Eisenberg <- data.frame(scale_hyd = c(0.62, 0.29, -0.9, -0.74, 1.19, 0.48, -0.4, 
                                      1.38, -1.5, 1.06, 0.64, -0.78, 0.12, -0.85, 
                                      -2.53, -0.18, -0.05, 1.08, 0.81, 0.26), 
                        aa = c('A','C','D','E','F','G','H','I','K','L','M','N',
                               'P','Q','R','S','T','V','W','Y'))

hydroph <- data.frame(position_aa = c("-10", "-9", "-8", "-7", "-6", "-5", "-4",
                                      "-3", "-2", "-1", "0", "1", "2", "3", "4",
                                      "5", "6", "7", "8", "9", "10", "11", "12",
                                      "13", "14"),
                      aa = c("C","V","Q","W","K","N","A","Y","A","L","C","W","L",
                             "D","C","I","L","S","A","L","V","H","S","E","E")) 



#combining data
res <- merge(Eisenberg, hydroph)

res1 <- res[match(hydroph$position_aa, res$position_aa), ]
view(res1)

#visualisation of data
ggplot(res1, aes(x=position_aa, y=scale_hyd)) +
  geom_boxplot() +
  labs(x="Amino acid position", y="Eisenberg scale", title="Hydrophobicity") +
  theme_bw() +
  theme(legend.position="none") +
     geom_dotplot(binaxis="y", stackdir="center", fill="black")

      

I would be grateful if you could help me put the right order of position_aa on the x axis:

-10 -9 -8 ... 14

instead of: -1 -10 -2...

As suggested I added res1$position_aa <- as.numeric(res1$position_aa)

After the changes, my plot looks like this: boxplot after the changes

1 Answers

Weirdly enough, the package seems to coerce the values back into character somehow, whatever we do.

You could try base R plots instead. First we need a "factor" variable with numerical ordered levels.

res$position_aa <- factor(res$position_aa, levels=-10:14)

Then we use plot which uses graphics:::plot.factor method, adequately sorted by the levels we have given. After that we add the points and make a grid using abline. That's it.

with(res, plot(position_aa, scale_hyd, main="Hydrophobicity",
               xlab="Amino acid position",
               ylab="Eisenberg scale"))
with(res, points(position_aa, scale_hyd, pch=16, cex=1.5))
.col <- rgb(0, 0, 0, .25)  ## alpha .25 for transparency
abline(h=axTicks(2), lty=3, col=.col)
abline(v=seq(-10:14), lty=3, col=.col)

enter image description here

Related