如何保持data.frame中的x轴顺序(对于常规r脚本)?

这是我的数据的一个示例:

structure(list(VARIETE = structure(c(3L,5L,4L,1L,2L,6L),.Label = c("id4","id5","id1","id3","id2","id6"),class = "factor"),MOY_AJUST_GENE = c(115.4217669,118.7343702,116.8029088,113.1666208,114.3314785,125.3140321
),`19` = c(115.5875947,117.9590553,110.2799894,115.1775659,125.3140321),`18` = c(115.2559391,119.5096851,NA,116.0532521,113.9523044,NA),`17` = c(NA,115.8286885,`15` = c(NA,113.3820091,`14` = c(NA,116.8935901,`13` = c(NA,113.1634867,`12` = c(NA,111.9227046,c1 = c(NA,114.9076441),c2 = c(NA,111.9647996,127.0981296),grp = structure(1:6,.Label = c("1","2","3","4","5","6"),Z55 = c(7.5,6.5,7,5,6,7),CLASSE = structure(c(3L,3L,3L),.Label = c("","BP","BPS"),`PROT (GPD)` = c(2L,7L,MOSA = structure(c(2L,2L),"S"),SEPTO = c(4L,6L,5L),RJ = c(6L,9L,8L,8L)),row.names = c(NA,-6L),class = "data.frame")

这是ggplot2代码,我用它来创建一个看起来像表格的绘图:

fig2 <- DTA_ecart %>%
  gather(catalogue,value,-VARIETE,-(MOY_AJUST_GENE:grp),na.rm = TRUE) %>% # put the wide table in long shape and remove a part of the columns between "MOY_AJUST_GENE" and "grp" that I use in another chart.
  ggplot(aes(x = catalogue,y = VARIETE,na.rm = TRUE)) + 
  geom_text(aes(label = paste(value)),size = 2,vjust = 0.5,hjust = 0.5) +
  scale_x_discrete(position = "top")

我的数据框DTA_ecart确实很大,我在(fig1)之前将它用于另一个图表。

问题是:如何保持数据框(DTA_ecart)中的x轴顺序正常?

这时,x轴按字母顺序绘制。我知道我们必须对ggplot2说要对订单使用特定的列,而我在上一张图表中使用的是DTA_ecart[,1] <- factor(DTA_ecart[,1],levels = DTA_ecart[,1][order(DTA_ecart[,2],decreasing = FALSE)])。现在,我不知道如何在第二张图表(fig2)上为x轴平移。

编辑:以下是两个正确答案,但出于我的目的,我不想指定列的“名称”或“数字”(由于常规限制)。有什么办法可以改变这种情况吗?

谢谢!

pangdianjun 回答:如何保持data.frame中的x轴顺序(对于常规r脚本)?

如果要更改原始数据框中的顺序,也可以直接从列顺序中获取:

在名为grp的列之后获取该列:

column_after_grp=grep("^grp$",names(DTA_ecart))+1
fig2 <- 
  DTA_ecart %>%
  # put the wide table in long shape and remove a part of the columns 
  # between "MOY_AJUST_GENE" and "grp" that I use in another chart.
  gather(catalogue,value,-VARIETE,-(MOY_AJUST_GENE:grp),na.rm = TRUE) %>% 
  ggplot(aes(x = catalogue,y = VARIETE,na.rm = TRUE)) + 
  geom_text(aes(label = paste(value)),size = 2,vjust = 0.5,hjust = 0.5) +
  # setting the limits here helps towards your goal
  scale_x_discrete(position = "top",limits = names(DTA_ecart)[column_after_grp:ncol(DTA_ecart)])
,

除了将变量转换为因子外,还可以使用scale_x_discrete(limits = [...])指定轴的顺序。因此,您可以在此处指定映射到x轴的列的唯一值(如它们显示的那样)。


DTA_ecart <- structure(list(VARIETE = structure(c(3L,5L,4L,1L,2L,6L),.Label = c("id4","id5","id1","id3","id2","id6"),class = "factor"),MOY_AJUST_GENE = c(115.4217669,118.7343702,116.8029088,113.1666208,114.3314785,125.3140321
  ),`19` = c(115.5875947,117.9590553,110.2799894,115.1775659,125.3140321),`18` = c(115.2559391,119.5096851,NA,116.0532521,113.9523044,NA),`17` = c(NA,115.8286885,`15` = c(NA,113.3820091,`14` = c(NA,116.8935901,`13` = c(NA,113.1634867,`12` = c(NA,111.9227046,c1 = c(NA,114.9076441),c2 = c(NA,111.9647996,127.0981296),grp = structure(1:6,.Label = c("1","2","3","4","5","6"),Z55 = c(7.5,6.5,7,5,6,7),CLASSE = structure(c(3L,3L,3L),.Label = c("","BP","BPS"),`PROT (GPD)` = c(2L,7L,MOSA = structure(c(2L,2L),"S"),SEPTO = c(4L,6L,5L),RJ = c(6L,9L,8L,8L)),row.names = c(NA,-6L),class = "data.frame")


library("dplyr")
library("tidyr")
library("ggplot2")


df <- DTA_ecart %>%
  gather(catalogue,na.rm = TRUE) 
#> Warning: attributes are not identical across measure variables;
#> they will be dropped

fig2 <- ggplot(df,aes(x = catalogue,hjust = 0.5) +
  scale_x_discrete(position = "top",limits  = unique(df[["catalogue"]])
    )

fig2

reprex package(v0.3.0)于2019-11-14创建

您还可以将其转换为函数,以将其应用于不同的列,例如:

foo <- function(df,x_column,y_column) 
  ggplot(df,aes_string(x = x_column y = y_column,na.rm = TRUE)) + 
    scale_x_discrete(limits = unique(df[[x_column]]))`
,

如评论中所述,我们需要定义因素,例如:

library(ggplot2)
library(dplyr)

plotDat <- DTA_ecart %>%
  gather(catalogue,na.rm = TRUE) %>% 
  mutate(catalogue = factor(catalogue,# set it manually
                            levels = c("Z55","CLASSE","PROT (GPD)","MOSA","SEPTO","RJ")
                            # or get it from column names order:
                            #levels = colnames(DTA_ecart)[13:18]
  ))

ggplot(plotDat,hjust = 0.5) +
  scale_x_discrete(position = "top")

注意:我使用的是中间数据-plotDat,可以更轻松地调试和检查我们实际传递给 ggplot 的内容,可以跳过并通过管道直接传递给 ggplot

enter image description here

本文链接:https://www.f2er.com/3111321.html

大家都在问