Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

r - How do I set factor levels to the order they appear in a data frame?

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?

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)

School D (22)

School A (25)

School F (25)

School B (28)

School 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One way around this is to change your ggplot call to

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))) + ...

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)))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...