Created
May 18, 2021 06:07
-
-
Save Nicktz/0edbeef4f3d56233a5f6b93fa0f834d9 to your computer and use it in GitHub Desktop.
Arranging by column for ggplot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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