Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 按数据框中显示的降序打印数据_R_Ggplot2 - Fatal编程技术网

R 按数据框中显示的降序打印数据

R 按数据框中显示的降序打印数据,r,ggplot2,R,Ggplot2,我一直在努力在ggplot2中以条形图的形式订购和绘制一个简单的数据框 我希望按显示的方式绘制数据,以便相应类别(例如“人类”、“男性”)的值(“计数”变量)从高到低绘制 我在这个网站上也关注过其他帖子,问过类似的问题,但都没法做到 ## Dataset (mesh2) #Category Count #Humans 62 #Male 40 #Female 38 #Adult

我一直在努力在ggplot2中以条形图的形式订购和绘制一个简单的数据框

我希望按显示的方式绘制数据,以便相应类别(例如“人类”、“男性”)的值(“计数”变量)从高到低绘制

我在这个网站上也关注过其他帖子,问过类似的问题,但都没法做到

## Dataset (mesh2)

#Category                   Count

#Humans             62

#Male               40

#Female             38

#Adult              37

#Middle Aged            30

#Liver/anatomy & histology          29

#Organ Size                 29

#Adolescent                 28

#Child              21

#Liver/radiography*             20

#Liver Transplantation*     20

#Tomography, X-Ray Computed         20

#Body Weight            18

#Child, Preschool               18

#Living Donors*         18

#Infant             16

#Aged               14

#Body Surface Area              14

#Regression Analysis        11

#Hepatectomy            10

## read in data (mesh2) as object (mesh2)

mesh2 <- read.csv("mesh2.csv", header = T)

## order data by count of mesh variable

mesh2$cat2 <- order(mesh2$Category, mesh2$Count, decreasing=TRUE)

## Barplot created in ggplot2

library(ggplot2)

mesh2p <- ggplot(mesh2, aes(x=cat2, y=Count)) + geom_bar (stat="identity") +     scale_x_continuous(breaks=c(1:20), labels=c("Humans", "Male", "Female", "Adult", "MAged",   "Liver anat & hist", "Organ Size", "Adolescent",   "Child", "Liver radiog", "Liver Transplnt", "Tomog X-Ray Computed", "Body Weight", "Child Preschool", "Living Donors", "Infant", "Aged", "BSA", "Regression Analysis", "Hepatectomy"))+ theme (axis.text.x=element_text(angle=45, hjust=1))
##数据集(mesh2)
#类别计数
#人类62
#男40
#女38
#成人37
#30岁的中年人
#肝脏/解剖学和组织学29
#器官大小29
#青少年28
#儿童21
#肝脏/放射照相*20
#肝移植*20
#X射线计算机体层摄影术20
#体重18
#儿童,学龄前18
#活体捐赠者*18
#婴儿16
#14岁
#体表面积14
#回归分析11
#肝切除术10
##作为对象(mesh2)读入数据(mesh2)
mesh2您需要
重新排序()
。下面是一个使用虚拟数据的示例

set.seed(42)
df <- data.frame(Category = sample(LETTERS), Count = rpois(26, 6))

require("ggplot2")

p1 <- ggplot(df, aes(x = Category, y = Count)) +
         geom_bar(stat = "identity")

p2 <- ggplot(df, aes(x = reorder(Category, -Count), y = Count)) +
         geom_bar(stat = "identity")

require("gridExtra")
grid.arrange(arrangeGrob(p1, p2))
set.seed(42)

df使您的
类别
成为有序因素。请参阅
?factor
,了解如何做到这一点。@Roland不,这不是一般的做法。集合
c(“人类”、“男性”、“女性”、“猫”)中的隐含顺序是什么?有序因子适用于水平本身传递一些定量信息的数据,例如集合
c(“湿”、“湿”、“干”)
。如果在R(多项式约束)模型中使用无序数据,那么将这些数据存储为有序因子将是错误的。我们需要的是
reorder()
函数。s/your/you's/fingers…嗨,加文。非常感谢。我刚刚登录,花了最后一个小时试图找到一个解决方案,也想出了你提出的解决方案!尽管如此,您关于水平是否传递定量信息以及适当使用因子或重排序函数的解释确实很有帮助。再次感谢。在这种情况下,如果每个类别有多个填充,我无法理解为什么这不起作用。我认为您的代码不会使用负值,请参见,在这种情况下,打印后的打印将不会按降序进行。这里也要编码dat.m@LéoLéopoldertz준영 这是不对的;使用我的示例中的代码将
df
替换为
df@Mel-ah-ok来检查它;您希望在facet因子的级别内调整x因子的顺序,但这不是factors所允许的。回想一下,我们正在对整个因子变量的级别进行排序。这可能是它自己的问题