Hi there,
I'm trying to create a horizontal column bar that is ordered, I am able to do it when they are vertical but can't figure out why it won't work when I flip the coordenantes.
Any insights please?
# libs:
libs <- c("tidyverse",
"lubridate","formattable")
lapply(libs, require, character.only = TRUE)
rm(libs)
# Don't know why it is categorizing `~custname` as list when creating the tribble.
sample <- tribble(
~custname, ~Tot.Billed.Amount,
#--|--|----
"CHARLESTON", 372406,
"CHARLOTTE", 686340,
"CHICAGO", 5646798,
"CLEVELAND", 85695,
"DALLAS", 9780,
"HOUSTON", 64531,
"LOS ANGELES", 212179,
"MIAMI", 1082,
"MINNEAPOLIS", 452929,
"NEWARK", 5055327,
"PEORIA", 49109,
"SAN FRANCISCO", 2165,
"SEATTLE", 26334,
"CHS", NA,
"NYP", NA,
"ORD", NA,
NULL, 377524)
The following plot is what I am looking for but the bars horizontal:
# Plot 1 - Vertical Col Bars.
sample$custname <- as.character(sample$custname) # fixing the column type which originally is a string
sample %>% mutate(custname = as.factor(custname)) %>%
rename(Customer.Name = custname) %>% mutate(Customer.Name = fct_reorder(Customer.Name,Tot.Billed.Amount)) %>%
drop_na(Tot.Billed.Amount) %>%
ggplot(aes(Customer.Name,Tot.Billed.Amount)) + geom_col()
The Plot 1 should give you a simple bar chart from smallest to largest, Plot 2 doesn't work it produces a single line.
# Plot 2 - Horizontal Columns bar won't work:
sample %>% mutate(custname = as.factor(custname)) %>%
rename(Customer.Name = custname) %>% mutate(Customer.Name = fct_reorder(Customer.Name,Tot.Billed.Amount)) %>%
drop_na(Tot.Billed.Amount) %>%
ggplot(aes(Tot.Billed.Amount,Customer.Name)) + geom_col()
Thank you for your time and assistance.
LF.