R: Sorting a data frame by an alphanumeric

ぃ、小莉子 提交于 2021-01-28 11:09:47

问题


I have a data frame which stores a count value for each model. Model name is an alphanumeric. Then I generate a bar plot using ggplot2 having the models in the x axis and the count in the y axis. I want to order my x axis. The x axis appears as follows in the data frame and in the x axis in the plot. I want to sort it properly for example, M_1, M_2, M_3, M_10, M_11, M_20 etc

Model   Count
M_1 73
M_10    71
M_100   65
M_11    65
M_110   64
M_111   71
M_13    70
M_130   73
M_2 72
M_20    69
M_200   63
M_21    72
M_210   72
M_211   67
M_3 78
M_30    76
M_300   59
M_31    73
M_310   64

I tried using order(), mixedsort(), arrange() to order the dataframe first and factor() in ggplot2. However was not successful.

geneDFColSum[with(geneDFColSum, order(geneDFColSum$Model)), ]

geneDFColSum[with(geneDFColSum, mixedsort(geneDFColSum$Model)), ]

library(dplyr)
  arrange(geneDFColSum, Model)

Is there a way to achieve this? I could separate the model number into a separate column and order by that column. However looking whether there is an easy way.


回答1:


You need to order the levels of the factor, not the rows of the data:

dd$Model = factor(dd$Model, levels = gtools::mixedsort(dd$Model))
ggplot(dd, aes(x = Model, y = Count)) + geom_col()


Using this as input data:

dd = read.table(text = "Model   Count
M_1 73
M_10    71
M_100   65
M_11    65
M_110   64
M_111   71
M_13    70
M_130   73
M_2 72
M_20    69
M_200   63
M_21    72
M_210   72
M_211   67
M_3 78
M_30    76
M_300   59
M_31    73
M_310   64", header = T, stringsAsFactors = FALSE)



回答2:


Here's a solution based on your idea "separate the model number into a separate column and order by that column". You can then use that to reorder the factor levels.

library(tidyverse)

geneDFColSum %>% 
  mutate(Order = as.numeric(gsub("M_", "", Model))) %>% 
  arrange(Order) %>% 
  mutate(Model = factor(Model, levels = Model)) %>%
  ggplot(aes(Model, Count)) + 
    geom_col()



来源:https://stackoverflow.com/questions/49763863/r-sorting-a-data-frame-by-an-alphanumeric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!