如何在 x 轴上获得正确的顺序?

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

我对 x 轴上的比例顺序有疑问。

我的输入文件在合并和匹配两个数据帧后顺序正确。

#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")

      

如果你能帮我把 position_aa 在 x 轴上的正确顺序,我将不胜感激:

-10 -9 -8 ... 14

而不是: -1 -10 -2...

按照建议我添加了res1$position_aa <- as.numeric(res1$position_aa)

更改后,我的情节如下所示:

奇怪的是,包似乎以某种方式将值强制转换回字符,无论我们做什么。

您可以改为尝试基于 R 的绘图。首先,我们需要一个 "factor" 具有数字有序级别的变量。

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

然后我们使用 plot,它使用 graphics:::plot.factor 方法,按我们给出的级别充分排序。之后我们添加 points 并使用 abline 制作网格。就是这样。

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)