Horizontal geom_col() - Issue when fct_reorder() - Works Vertically.

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.

What seems to be the error?
The code works fine with me.
Rplot

What the...???

I don't get an error but my plot comes out like below, I'll attach two screen shots:

As you can see below the plot with names on Y gave me these lines...

How the?? Literally the same code.

Ran a reprex presented below:

# Don't know why it is categorizing `~custname` as list when creating the tribble.
sample <- tribble(
~custname,         ~Tot.Billed.Amount,
#> Error: <text>:4:0: unexpected end of input
#> 2: sample <- tribble(
#> 3: ~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)

# Plot 1
sample$custname <-  as.character(sample$custname)
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()
# Side 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()
#> Error: <text>:1:13: unexpected ','
#> 1: "CHARLESTON",
#>                 ^

Created on 2020-06-13 by the reprex package (v0.2.1)

Your reprex is not actually a reprex since it produces errors instead of a plot so, Do you mind if we work with this reprex instead? Can you run this code and see if you get the desired result?

library(tidyverse)

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)

sample %>%
    drop_na() %>% 
    ggplot(aes(Tot.Billed.Amount, reorder(custname, Tot.Billed.Amount))) +
    geom_col() +
    labs(x = "Total Billed Amount",
         y = "Customer Name")

Dear @andresrcs,

I literally just opened a script in my RStudio and pasted your above script.

library("tidyverse")
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)

sample %>%
  drop_na() %>% 
  ggplot(aes(Tot.Billed.Amount, reorder(custname, Tot.Billed.Amount))) +
  geom_col() +
  labs(x = "Total Billed Amount",
       y = "Customer Name")

Created on 2020-06-13 by the reprex package (v0.2.1)

This was what my plot looked like... I'm starting to believe there is something wrong with my R because I don't have the across() function on the other question and now this... I've reinstalled all packages.

Any insight?

Thank you.
LF.

What is your ggplot2 version?

Ran the below:

> sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] lubridate_1.7.4      forcats_0.4.0        stringr_1.4.0       
 [4] dplyr_0.8.0.1        purrr_0.3.2          readr_1.3.1         
 [7] tidyr_0.8.3          tibble_2.1.1         tidyverse_1.2.1     
[10] ggplot2_3.1.1        RevoUtils_11.0.3     RevoUtilsMath_11.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1       cellranger_1.1.0 pillar_1.3.1     compiler_3.5.3  
 [5] plyr_1.8.4       tools_3.5.3      packrat_0.5.0    jsonlite_1.6    
 [9] gtable_0.3.0     nlme_3.1-137     lattice_0.20-38  pkgconfig_2.0.2 
[13] rlang_0.3.4      cli_1.1.0        rstudioapi_0.10  haven_2.1.0     
[17] xml2_1.2.0       withr_2.1.2      httr_1.4.0       generics_0.0.2  
[21] hms_0.4.2        grid_3.5.3       tidyselect_0.2.5 glue_1.3.1      
[25] R6_2.3.0         readxl_1.3.1     modelr_0.1.4     magrittr_1.5    
[29] scales_1.0.0     backports_1.1.4  rvest_0.3.3      assertthat_0.2.1
[33] colorspace_1.4-1 stringi_1.4.3    lazyeval_0.2.2   munsell_0.5.0   
[37] broom_0.5.2      crayon_1.3.4    

Shouldn't just reinstalling with install.package() get the most up to date version?
Sorry for the ignorance, I've never had to deal with this sort of issue.

Thank you once again for your time and assistance, seriously.

As explained in your other topic, you are using Microsoft R open, so you are installing the latest version available at Microsoft repositories but sadly they are not up to date with CRAN.

1 Like

Thank you @andresrcs,

I have switched to the CRAN and not the MS version, that was the cause for all these issues.

Thank you for your help.
LF.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.