Skip to content

Instantly share code, notes, and snippets.

@Nicktz
Created May 18, 2021 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicktz/0edbeef4f3d56233a5f6b93fa0f834d9 to your computer and use it in GitHub Desktop.
Save Nicktz/0edbeef4f3d56233a5f6b93fa0f834d9 to your computer and use it in GitHub Desktop.
Arranging by column for ggplot
# This function takes in a dataframe, a column name (to be arranged), and the order with which to arrange it.
# Use example:
library(tidyverse)
set.seed(123)
df <- tibble::tibble( Charcol = LETTERS[1:10], Numcol = rnorm(10))
order <- df %>% arrange(desc(Numcol)) %>% pull(Charcol) %>% unique
plot_orderset <- function(df, Column, Order) {
df[,Column][[1]] <- factor(df[,Column][[1]], levels = Order)
df
}
df %>%
plot_orderset(., Column = "Charcol", Order = order) %>%
ggplot() +
geom_bar(aes(Charcol, Numcol), stat = "identity")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment