热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何将因子水平设置为它们在数据框中出现的顺序?-HowdoIsetfactorlevelstotheordertheyappearinadataframe?

IwanttocreateaheatmapusingggplothoweverIwanttoorderthey-axisbythenumberofobserva

I want to create a heat map using ggplot however I want to order the y-axis by the number of observations. I order the dataframe by the column N and add the number of observations to the group name so that it appears in the axis label. When I plot the data it re-orders based on the group name. Is there a way to set factor levels based on the order they appear in the data frame?

我想用ggplot创建一个热图,但我想按观察次数对y轴进行排序。我按N列对数据帧进行排序,并将观察次数添加到组名称中,以使其显示在轴标签中。当我绘制数据时,它会根据组名重新排序。有没有办法根据数据框中出现的顺序设置因子水平?

Some data:

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

school <- c("School A", "SChool B", "School C", "School D", "School E", "School F")
N <- c(25,28,12,22,30,25)
var1 <- c(1,0,1,1,0,1)
var2 <- c(0,0,0,1,0,1)
var3 <- c(0,1,0,1,1,1)

df <- tbl_df (data.frame (school, N, var1, var2, var3))

df <- arrange (df, N) %>%
  gather (variable, value, var1:var3)

df$school <- paste0 (df$school, " (", df$N, ")")

df <- select (df, school, variable, value)

ggplot(df, aes(variable, school)) + geom_tile(aes(fill = value), colour = "white") + 
  scale_fill_gradient(low = "white",high = "steelblue")

Ultimately I want the order of schools to be:

最终我希望学校的顺序是:

School C (12)

C学校(12)

School D (22)

D学校(22)

School A (25)

学校A(25)

School F (25)

F学校(25)

School B (28)

B学校(28)

School E (30)

E学校(30)

As I want to do this for multiple plots I want to find a way to do this automatically and not have to re-set factor levels each time.

由于我想为多个图表执行此操作,我希望找到一种自动执行此操作的方法,而不必每次都重新设置因子级别。

3 个解决方案

#1


4  

One way around this is to change your ggplot call to

解决这个问题的一种方法是将你的ggplot调用更改为

ggplot(df, aes(variable, factor(school, levels = unique(school)))) + ...

To avoid typing this every time, you can create a function

为避免每次都输入此内容,您可以创建一个函数

f <- function(x) factor(x, levels = unique(x))

and then call it by ggplot(df, aes(variable, f(school))) + ...

然后通过ggplot调用它(df,aes(变量,f(学校)))+ ...

Note that this will place the first level of the factor at the bottom of the plot. If you want it at the top, you need to change f to function(x) factor(x, levels = rev(unique(x)))

请注意,这会将因子的第一级放在图的底部。如果你想要它在顶部,你需要将f改为function(x)factor(x,levels = rev(unique(x)))

#2


3  

Add the following forcats pipe to the code just before the call to ggplot().

在调用ggplot()之前,将以下forcats管道添加到代码中。

library(forcats)
df$school <- fct_inorder(df$school) %>% fct_rev()

fct_inorder() creates factor levels in data frame order and fct_rev() reverses them so the plot goes in the right direction.

fct_inorder()以数据帧顺序创建因子级别,fct_rev()将它们反转,以使绘图朝向正确的方向。

#3


0  

One way would be to make the school column and ordered factor:

一种方法是制作学校专栏和有序因素:

df$school <- reorder(df$school, rep(6:1, length.out=length(k)), order=TRUE)

enter image description here


推荐阅读
author-avatar
手机用户2502916627
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有