Questions tagged [forcats]

forcats is an R package for working with categorical variables (factors), and part of the tidyverse collection of packages.

forcats is an R package for working with categorical variables (factors) written by Hadley Wickham. It is part of the larger tidyverse collection of packages.

192 questions
28
votes
4 answers

How to reorder factor levels in a tidy way?

Hi I usually use some code like the following to reorder bars in ggplot or other types of plots. Normal plot (unordered) library(tidyverse) iris.tr <-iris %>% group_by(Species) %>% mutate(mSW = mean(Sepal.Width)) %>% select(mSW,Species) %>% …
David Mas
  • 1,149
  • 2
  • 12
  • 18
24
votes
5 answers

R: convert to factor with order of levels same with case_when

When doing data analysis, I sometimes need to recode values to factors in order to carry out groups analysis. I want to keep the order of factor same as the order of conversion specified in case_when. In this case, the order should be "Excellent" …
user5068121
9
votes
1 answer

fct_reorder factor column by another column

I have this dataframe called test: structure(list(event_type = structure(c(5L, 6L, 8L, 3L, 9L, 1L, 7L, 4L, 10L, 2L), .Label = c("BLOCK", "CHL", "FAC", "GIVE", "GOAL", "HIT", "MISS", "SHOT", "STOP", "TAKE"), class = "factor"), hazard_ratio =…
user8248672
7
votes
1 answer

relevel factors and reorder factors using tidyverse in R

I want to use the functions relevel() and reorder() in my data frame. I understand how relevel works, but I do NOT understand why I do not see the change in the levels in my data.frame. For example, imagine that I have the iris…
LDT
  • 2,856
  • 2
  • 15
  • 32
6
votes
2 answers

How to order primary Y axis within label in ggplot

I am new to ggplot library. And trying to draw the plot using the following data.frame: library(tidyverse) df <- tribble(~event, ~startdate,~enddate,~loc, "A",as.POSIXct("1984/02/10"),as.POSIXct("1987/06/10"),"1", …
Saurabh Chauhan
  • 3,161
  • 2
  • 19
  • 46
6
votes
3 answers

re-ordering factors according to a value using fct_reorder in R

My data: structure(list(LoB = c("C", "C", "C", "A", "A", "B", "C", "A", "A", "C", "A", "B", "C", "B", "A", "C", "B", "A", "B", "C", "A", "B", "B", "A", "B", "C", "A", "B", "C", "B"), word = c("speed", "connection", "call", "bt", "reliable",…
Shery
  • 1,808
  • 5
  • 27
  • 51
6
votes
1 answer

Can forcats::as_factor return an ordered factor?

Can as_factor from forcats return an ordered factor? It seems like a missing feature if not although I haven't seen it reported as an issue on the GitHub page. I have tried: y <- forcats::as_factor(c("a", "z", "g"), ordered = TRUE) is.ordered(y) #…
Danny
  • 448
  • 3
  • 15
5
votes
1 answer

How to plot one factor level as a base on geom_col/geom_area

I've written following function that makes a customised stacked plot: stacked_plot <- function(data, what, by = NULL, date_col = date, date_unit = NULL, type = 'area'){ by <- enquo(by) what <- ensym(what) date_col <- ensym(date_col) …
Kuba_
  • 886
  • 6
  • 22
5
votes
2 answers

how to reorder a factor in a dataframe with fct_reorder?

Consider the following example > library(forcats) > library(dplyr) > > > dataframe <- data_frame(var = c(1,1,1,2,3,4), + var2 = c(10,9,8,7,6,5)) > dataframe # A tibble: 6 x 2 var var2 1 1.00 10.0 2 …
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
4
votes
3 answers

Combining data under different factor levels while retaining original levels

I would like to have a tidyverse solution for the following problem. In my dataset, I have data on various factor levels. I would like to create a new factor level "Total" that is the sum of all values Y at existing factor levels of X. This can be…
miwin
  • 61
  • 9
4
votes
1 answer

How to reorder a factor based on a subset (facets) of another variable, using forcats?

forcats vignette states that The goal of the forcats package is to provide a suite of useful tools that solve common problems with factors And indeed one of the tools is to reorder factors by another variable, which is a very common use case…
Hernando Casas
  • 2,837
  • 4
  • 21
  • 30
4
votes
1 answer

How to relevel factor column using the fct_relevel function?

I am an absolute beginner and really struggling - can anyone please help and tell me why I get that error message and how to resolve it? This is what I am trying to do: and this is my…
Natasha Reece
  • 53
  • 1
  • 5
3
votes
3 answers

Reorder factor level based on group averages

I often make a factor variable that I want to retain the order of the variable it comes from. I feel like I should be able to do this by taking the average within each group of the new categorical variable, then using that as the ordering variable…
Jack Landry
  • 138
  • 8
3
votes
1 answer

Reordering factors by group using fct_relevel only change factor order in first group

Context: I need to use factor order to make arrange diplays a table in a certain way. I.e I want to get a "Total" line at the end of each group. Problem: using fct_relevel I acheive the expected output only within the first…
Paul
  • 2,850
  • 1
  • 12
  • 37
3
votes
2 answers

Forcats reordering not working for ggplot

I have the following code for plotting a simple lollipop chart for my data: p <- data %>% mutate(Activity_Name = fct_reorder(Activity_Name, count)) %>% ggplot(aes(x = Activity_Name, y = count)) + geom_segment(aes( x = Activity_Name, …
namtar
  • 95
  • 9
1
2 3
12 13