如何停止 ggplot 交换我的样本顺序?

How to stop ggplot swapping the order of my samples?

我的基因顺序正在调换。理想情况下,我希望 mRNA(C 或 R)出现在 miRNA 之前,但如图 1(R:miRNA1 表达)所示,在某些情况下顺序被交换了。下面我添加了分别用于创建图 1 和图 2 的数据框和代码。

df1 <- structure(list(R = c(-1.14250840373484, -1.02133830125996, 0.553311226987293, 
                             0.649580480526957, 0.960954997480556),
                     miRNA1 = c(1.39724430765698, 0.649024751983948, -0.36832537544742, 
                             -1.04276601543279, -0.635177668760717),
                    Time = c(1L, 3L, 6L, 10L, 14L)), 
                    class = "data.frame", row.names = c(NA, 5L))

df2 <- structure(list(C = c(-1.34260625841716, -0.587744704056962, 0.134415861797786,
                            0.565397609657598, 1.23053749101874),
                      miRNA2 = c(-1.10103340686835,-1.02891995290153, 1.09002231659813, 
                                  0.510685717630627, 0.529245325541117), 
                     Time = c(1L, 3L, 6L, 10L, 14L)), 
                     class = "data.frame", row.names = c(NA, 5L))




X <- as.data.frame(df1) # To create Figure1
x <- as.data.frame(df2) # To create Figure2
rownames(X) <- X$Time
X$Time <- NULL
Melted <- melt(as.matrix(X), varnames = c("Time", "Gene"))
colnames(Melted)[3] <- "Expression"
miR <- as.character(unique(Melted$Gene)[[1]])
mRNA <- as.character(unique(Melted$Gene)[[2]])
ggplot(Melted, aes(x = Time, y = Expression, group = Gene, color = Gene)) +
    geom_line(data = ~subset(., Gene == paste(mRNA)), size = 2) +
    geom_line(data = ~subset(., Gene == paste(miR)), size = 2) + 
    scale_colour_manual(values = c("orange", "purple")) + 
    theme_classic() + 
    labs(title = paste0(miR, ":", mRNA, " Expression"), x = "Time", y = "Expression", 
    subtitle = paste0("")) + 
    theme(plot.title = element_text(size = 20, face = "bold",
                                    hjust = 0.5), 
          axis.text.x = element_text(size = 15), 
          axis.text.y = element_text(size = 15),
          axis.title.x = element_text(size = 20),
          axis.title.y = element_text(size = 20),
          legend.text = element_text(size = 12)) + 
    theme(plot.subtitle = element_text(size = 25, hjust = 1.2, face = "italic",
                                       color = "black"))

您可以使用像

这样不那么混乱的代码
library(tidyverse)

df1 <- structure(list(R = c(-1.14250840373484, -1.02133830125996, 0.553311226987293, 
                            0.649580480526957, 0.960954997480556),
                      miRNA1 = c(1.39724430765698, 0.649024751983948, -0.36832537544742, 
                                 -1.04276601543279, -0.635177668760717),
                      Time = c(1L, 3L, 6L, 10L, 14L)), 
                 class = "data.frame", row.names = c(NA, 5L))

df2 <- structure(list(C = c(-1.34260625841716, -0.587744704056962, 0.134415861797786,
                            0.565397609657598, 1.23053749101874),
                      miRNA2 = c(-1.10103340686835,-1.02891995290153, 1.09002231659813, 
                                 0.510685717630627, 0.529245325541117), 
                      Time = c(1L, 3L, 6L, 10L, 14L)), 
                 class = "data.frame", row.names = c(NA, 5L))

df1 %>% 
  pivot_longer(-Time, values_to = "Expression", names_to = "Gene") %>% 
  mutate(Gene =  factor(Gene, levels = c("miRNA1", "R"))) %>% 
  ggplot(aes(x = Time, y = Expression, group = Gene, color = Gene)) +
  geom_line(size = 2) +
  scale_colour_manual(values = c("orange", "purple")) + 
  theme_classic() + 
  labs(title = "R:mRNA1 Expression", x = "Time", y = "Expression", 
       subtitle = paste0("")) + 
  theme(plot.title = element_text(size = 20, face = "bold",
                                  hjust = 0.5), 
        axis.text.x = element_text(size = 15), 
        axis.text.y = element_text(size = 15),
        axis.title.x = element_text(size = 20),
        axis.title.y = element_text(size = 20),
        legend.text = element_text(size = 12)) + 
  theme(plot.subtitle = element_text(size = 25, hjust = 1.2, face = "italic",
                                     color = "black"))
df2 %>% 
  pivot_longer(-Time, values_to = "Expression", names_to = "Gene") %>% 
  mutate(Gene =  factor(Gene, levels = c("miRNA2", "C"))) %>% 
  ggplot(aes(x = Time, y = Expression, group = Gene, color = Gene)) +
  geom_line(size = 2) +
  scale_colour_manual(values = c("orange", "purple")) + 
  theme_classic() + 
  labs(title = "C:mRNA2 Expression", x = "Time", y = "Expression", 
       subtitle = paste0("")) + 
  theme(plot.title = element_text(size = 20, face = "bold",
                                  hjust = 0.5), 
        axis.text.x = element_text(size = 15), 
        axis.text.y = element_text(size = 15),
        axis.title.x = element_text(size = 20),
        axis.title.y = element_text(size = 20),
        legend.text = element_text(size = 12)) + 
  theme(plot.subtitle = element_text(size = 25, hjust = 1.2, face = "italic",
                                     color = "black"))

在绘制对象之前,您可以将 Gene 定义为一个因子,并通过包含 Melted$Gene <- factor(Melted$Gene, levels = c("R", "miRNA1")).

来定义级别
X <- as.data.frame(df1) # To create Figure1
x <- as.data.frame(df2) # To create Figure2
rownames(X) <- X$Time
X$Time <- NULL
Melted <- melt(as.matrix(X), varnames = c("Time", "Gene"))
Melted$Gene <- factor(Melted$Gene, levels = c("R", "miRNA1")) # define order/levels
colnames(Melted)[3] <- "Expression"
miR <- as.character(unique(Melted$Gene)[[1]])
mRNA <- as.character(unique(Melted$Gene)[[2]])

然后,通过简化 geom_line(),它会相应地对它们进行排序。

ggplot(Melted, aes(x = Time, y = Expression, group = Gene, color = Gene)) +
  geom_line(size = 2) + # simplified
  scale_colour_manual(values = c("orange", "purple")) + 
  theme_classic() + 
  labs(title = paste0(miR, ":", mRNA, " Expression"), x = "Time", y = "Expression", 
       subtitle = paste0("")) + 
  theme(plot.title = element_text(size = 20, face = "bold",
                                  hjust = 0.5), 
        axis.text.x = element_text(size = 15), 
        axis.text.y = element_text(size = 15),
        axis.title.x = element_text(size = 20),
        axis.title.y = element_text(size = 20),
        legend.text = element_text(size = 12)) + 
  theme(plot.subtitle = element_text(size = 25, hjust = 1.2, face = "italic",
                                     color = "black"))

输出:

但是请注意,我不确定您如何获得 x 轴上的时间 1:5,因为在您发布的数据中它是 1、3、6、10、14。