How to proportion the barplot height in facet_grid()

ggplot2

#1

Hello,

I used facet_grid() to produce two plots: one for percentage and one for sale dollars.
Somehow, the height for each bar plot seems to be off i.e the 20% bar is shorter than the 5% bar.
There is an issue with the chr in my data frame and I suspected that caused the issue why my barplot heigh is inproportional.
I then changed the chr to num but the plot now has two plots in the same grid, but one of them (Sales_Amount plot is blank)

library(tidyverse) 
library(scales) 
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
library(forcats) 
library(dplyr)
library(ggplot2)
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
library(reprex)
library(cowplot)
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave

df = structure(list(QtySold = c(4L, 2L, 1L, 1L, 3L, 2L, 2L, 9L, 1L, 
                                    1L, 4L, 1L, 1L, 6L, 5L, 4L, 1L, 12L, 1L, 1L, 2L, 1L, 1L, 4L, 
                                    6L, -1L, 2L, 2L, 1L, 3L, 1L, 1L, 2L, 1L, 4L, 6L, 18L, 8L, 1L, 
                                    1L, 2L, 1L, 6L, 5L, 4L, 1L, 1L, 12L, 2L, 18L, 1L, 4L, 5L, 1L, 
                                    1L, 2L, 1L, 3L, 4L, 1L, 2L, 1L, 2L, 2L, 2L, 6L, 3L, 1L, 5L, 4L, 
                                    1L, 8L, 8L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 8L, 3L, 5L, 2L, 2L, 1L, 
                                    1L, 6L, 2L, 3L, 1L, 2L, 3L, 1L, 3L, 1L, 6L, 1L, 2L, 2L), PRODUCT_SUB_LINE_DESCR = structure(c(2L, 
                                                                                                                                  2L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 4L, 2L, 4L, 4L, 
                                                                                                                                  2L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 2L, 4L, 2L, 
                                                                                                                                  2L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 
                                                                                                                                  2L, 4L, 4L, 2L, 4L, 4L, 4L, 2L, 4L, 2L, 2L, 2L, 4L, 2L, 4L, 2L, 
                                                                                                                                  4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 1L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 
                                                                                                                                  2L, 4L, 2L, 1L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 
                                                                                                                                  4L, 4L, 2L), .Label = c("Handpieces", "PRIVATE LABEL", "SUND DISC", 
                                                                                                                                                          "SUNDRY"), class = "factor"), Sales = c(33.6, 55.7, 68.25, 0, 
                                                                                                                                                                                                  277.08, 13.5, 117.3, 1214.64, 30.25, 109.2, 487.12, 12.99, 122, 
                                                                                                                                                                                                  49.62, 76.55, 77, 133.95, 466.44, 85.95, 172.5, 55.14, 93.5, 
                                                                                                                                                                                                  55.4, 31.8, 66.3, -98.95, 14.3, 29.98, 25.15, 234, 37.5, 39.95, 
                                                                                                                                                                                                  50.3, 13.99, 212.6, 48.3, 0, 76.48, 115.85, 14, 135.2, 118.25, 
                                                                                                                                                                                                  882.6, 79.75, 467.8, 6.78, 14.95, 398.4, 41.98, 66.24, 37.03, 
                                                                                                                                                                                                  304.28, 115, 43.25, 146.95, 10, 15.95, 166.5, 20.96, 0, 13.9, 
                                                                                                                                                                                                  307, 27.4, 129.5, 12.5, 267, 32.85, 26.55, 64.85, 22.36, 20.76, 
                                                                                                                                                                                                  98.95, 0, 415.65, 130, 98, 41.95, 45, 86.84, 176.13, 291.2, 62.85, 
                                                                                                                                                                                                  193.45, 26.5, 104.3, 43.95, 199.25, 65.7, 23, 33.15, 50.55, 54.34, 
                                                                                                                                                                                                  74.97, 16.95, 87.17, 29.95, 245.4, 0, 121.36, 51), MarginDollars = c(16.6, 
                                                                                                                                                                                                                                                                       25.76, 17.13, -46.05, 43.11, 3.88, 47.3, 359.82, 13.24, 20.97, 
                                                                                                                                                                                                                                                                       161.32, 6.24, 50.92, 4.02, 36.7, 29.52, 50.89, 118.92, 43, 66, 
                                                                                                                                                                                                                                                                       24.76, 25.52, 27.92, 9.92, 32.82, -31.96, 7.2, 8.76, 12.72, 60.75, 
                                                                                                                                                                                                                                                                       18.75, 15.45, 22.34, 2.51, 101.04, 21.42, -54, 16.48, 28.66, 
                                                                                                                                                                                                                                                                       2.9, 50.76, 33.75, 322.62, 39.9, 100, 3.3, 5.5, 96, 13.92, 29.34, 
                                                                                                                                                                                                                                                                       10.13, 88.48, 38.75, 15.45, 58.95, 4.88, 7.98, 76.23, 7.04, 0, 
                                                                                                                                                                                                                                                                       6.94, 107.51, -0.98, 35.38, 5.44, 102.9, 16.44, 12.57, 30.45, 
                                                                                                                                                                                                                                                                       0.04, 4.56, 41.45, -0.08, 155, 44.23, 41.19, 20.97, 18.8, 22.84, 
                                                                                                                                                                                                                                                                       57.37, 86.32, 17.94, 48.35, 12.56, 30.8, 22.97, 67.3, 32.88, 
                                                                                                                                                                                                                                                                       10.54, 16.74, 21.8, 25.82, 28.23, 1.82, 40.73, 14.98, 91.92, 
                                                                                                                                                                                                                                                                       0, 38.16, 26.5), CUSTOMER_NUMBER = c(763111133L, 746557212L, 
                                                                                                                                                                                                                                                                                                            342200066L, 301117971L, 373200028L, 760200513L, 460544031L, 460216301L, 
                                                                                                                                                                                                                                                                                                            458554144L, 460200191L, 652658559L, 368200374L, 454446742L, 768611747L, 
                                                                                                                                                                                                                                                                                                            240862172L, 768440536L, 448432620L, 240575110L, 328200931L, 708242740L, 
                                                                                                                                                                                                                                                                                                            209401043L, 648569354L, 764750988L, 328315839L, 209816642L, 642200096L, 
                                                                                                                                                                                                                                                                                                            373474760L, 652697455L, 704198213L, 763787804L, 632121714L, 710214566L, 
                                                                                                                                                                                                                                                                                                            728520517L, 344567583L, 240653097L, 610424807L, 332200144L, 375427350L, 
                                                                                                                                                                                                                                                                                                            462387101L, 743200311L, 704803447L, 444202350L, 710270195L, 212293733L, 
                                                                                                                                                                                                                                                                                                            710428450L, 768459921L, 744200124L, 652786586L, 341200148L, 374200648L, 
                                                                                                                                                                                                                                                                                                            710242975L, 373552475L, 728845537L, 304570408L, 768351445L, 743912626L, 
                                                                                                                                                                                                                                                                                                            744258184L, 703130888L, 240255680L, 304356033L, 745200209L, 450237191L, 
                                                                                                                                                                                                                                                                                                            760498333L, 448622709L, 744331164L, 704109714L, 336361286L, 209183107L, 
                                                                                                                                                                                                                                                                                                            766200292L, 304200615L, 438271889L, 236460283L, 648768108L, 610400050L, 
                                                                                                                                                                                                                                                                                                            345439672L, 344384829L, 368767216L, 766200119L, 454395778L, 328871948L, 
                                                                                                                                                                                                                                                                                                            703200493L, 642546221L, 763234221L, 209200307L, 460605751L, 620791380L, 
                                                                                                                                                                                                                                                                                                            704171787L, 304200602L, 610200847L, 704589211L, 373558583L, 460612110L, 
                                                                                                                                                                                                                                                                                                            728200867L, 708200091L, 240334744L, 766301355L, 301242670L, 703200228L, 
                                                                                                                                                                                                                                                                                                            764505193L, 438638086L), Cust_Segments = structure(c(3L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 3L, 5L, 1L, 5L, 5L, 1L, 5L, 3L, 3L, 1L, 6L, 1L, 5L, 5L, 5L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 3L, 6L, 5L, 5L, 5L, 3L, 3L, 5L, 5L, 5L, 6L, 5L, 5L, 3L, 6L, 1L, 
                                                                                                                                                                                                                                                                                                                                                                 1L, 3L, 3L, 5L, 5L, 3L, 5L, 5L, 5L, 5L, 3L, 5L, 3L, 1L, 5L, 1L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 5L, 3L, 5L, 5L, 5L, 5L, 6L, 1L, 5L, 4L, 5L, 1L, 1L, 5L, 1L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 5L, 5L, 1L, 1L, 3L, 5L, 3L, 5L, 6L, 6L, 6L, 1L, 6L, 1L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 3L, 1L, 3L, 1L, 5L, 3L, 3L, 3L, 5L, 3L, 1L, 5L, 3L, 5L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 6L), .Label = c("0", "1", "2", "3", "4", "5", "6"), class = "factor")), row.names = c(485723L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       252377L, 402164L, 866501L, 473302L, 141482L, 634829L, 538734L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       676824L, 321605L, 134718L, 831751L, 771455L, 91084L, 882874L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       125729L, 39615L, 144677L, 776352L, 361353L, 267097L, 82363L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       232166L, 1029892L, 940797L, 404645L, 201840L, 677648L, 732311L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       625444L, 907301L, 239189L, 264560L, 875560L, 200124L, 412518L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       551276L, 423981L, 1968L, 774316L, 16254L, 356605L, 371659L, 880595L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       327133L, 191350L, 642680L, 398031L, 71792L, 270565L, 630174L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       527505L, 1034597L, 474552L, 576251L, 123809L, 907733L, 126922L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       270776L, 861598L, 314906L, 427173L, 250436L, 552483L, 326319L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       586923L, 848197L, 219453L, 894751L, 917982L, 19339L, 505489L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       342574L, 539108L, 419799L, 809494L, 720683L, 49653L, 76307L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       295730L, 613241L, 250587L, 583849L, 826410L, 477638L, 663683L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       84433L, 946764L, 159166L, 1048094L, 908709L, 231996L, 706027L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       789714L, 1036271L, 266501L, 442407L, 715768L, 13007L, 1015893L
                                                                                                                                                                                                                                                                                                                                                                 ), class = c("data.table", "data.frame"))




datt <- df %>%
  filter(!is.na(QtySold),  !is.na(PRODUCT_SUB_LINE_DESCR), QtySold > 0, Sales > 0, MarginDollars > 0) %>%
  map_if(is.factor, as.character) %>%
  as_tibble() %>%
  group_by(CUSTOMER_NUMBER,PRODUCT_SUB_LINE_DESCR,Cust_Segments) %>%
  summarise(Quants = sum(QtySold)) %>%
  ungroup() %>%
  spread(PRODUCT_SUB_LINE_DESCR,Quants,fill=0) %>%
  mutate(Total_Orders = `PRIVATE LABEL` + SUNDRY + Handpieces,
         PL_Order_Percentage= round((`PRIVATE LABEL` / Total_Orders) * 100))

datt_sales <- df %>%
  select(QtySold, PRODUCT_SUB_LINE_DESCR, Sales, MarginDollars, CUSTOMER_NUMBER, Cust_Segments) %>%
  filter(!is.na(QtySold),  !is.na(PRODUCT_SUB_LINE_DESCR), QtySold > 0, Sales > 0, MarginDollars > 0) %>%
  map_if(is.factor, as.character) %>%
  as_tibble() %>%
  group_by(CUSTOMER_NUMBER,PRODUCT_SUB_LINE_DESCR) %>%
  summarise(My_Sales = sum(Sales)) %>%
  ungroup () %>%
  spread(PRODUCT_SUB_LINE_DESCR,My_Sales,fill=0) %>%
  mutate(Total_Sales = `PRIVATE LABEL` + SUNDRY + Handpieces)

datt_merge = merge(datt, datt_sales, by = "CUSTOMER_NUMBER", all = TRUE)
new_datt_merge = 
  datt_merge %>%
  select(CUSTOMER_NUMBER, Cust_Segments,Handpieces.x,`PRIVATE LABEL.x`,SUNDRY.x,Total_Orders, Total_Sales,PL_Order_Percentage) %>%
  mutate(category = cut(PL_Order_Percentage,breaks = c(0,1,11,21,31,41,51,61,71,81,91,100,Inf), 
                        labels = c('0%','1%-10%','11%-20%',
                                   '21%-30%','31%-40%','41%-50%',
                                   '51%-60%','61%-70%','71%-80%','81%-90%','91%-99%','100%'),include.lowest = T,right = F)) %>%
  group_by(category) %>%
  summarise(count=n(), Total_Sales = sum(Total_Sales)) %>% 
  mutate(percent = as.numeric(round(count/sum(count)*100)),
         Percentage= paste0(round(count/sum(count)*100),'%'),
         Sales_Amount = (Total_Sales),
         Sale_Dollars = paste0("$",comma(Total_Sales)))

newdf <- new_datt_merge %>%
  gather(Bucket, value, -category) %>% 
  filter(Bucket %in% c("Sales_Amount","percent"))
newdf$value = as.numeric((as.character(newdf$value)))

newdf %>% ggplot()+
  geom_bar(aes(x=category,y=value, fill = category),stat='identity') + 
  theme(legend.position="none") +
  labs(x ="",y="")+
  facet_grid(Bucket~.) +
  geom_text(aes(x=category,y=value, label = comma(value)),vjust=-0.5) +
  theme(legend.position="none",axis.text.y = element_blank(),  axis.ticks = element_blank(),
        axis.line.y = element_blank())

str(newdf)
#> Classes 'tbl_df', 'tbl' and 'data.frame':    4 obs. of  3 variables:
#>  $ category: Factor w/ 12 levels "0%","1%-10%",..: 1 12 1 12
#>  $ Bucket  : chr  "percent" "percent" "Sales_Amount" "Sales_Amount"
#>  $ value   : num  54 46 8983 2672
str(new_datt_merge)
#> Classes 'tbl_df', 'tbl' and 'data.frame':    2 obs. of  7 variables:
#>  $ category    : Factor w/ 12 levels "0%","1%-10%",..: 1 12
#>  $ count       : int  50 43
#>  $ Total_Sales : num  8983 2672
#>  $ percent     : num  54 46
#>  $ Percentage  : chr  "54%" "46%"
#>  $ Sales_Amount: num  8983 2672
#>  $ Sale_Dollars: chr  "$8,983.23" "$2,671.65"

Created on 2018-07-17 by the reprex
package
(v0.2.0).
Thank you!


#2

Although I'm sure there will be ingenious workarounds, the proper answer is to produce two plots rather than one combined one because you are displaying two different sets of units for the y-axes.

You can always align the two plots using e.g. cowplot or similar.


#3

I have fixed the original issue but I have another issue the comes along.
For the percent plot, how comes the 20% bar is shorter than the 5% bar?

library(tidyverse) 
library(scales) 
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
library(forcats) 
library(dplyr)
library(ggplot2)
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
library(reprex)
library(cowplot)
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave

df = structure(list(QtySold = c(4L, 2L, 1L, 1L, 3L, 2L, 2L, 9L, 1L, 
                                    1L, 4L, 1L, 1L, 6L, 5L, 4L, 1L, 12L, 1L, 1L, 2L, 1L, 1L, 4L, 
                                    6L, -1L, 2L, 2L, 1L, 3L, 1L, 1L, 2L, 1L, 4L, 6L, 18L, 8L, 1L, 
                                    1L, 2L, 1L, 6L, 5L, 4L, 1L, 1L, 12L, 2L, 18L, 1L, 4L, 5L, 1L, 
                                    1L, 2L, 1L, 3L, 4L, 1L, 2L, 1L, 2L, 2L, 2L, 6L, 3L, 1L, 5L, 4L, 
                                    1L, 8L, 8L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 8L, 3L, 5L, 2L, 2L, 1L, 
                                    1L, 6L, 2L, 3L, 1L, 2L, 3L, 1L, 3L, 1L, 6L, 1L, 2L, 2L), PRODUCT_SUB_LINE_DESCR = structure(c(2L, 
                                                                                                                                  2L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 4L, 2L, 4L, 4L, 
                                                                                                                                  2L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 2L, 4L, 2L, 
                                                                                                                                  2L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 
                                                                                                                                  2L, 4L, 4L, 2L, 4L, 4L, 4L, 2L, 4L, 2L, 2L, 2L, 4L, 2L, 4L, 2L, 
                                                                                                                                  4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 1L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 
                                                                                                                                  2L, 4L, 2L, 1L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 
                                                                                                                                  4L, 4L, 2L), .Label = c("Handpieces", "PRIVATE LABEL", "SUND DISC", 
                                                                                                                                                          "SUNDRY"), class = "factor"), Sales = c(33.6, 55.7, 68.25, 0, 
                                                                                                                                                                                                  277.08, 13.5, 117.3, 1214.64, 30.25, 109.2, 487.12, 12.99, 122, 
                                                                                                                                                                                                  49.62, 76.55, 77, 133.95, 466.44, 85.95, 172.5, 55.14, 93.5, 
                                                                                                                                                                                                  55.4, 31.8, 66.3, -98.95, 14.3, 29.98, 25.15, 234, 37.5, 39.95, 
                                                                                                                                                                                                  50.3, 13.99, 212.6, 48.3, 0, 76.48, 115.85, 14, 135.2, 118.25, 
                                                                                                                                                                                                  882.6, 79.75, 467.8, 6.78, 14.95, 398.4, 41.98, 66.24, 37.03, 
                                                                                                                                                                                                  304.28, 115, 43.25, 146.95, 10, 15.95, 166.5, 20.96, 0, 13.9, 
                                                                                                                                                                                                  307, 27.4, 129.5, 12.5, 267, 32.85, 26.55, 64.85, 22.36, 20.76, 
                                                                                                                                                                                                  98.95, 0, 415.65, 130, 98, 41.95, 45, 86.84, 176.13, 291.2, 62.85, 
                                                                                                                                                                                                  193.45, 26.5, 104.3, 43.95, 199.25, 65.7, 23, 33.15, 50.55, 54.34, 
                                                                                                                                                                                                  74.97, 16.95, 87.17, 29.95, 245.4, 0, 121.36, 51), MarginDollars = c(16.6, 
                                                                                                                                                                                                                                                                       25.76, 17.13, -46.05, 43.11, 3.88, 47.3, 359.82, 13.24, 20.97, 
                                                                                                                                                                                                                                                                       161.32, 6.24, 50.92, 4.02, 36.7, 29.52, 50.89, 118.92, 43, 66, 
                                                                                                                                                                                                                                                                       24.76, 25.52, 27.92, 9.92, 32.82, -31.96, 7.2, 8.76, 12.72, 60.75, 
                                                                                                                                                                                                                                                                       18.75, 15.45, 22.34, 2.51, 101.04, 21.42, -54, 16.48, 28.66, 
                                                                                                                                                                                                                                                                       2.9, 50.76, 33.75, 322.62, 39.9, 100, 3.3, 5.5, 96, 13.92, 29.34, 
                                                                                                                                                                                                                                                                       10.13, 88.48, 38.75, 15.45, 58.95, 4.88, 7.98, 76.23, 7.04, 0, 
                                                                                                                                                                                                                                                                       6.94, 107.51, -0.98, 35.38, 5.44, 102.9, 16.44, 12.57, 30.45, 
                                                                                                                                                                                                                                                                       0.04, 4.56, 41.45, -0.08, 155, 44.23, 41.19, 20.97, 18.8, 22.84, 
                                                                                                                                                                                                                                                                       57.37, 86.32, 17.94, 48.35, 12.56, 30.8, 22.97, 67.3, 32.88, 
                                                                                                                                                                                                                                                                       10.54, 16.74, 21.8, 25.82, 28.23, 1.82, 40.73, 14.98, 91.92, 
                                                                                                                                                                                                                                                                       0, 38.16, 26.5), CUSTOMER_NUMBER = c(763111133L, 746557212L, 
                                                                                                                                                                                                                                                                                                            342200066L, 301117971L, 373200028L, 760200513L, 460544031L, 460216301L, 
                                                                                                                                                                                                                                                                                                            458554144L, 460200191L, 652658559L, 368200374L, 454446742L, 768611747L, 
                                                                                                                                                                                                                                                                                                            240862172L, 768440536L, 448432620L, 240575110L, 328200931L, 708242740L, 
                                                                                                                                                                                                                                                                                                            209401043L, 648569354L, 764750988L, 328315839L, 209816642L, 642200096L, 
                                                                                                                                                                                                                                                                                                            373474760L, 652697455L, 704198213L, 763787804L, 632121714L, 710214566L, 
                                                                                                                                                                                                                                                                                                            728520517L, 344567583L, 240653097L, 610424807L, 332200144L, 375427350L, 
                                                                                                                                                                                                                                                                                                            462387101L, 743200311L, 704803447L, 444202350L, 710270195L, 212293733L, 
                                                                                                                                                                                                                                                                                                            710428450L, 768459921L, 744200124L, 652786586L, 341200148L, 374200648L, 
                                                                                                                                                                                                                                                                                                            710242975L, 373552475L, 728845537L, 304570408L, 768351445L, 743912626L, 
                                                                                                                                                                                                                                                                                                            744258184L, 703130888L, 240255680L, 304356033L, 745200209L, 450237191L, 
                                                                                                                                                                                                                                                                                                            760498333L, 448622709L, 744331164L, 704109714L, 336361286L, 209183107L, 
                                                                                                                                                                                                                                                                                                            766200292L, 304200615L, 438271889L, 236460283L, 648768108L, 610400050L, 
                                                                                                                                                                                                                                                                                                            345439672L, 344384829L, 368767216L, 766200119L, 454395778L, 328871948L, 
                                                                                                                                                                                                                                                                                                            703200493L, 642546221L, 763234221L, 209200307L, 460605751L, 620791380L, 
                                                                                                                                                                                                                                                                                                            704171787L, 304200602L, 610200847L, 704589211L, 373558583L, 460612110L, 
                                                                                                                                                                                                                                                                                                            728200867L, 708200091L, 240334744L, 766301355L, 301242670L, 703200228L, 
                                                                                                                                                                                                                                                                                                            764505193L, 438638086L), Cust_Segments = structure(c(3L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 3L, 5L, 1L, 5L, 5L, 1L, 5L, 3L, 3L, 1L, 6L, 1L, 5L, 5L, 5L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 3L, 6L, 5L, 5L, 5L, 3L, 3L, 5L, 5L, 5L, 6L, 5L, 5L, 3L, 6L, 1L, 
                                                                                                                                                                                                                                                                                                                                                                 1L, 3L, 3L, 5L, 5L, 3L, 5L, 5L, 5L, 5L, 3L, 5L, 3L, 1L, 5L, 1L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 5L, 3L, 5L, 5L, 5L, 5L, 6L, 1L, 5L, 4L, 5L, 1L, 1L, 5L, 1L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 5L, 5L, 1L, 1L, 3L, 5L, 3L, 5L, 6L, 6L, 6L, 1L, 6L, 1L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 3L, 1L, 3L, 1L, 5L, 3L, 3L, 3L, 5L, 3L, 1L, 5L, 3L, 5L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 6L), .Label = c("0", "1", "2", "3", "4", "5", "6"), class = "factor")), row.names = c(485723L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       252377L, 402164L, 866501L, 473302L, 141482L, 634829L, 538734L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       676824L, 321605L, 134718L, 831751L, 771455L, 91084L, 882874L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       125729L, 39615L, 144677L, 776352L, 361353L, 267097L, 82363L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       232166L, 1029892L, 940797L, 404645L, 201840L, 677648L, 732311L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       625444L, 907301L, 239189L, 264560L, 875560L, 200124L, 412518L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       551276L, 423981L, 1968L, 774316L, 16254L, 356605L, 371659L, 880595L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       327133L, 191350L, 642680L, 398031L, 71792L, 270565L, 630174L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       527505L, 1034597L, 474552L, 576251L, 123809L, 907733L, 126922L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       270776L, 861598L, 314906L, 427173L, 250436L, 552483L, 326319L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       586923L, 848197L, 219453L, 894751L, 917982L, 19339L, 505489L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       342574L, 539108L, 419799L, 809494L, 720683L, 49653L, 76307L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       295730L, 613241L, 250587L, 583849L, 826410L, 477638L, 663683L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       84433L, 946764L, 159166L, 1048094L, 908709L, 231996L, 706027L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       789714L, 1036271L, 266501L, 442407L, 715768L, 13007L, 1015893L
                                                                                                                                                                                                                                                                                                                                                                 ), class = c("data.table", "data.frame"))




datt <- df %>%
  filter(!is.na(QtySold),  !is.na(PRODUCT_SUB_LINE_DESCR), QtySold > 0, Sales > 0, MarginDollars > 0) %>%
  map_if(is.factor, as.character) %>%
  as_tibble() %>%
  group_by(CUSTOMER_NUMBER,PRODUCT_SUB_LINE_DESCR,Cust_Segments) %>%
  summarise(Quants = sum(QtySold)) %>%
  ungroup() %>%
  spread(PRODUCT_SUB_LINE_DESCR,Quants,fill=0) %>%
  mutate(Total_Orders = `PRIVATE LABEL` + SUNDRY + Handpieces,
         PL_Order_Percentage= round((`PRIVATE LABEL` / Total_Orders) * 100))

datt_sales <- df %>%
  select(QtySold, PRODUCT_SUB_LINE_DESCR, Sales, MarginDollars, CUSTOMER_NUMBER, Cust_Segments) %>%
  filter(!is.na(QtySold),  !is.na(PRODUCT_SUB_LINE_DESCR), QtySold > 0, Sales > 0, MarginDollars > 0) %>%
  map_if(is.factor, as.character) %>%
  as_tibble() %>%
  group_by(CUSTOMER_NUMBER,PRODUCT_SUB_LINE_DESCR) %>%
  summarise(My_Sales = sum(Sales)) %>%
  ungroup () %>%
  spread(PRODUCT_SUB_LINE_DESCR,My_Sales,fill=0) %>%
  mutate(Total_Sales = `PRIVATE LABEL` + SUNDRY + Handpieces)

datt_merge = merge(datt, datt_sales, by = "CUSTOMER_NUMBER", all = TRUE)
new_datt_merge = 
  datt_merge %>%
  select(CUSTOMER_NUMBER, Cust_Segments,Handpieces.x,`PRIVATE LABEL.x`,SUNDRY.x,Total_Orders, Total_Sales,PL_Order_Percentage) %>%
  mutate(category = cut(PL_Order_Percentage,breaks = c(0,1,11,21,31,41,51,61,71,81,91,100,Inf), 
                        labels = c('0%','1%-10%','11%-20%',
                                   '21%-30%','31%-40%','41%-50%',
                                   '51%-60%','61%-70%','71%-80%','81%-90%','91%-99%','100%'),include.lowest = T,right = F)) %>%
  group_by(category) %>%
  summarise(count=n(), Total_Sales = sum(Total_Sales)) %>% 
  mutate(percent = round(count/sum(count)*100),
         Percentage= paste0(round(count/sum(count)*100),'%'),
         Sale_Dollars = paste0("$",comma(Total_Sales)))

newdf <- new_datt_merge %>%
  gather(Bucket, value, -category) %>%
  filter(Bucket %in% c("Sale_Dollars","Percentage")) %>%
  ggplot()+
  geom_bar(aes(x=category,y=value, fill = category),stat='identity') + 
  theme(legend.position="none") +
  labs(x ="",y="")+
  facet_grid(Bucket~., scales = "free") +
  geom_text(aes(x=category,y=value, label = (value)),vjust=-0.5) +
  theme(legend.position="none",axis.text.y = element_blank(),  axis.ticks = element_blank(),
        axis.line.y = element_blank())
newdf

Created on 2018-07-17 by the reprex
package
(v0.2.0).
I meant the df in reprex() does not fully contain the issue I am facing since the plot looks correct.
The screenshot I attached is from my original data frame.


#4

Removing the y-axes and labelling the bars is a good solution in this case.


#5

Exactly. I did that.
But do you see my point from my screen shot that I attached?


The 20% bar is shorter than the 7% bar. It does not make sense to me.


#6

y = value is a string and not an integer or numerical value.

To be honest I'm surprised this didn't generate an error.


#7

Why would it generate an error?
Even when I change value to a numerical value, the barplot still gives the same result.
I am not sure how to fix this.


#8

Thinking about it, the y-axes are being displayed as ordered factors (due to the values being strings), so the lowest value (alphabetically) is shown as one unit on the scale, then the second lowest as two units, etc.

You need to change the y = value part to refer to the actual values, not the formatted strings.


#9

I did.
The plot on my end is still the same. The bar height is not proportional.

library(tidyverse) 
library(scales) 
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
library(forcats) 
library(dplyr)
library(ggplot2)
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
library(reprex)
library(cowplot)
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave

df = structure(list(QtySold = c(4L, 2L, 1L, 1L, 3L, 2L, 2L, 9L, 1L, 
                                    1L, 4L, 1L, 1L, 6L, 5L, 4L, 1L, 12L, 1L, 1L, 2L, 1L, 1L, 4L, 
                                    6L, -1L, 2L, 2L, 1L, 3L, 1L, 1L, 2L, 1L, 4L, 6L, 18L, 8L, 1L, 
                                    1L, 2L, 1L, 6L, 5L, 4L, 1L, 1L, 12L, 2L, 18L, 1L, 4L, 5L, 1L, 
                                    1L, 2L, 1L, 3L, 4L, 1L, 2L, 1L, 2L, 2L, 2L, 6L, 3L, 1L, 5L, 4L, 
                                    1L, 8L, 8L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 8L, 3L, 5L, 2L, 2L, 1L, 
                                    1L, 6L, 2L, 3L, 1L, 2L, 3L, 1L, 3L, 1L, 6L, 1L, 2L, 2L), PRODUCT_SUB_LINE_DESCR = structure(c(2L, 
                                                                                                                                  2L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 4L, 2L, 4L, 4L, 
                                                                                                                                  2L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 2L, 4L, 2L, 
                                                                                                                                  2L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 
                                                                                                                                  2L, 4L, 4L, 2L, 4L, 4L, 4L, 2L, 4L, 2L, 2L, 2L, 4L, 2L, 4L, 2L, 
                                                                                                                                  4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 1L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 
                                                                                                                                  2L, 4L, 2L, 1L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 
                                                                                                                                  4L, 4L, 2L), .Label = c("Handpieces", "PRIVATE LABEL", "SUND DISC", 
                                                                                                                                                          "SUNDRY"), class = "factor"), Sales = c(33.6, 55.7, 68.25, 0, 
                                                                                                                                                                                                  277.08, 13.5, 117.3, 1214.64, 30.25, 109.2, 487.12, 12.99, 122, 
                                                                                                                                                                                                  49.62, 76.55, 77, 133.95, 466.44, 85.95, 172.5, 55.14, 93.5, 
                                                                                                                                                                                                  55.4, 31.8, 66.3, -98.95, 14.3, 29.98, 25.15, 234, 37.5, 39.95, 
                                                                                                                                                                                                  50.3, 13.99, 212.6, 48.3, 0, 76.48, 115.85, 14, 135.2, 118.25, 
                                                                                                                                                                                                  882.6, 79.75, 467.8, 6.78, 14.95, 398.4, 41.98, 66.24, 37.03, 
                                                                                                                                                                                                  304.28, 115, 43.25, 146.95, 10, 15.95, 166.5, 20.96, 0, 13.9, 
                                                                                                                                                                                                  307, 27.4, 129.5, 12.5, 267, 32.85, 26.55, 64.85, 22.36, 20.76, 
                                                                                                                                                                                                  98.95, 0, 415.65, 130, 98, 41.95, 45, 86.84, 176.13, 291.2, 62.85, 
                                                                                                                                                                                                  193.45, 26.5, 104.3, 43.95, 199.25, 65.7, 23, 33.15, 50.55, 54.34, 
                                                                                                                                                                                                  74.97, 16.95, 87.17, 29.95, 245.4, 0, 121.36, 51), MarginDollars = c(16.6, 
                                                                                                                                                                                                                                                                       25.76, 17.13, -46.05, 43.11, 3.88, 47.3, 359.82, 13.24, 20.97, 
                                                                                                                                                                                                                                                                       161.32, 6.24, 50.92, 4.02, 36.7, 29.52, 50.89, 118.92, 43, 66, 
                                                                                                                                                                                                                                                                       24.76, 25.52, 27.92, 9.92, 32.82, -31.96, 7.2, 8.76, 12.72, 60.75, 
                                                                                                                                                                                                                                                                       18.75, 15.45, 22.34, 2.51, 101.04, 21.42, -54, 16.48, 28.66, 
                                                                                                                                                                                                                                                                       2.9, 50.76, 33.75, 322.62, 39.9, 100, 3.3, 5.5, 96, 13.92, 29.34, 
                                                                                                                                                                                                                                                                       10.13, 88.48, 38.75, 15.45, 58.95, 4.88, 7.98, 76.23, 7.04, 0, 
                                                                                                                                                                                                                                                                       6.94, 107.51, -0.98, 35.38, 5.44, 102.9, 16.44, 12.57, 30.45, 
                                                                                                                                                                                                                                                                       0.04, 4.56, 41.45, -0.08, 155, 44.23, 41.19, 20.97, 18.8, 22.84, 
                                                                                                                                                                                                                                                                       57.37, 86.32, 17.94, 48.35, 12.56, 30.8, 22.97, 67.3, 32.88, 
                                                                                                                                                                                                                                                                       10.54, 16.74, 21.8, 25.82, 28.23, 1.82, 40.73, 14.98, 91.92, 
                                                                                                                                                                                                                                                                       0, 38.16, 26.5), CUSTOMER_NUMBER = c(763111133L, 746557212L, 
                                                                                                                                                                                                                                                                                                            342200066L, 301117971L, 373200028L, 760200513L, 460544031L, 460216301L, 
                                                                                                                                                                                                                                                                                                            458554144L, 460200191L, 652658559L, 368200374L, 454446742L, 768611747L, 
                                                                                                                                                                                                                                                                                                            240862172L, 768440536L, 448432620L, 240575110L, 328200931L, 708242740L, 
                                                                                                                                                                                                                                                                                                            209401043L, 648569354L, 764750988L, 328315839L, 209816642L, 642200096L, 
                                                                                                                                                                                                                                                                                                            373474760L, 652697455L, 704198213L, 763787804L, 632121714L, 710214566L, 
                                                                                                                                                                                                                                                                                                            728520517L, 344567583L, 240653097L, 610424807L, 332200144L, 375427350L, 
                                                                                                                                                                                                                                                                                                            462387101L, 743200311L, 704803447L, 444202350L, 710270195L, 212293733L, 
                                                                                                                                                                                                                                                                                                            710428450L, 768459921L, 744200124L, 652786586L, 341200148L, 374200648L, 
                                                                                                                                                                                                                                                                                                            710242975L, 373552475L, 728845537L, 304570408L, 768351445L, 743912626L, 
                                                                                                                                                                                                                                                                                                            744258184L, 703130888L, 240255680L, 304356033L, 745200209L, 450237191L, 
                                                                                                                                                                                                                                                                                                            760498333L, 448622709L, 744331164L, 704109714L, 336361286L, 209183107L, 
                                                                                                                                                                                                                                                                                                            766200292L, 304200615L, 438271889L, 236460283L, 648768108L, 610400050L, 
                                                                                                                                                                                                                                                                                                            345439672L, 344384829L, 368767216L, 766200119L, 454395778L, 328871948L, 
                                                                                                                                                                                                                                                                                                            703200493L, 642546221L, 763234221L, 209200307L, 460605751L, 620791380L, 
                                                                                                                                                                                                                                                                                                            704171787L, 304200602L, 610200847L, 704589211L, 373558583L, 460612110L, 
                                                                                                                                                                                                                                                                                                            728200867L, 708200091L, 240334744L, 766301355L, 301242670L, 703200228L, 
                                                                                                                                                                                                                                                                                                            764505193L, 438638086L), Cust_Segments = structure(c(3L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 3L, 5L, 1L, 5L, 5L, 1L, 5L, 3L, 3L, 1L, 6L, 1L, 5L, 5L, 5L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 3L, 6L, 5L, 5L, 5L, 3L, 3L, 5L, 5L, 5L, 6L, 5L, 5L, 3L, 6L, 1L, 
                                                                                                                                                                                                                                                                                                                                                                 1L, 3L, 3L, 5L, 5L, 3L, 5L, 5L, 5L, 5L, 3L, 5L, 3L, 1L, 5L, 1L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 5L, 3L, 5L, 5L, 5L, 5L, 6L, 1L, 5L, 4L, 5L, 1L, 1L, 5L, 1L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 5L, 5L, 1L, 1L, 3L, 5L, 3L, 5L, 6L, 6L, 6L, 1L, 6L, 1L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 3L, 1L, 3L, 1L, 5L, 3L, 3L, 3L, 5L, 3L, 1L, 5L, 3L, 5L, 5L, 
                                                                                                                                                                                                                                                                                                                                                                 5L, 6L), .Label = c("0", "1", "2", "3", "4", "5", "6"), class = "factor")), row.names = c(485723L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       252377L, 402164L, 866501L, 473302L, 141482L, 634829L, 538734L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       676824L, 321605L, 134718L, 831751L, 771455L, 91084L, 882874L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       125729L, 39615L, 144677L, 776352L, 361353L, 267097L, 82363L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       232166L, 1029892L, 940797L, 404645L, 201840L, 677648L, 732311L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       625444L, 907301L, 239189L, 264560L, 875560L, 200124L, 412518L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       551276L, 423981L, 1968L, 774316L, 16254L, 356605L, 371659L, 880595L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       327133L, 191350L, 642680L, 398031L, 71792L, 270565L, 630174L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       527505L, 1034597L, 474552L, 576251L, 123809L, 907733L, 126922L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       270776L, 861598L, 314906L, 427173L, 250436L, 552483L, 326319L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       586923L, 848197L, 219453L, 894751L, 917982L, 19339L, 505489L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       342574L, 539108L, 419799L, 809494L, 720683L, 49653L, 76307L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       295730L, 613241L, 250587L, 583849L, 826410L, 477638L, 663683L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       84433L, 946764L, 159166L, 1048094L, 908709L, 231996L, 706027L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       789714L, 1036271L, 266501L, 442407L, 715768L, 13007L, 1015893L
                                                                                                                                                                                                                                                                                                                                                                 ), class = c("data.table", "data.frame"))




datt <- df %>%
  filter(!is.na(QtySold),  !is.na(PRODUCT_SUB_LINE_DESCR), QtySold > 0, Sales > 0, MarginDollars > 0) %>%
  map_if(is.factor, as.character) %>%
  as_tibble() %>%
  group_by(CUSTOMER_NUMBER,PRODUCT_SUB_LINE_DESCR,Cust_Segments) %>%
  summarise(Quants = sum(QtySold)) %>%
  ungroup() %>%
  spread(PRODUCT_SUB_LINE_DESCR,Quants,fill=0) %>%
  mutate(Total_Orders = `PRIVATE LABEL` + SUNDRY + Handpieces,
         PL_Order_Percentage= round((`PRIVATE LABEL` / Total_Orders) * 100))

datt_sales <- df %>%
  select(QtySold, PRODUCT_SUB_LINE_DESCR, Sales, MarginDollars, CUSTOMER_NUMBER, Cust_Segments) %>%
  filter(!is.na(QtySold),  !is.na(PRODUCT_SUB_LINE_DESCR), QtySold > 0, Sales > 0, MarginDollars > 0) %>%
  map_if(is.factor, as.character) %>%
  as_tibble() %>%
  group_by(CUSTOMER_NUMBER,PRODUCT_SUB_LINE_DESCR) %>%
  summarise(My_Sales = sum(Sales)) %>%
  ungroup () %>%
  spread(PRODUCT_SUB_LINE_DESCR,My_Sales,fill=0) %>%
  mutate(Total_Sales = `PRIVATE LABEL` + SUNDRY + Handpieces)

datt_merge = merge(datt, datt_sales, by = "CUSTOMER_NUMBER", all = TRUE)
new_datt_merge = 
  datt_merge %>%
  select(CUSTOMER_NUMBER, Cust_Segments,Handpieces.x,`PRIVATE LABEL.x`,SUNDRY.x,Total_Orders, Total_Sales,PL_Order_Percentage) %>%
  mutate(category = cut(PL_Order_Percentage,breaks = c(0,1,11,21,31,41,51,61,71,81,91,100,Inf), 
                        labels = c('0%','1%-10%','11%-20%',
                                   '21%-30%','31%-40%','41%-50%',
                                   '51%-60%','61%-70%','71%-80%','81%-90%','91%-99%','100%'),include.lowest = T,right = F)) %>%
  group_by(category) %>%
  summarise(count=n(), Total_Sales = sum(Total_Sales)) %>% 
  mutate(percent = round(count/sum(count)*100),
         Percentage= paste0(round(count/sum(count)*100),'%'),
         Sales_Amount = Total_Sales,
         Sale_Dollars = paste0("$",comma(Total_Sales)))

newdf <- new_datt_merge %>%
  gather(Bucket, value, -category) %>%
  filter(Bucket %in% c("Sales_Amount","percent")) %>%
  ggplot()+
  geom_bar(aes(x=category,y=value, fill = category),stat='identity') + 
  theme(legend.position="none") +
  labs(x ="",y="")+
  facet_grid(Bucket~., scales = "free", space = "free") +
  geom_text(aes(x=category,y=value, label = (value)),vjust=-0.5) +
  theme(legend.position="none",axis.text.y = element_blank(),  axis.ticks = element_blank(),
        axis.line.y = element_blank())

newdf

Created on 2018-07-17 by the reprex
package
(v0.2.0).


#10

If you run your reprex (which, btw, you don't need to re-load the tidyverse core libraries if you load tidyverse), but create newdf before passing it to ggplot2, you'll see that the class of the column "value" is still "character".

library(tidyverse) 
library(scales) 
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
library(forcats) 
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
library(cowplot)
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave


df = structure(list(QtySold = c(4L, 2L, 1L, 1L, 3L, 2L, 2L, 9L, 1L, 
                                1L, 4L, 1L, 1L, 6L, 5L, 4L, 1L, 12L, 1L, 1L, 2L, 1L, 1L, 4L, 
                                6L, -1L, 2L, 2L, 1L, 3L, 1L, 1L, 2L, 1L, 4L, 6L, 18L, 8L, 1L, 
                                1L, 2L, 1L, 6L, 5L, 4L, 1L, 1L, 12L, 2L, 18L, 1L, 4L, 5L, 1L, 
                                1L, 2L, 1L, 3L, 4L, 1L, 2L, 1L, 2L, 2L, 2L, 6L, 3L, 1L, 5L, 4L, 
                                1L, 8L, 8L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 8L, 3L, 5L, 2L, 2L, 1L, 
                                1L, 6L, 2L, 3L, 1L, 2L, 3L, 1L, 3L, 1L, 6L, 1L, 2L, 2L), PRODUCT_SUB_LINE_DESCR = structure(c(2L, 
                                                                                                                              2L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 4L, 2L, 4L, 4L, 
                                                                                                                              2L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 2L, 4L, 2L, 
                                                                                                                              2L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 
                                                                                                                              2L, 4L, 4L, 2L, 4L, 4L, 4L, 2L, 4L, 2L, 2L, 2L, 4L, 2L, 4L, 2L, 
                                                                                                                              4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 1L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 
                                                                                                                              2L, 4L, 2L, 1L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 
                                                                                                                              4L, 4L, 2L), .Label = c("Handpieces", "PRIVATE LABEL", "SUND DISC", 
                                                                                                                                                      "SUNDRY"), class = "factor"), Sales = c(33.6, 55.7, 68.25, 0, 
                                                                                                                                                                                              277.08, 13.5, 117.3, 1214.64, 30.25, 109.2, 487.12, 12.99, 122, 
                                                                                                                                                                                              49.62, 76.55, 77, 133.95, 466.44, 85.95, 172.5, 55.14, 93.5, 
                                                                                                                                                                                              55.4, 31.8, 66.3, -98.95, 14.3, 29.98, 25.15, 234, 37.5, 39.95, 
                                                                                                                                                                                              50.3, 13.99, 212.6, 48.3, 0, 76.48, 115.85, 14, 135.2, 118.25, 
                                                                                                                                                                                              882.6, 79.75, 467.8, 6.78, 14.95, 398.4, 41.98, 66.24, 37.03, 
                                                                                                                                                                                              304.28, 115, 43.25, 146.95, 10, 15.95, 166.5, 20.96, 0, 13.9, 
                                                                                                                                                                                              307, 27.4, 129.5, 12.5, 267, 32.85, 26.55, 64.85, 22.36, 20.76, 
                                                                                                                                                                                              98.95, 0, 415.65, 130, 98, 41.95, 45, 86.84, 176.13, 291.2, 62.85, 
                                                                                                                                                                                              193.45, 26.5, 104.3, 43.95, 199.25, 65.7, 23, 33.15, 50.55, 54.34, 
                                                                                                                                                                                              74.97, 16.95, 87.17, 29.95, 245.4, 0, 121.36, 51), MarginDollars = c(16.6, 
                                                                                                                                                                                                                                                                   25.76, 17.13, -46.05, 43.11, 3.88, 47.3, 359.82, 13.24, 20.97, 
                                                                                                                                                                                                                                                                   161.32, 6.24, 50.92, 4.02, 36.7, 29.52, 50.89, 118.92, 43, 66, 
                                                                                                                                                                                                                                                                   24.76, 25.52, 27.92, 9.92, 32.82, -31.96, 7.2, 8.76, 12.72, 60.75, 
                                                                                                                                                                                                                                                                   18.75, 15.45, 22.34, 2.51, 101.04, 21.42, -54, 16.48, 28.66, 
                                                                                                                                                                                                                                                                   2.9, 50.76, 33.75, 322.62, 39.9, 100, 3.3, 5.5, 96, 13.92, 29.34, 
                                                                                                                                                                                                                                                                   10.13, 88.48, 38.75, 15.45, 58.95, 4.88, 7.98, 76.23, 7.04, 0, 
                                                                                                                                                                                                                                                                   6.94, 107.51, -0.98, 35.38, 5.44, 102.9, 16.44, 12.57, 30.45, 
                                                                                                                                                                                                                                                                   0.04, 4.56, 41.45, -0.08, 155, 44.23, 41.19, 20.97, 18.8, 22.84, 
                                                                                                                                                                                                                                                                   57.37, 86.32, 17.94, 48.35, 12.56, 30.8, 22.97, 67.3, 32.88, 
                                                                                                                                                                                                                                                                   10.54, 16.74, 21.8, 25.82, 28.23, 1.82, 40.73, 14.98, 91.92, 
                                                                                                                                                                                                                                                                   0, 38.16, 26.5), CUSTOMER_NUMBER = c(763111133L, 746557212L, 
                                                                                                                                                                                                                                                                                                        342200066L, 301117971L, 373200028L, 760200513L, 460544031L, 460216301L, 
                                                                                                                                                                                                                                                                                                        458554144L, 460200191L, 652658559L, 368200374L, 454446742L, 768611747L, 
                                                                                                                                                                                                                                                                                                        240862172L, 768440536L, 448432620L, 240575110L, 328200931L, 708242740L, 
                                                                                                                                                                                                                                                                                                        209401043L, 648569354L, 764750988L, 328315839L, 209816642L, 642200096L, 
                                                                                                                                                                                                                                                                                                        373474760L, 652697455L, 704198213L, 763787804L, 632121714L, 710214566L, 
                                                                                                                                                                                                                                                                                                        728520517L, 344567583L, 240653097L, 610424807L, 332200144L, 375427350L, 
                                                                                                                                                                                                                                                                                                        462387101L, 743200311L, 704803447L, 444202350L, 710270195L, 212293733L, 
                                                                                                                                                                                                                                                                                                        710428450L, 768459921L, 744200124L, 652786586L, 341200148L, 374200648L, 
                                                                                                                                                                                                                                                                                                        710242975L, 373552475L, 728845537L, 304570408L, 768351445L, 743912626L, 
                                                                                                                                                                                                                                                                                                        744258184L, 703130888L, 240255680L, 304356033L, 745200209L, 450237191L, 
                                                                                                                                                                                                                                                                                                        760498333L, 448622709L, 744331164L, 704109714L, 336361286L, 209183107L, 
                                                                                                                                                                                                                                                                                                        766200292L, 304200615L, 438271889L, 236460283L, 648768108L, 610400050L, 
                                                                                                                                                                                                                                                                                                        345439672L, 344384829L, 368767216L, 766200119L, 454395778L, 328871948L, 
                                                                                                                                                                                                                                                                                                        703200493L, 642546221L, 763234221L, 209200307L, 460605751L, 620791380L, 
                                                                                                                                                                                                                                                                                                        704171787L, 304200602L, 610200847L, 704589211L, 373558583L, 460612110L, 
                                                                                                                                                                                                                                                                                                        728200867L, 708200091L, 240334744L, 766301355L, 301242670L, 703200228L, 
                                                                                                                                                                                                                                                                                                        764505193L, 438638086L), Cust_Segments = structure(c(3L, 5L, 
                                                                                                                                                                                                                                                                                                                                                             3L, 5L, 1L, 5L, 5L, 1L, 5L, 3L, 3L, 1L, 6L, 1L, 5L, 5L, 5L, 5L, 
                                                                                                                                                                                                                                                                                                                                                             3L, 6L, 5L, 5L, 5L, 3L, 3L, 5L, 5L, 5L, 6L, 5L, 5L, 3L, 6L, 1L, 
                                                                                                                                                                                                                                                                                                                                                             1L, 3L, 3L, 5L, 5L, 3L, 5L, 5L, 5L, 5L, 3L, 5L, 3L, 1L, 5L, 1L, 
                                                                                                                                                                                                                                                                                                                                                             5L, 5L, 3L, 5L, 5L, 5L, 5L, 6L, 1L, 5L, 4L, 5L, 1L, 1L, 5L, 1L, 
                                                                                                                                                                                                                                                                                                                                                             5L, 5L, 5L, 1L, 1L, 3L, 5L, 3L, 5L, 6L, 6L, 6L, 1L, 6L, 1L, 5L, 
                                                                                                                                                                                                                                                                                                                                                             5L, 3L, 1L, 3L, 1L, 5L, 3L, 3L, 3L, 5L, 3L, 1L, 5L, 3L, 5L, 5L, 
                                                                                                                                                                                                                                                                                                                                                             5L, 6L), .Label = c("0", "1", "2", "3", "4", "5", "6"), class = "factor")), row.names = c(485723L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       252377L, 402164L, 866501L, 473302L, 141482L, 634829L, 538734L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       676824L, 321605L, 134718L, 831751L, 771455L, 91084L, 882874L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       125729L, 39615L, 144677L, 776352L, 361353L, 267097L, 82363L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       232166L, 1029892L, 940797L, 404645L, 201840L, 677648L, 732311L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       625444L, 907301L, 239189L, 264560L, 875560L, 200124L, 412518L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       551276L, 423981L, 1968L, 774316L, 16254L, 356605L, 371659L, 880595L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       327133L, 191350L, 642680L, 398031L, 71792L, 270565L, 630174L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       527505L, 1034597L, 474552L, 576251L, 123809L, 907733L, 126922L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       270776L, 861598L, 314906L, 427173L, 250436L, 552483L, 326319L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       586923L, 848197L, 219453L, 894751L, 917982L, 19339L, 505489L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       342574L, 539108L, 419799L, 809494L, 720683L, 49653L, 76307L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       295730L, 613241L, 250587L, 583849L, 826410L, 477638L, 663683L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       84433L, 946764L, 159166L, 1048094L, 908709L, 231996L, 706027L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                       789714L, 1036271L, 266501L, 442407L, 715768L, 13007L, 1015893L
                                                                                                                                                                                                                                                                                                                                                             ), class = c("data.table", "data.frame"))




datt <- df %>%
  filter(!is.na(QtySold),  !is.na(PRODUCT_SUB_LINE_DESCR), QtySold > 0, Sales > 0, MarginDollars > 0) %>%
  map_if(is.factor, as.character) %>%
  as_tibble() %>%
  group_by(CUSTOMER_NUMBER,PRODUCT_SUB_LINE_DESCR,Cust_Segments) %>%
  summarise(Quants = sum(QtySold)) %>%
  ungroup() %>%
  spread(PRODUCT_SUB_LINE_DESCR,Quants,fill=0) %>%
  mutate(Total_Orders = `PRIVATE LABEL` + SUNDRY + Handpieces,
         PL_Order_Percentage= round((`PRIVATE LABEL` / Total_Orders) * 100))

datt_sales <- df %>%
  select(QtySold, PRODUCT_SUB_LINE_DESCR, Sales, MarginDollars, CUSTOMER_NUMBER, Cust_Segments) %>%
  filter(!is.na(QtySold),  !is.na(PRODUCT_SUB_LINE_DESCR), QtySold > 0, Sales > 0, MarginDollars > 0) %>%
  map_if(is.factor, as.character) %>%
  as_tibble() %>%
  group_by(CUSTOMER_NUMBER,PRODUCT_SUB_LINE_DESCR) %>%
  summarise(My_Sales = sum(Sales)) %>%
  ungroup () %>%
  spread(PRODUCT_SUB_LINE_DESCR,My_Sales,fill=0) %>%
  mutate(Total_Sales = `PRIVATE LABEL` + SUNDRY + Handpieces)

datt_merge = merge(datt, datt_sales, by = "CUSTOMER_NUMBER", all = TRUE)
new_datt_merge = 
  datt_merge %>%
  select(CUSTOMER_NUMBER, Cust_Segments,Handpieces.x,`PRIVATE LABEL.x`,SUNDRY.x,Total_Orders, Total_Sales,PL_Order_Percentage) %>%
  mutate(category = cut(PL_Order_Percentage,breaks = c(0,1,11,21,31,41,51,61,71,81,91,100,Inf), 
                        labels = c('0%','1%-10%','11%-20%',
                                   '21%-30%','31%-40%','41%-50%',
                                   '51%-60%','61%-70%','71%-80%','81%-90%','91%-99%','100%'),include.lowest = T,right = F)) %>%
  group_by(category) %>%
  summarise(count=n(), Total_Sales = sum(Total_Sales)) %>% 
  mutate(percent = round(count/sum(count)*100),
         Percentage= paste0(round(count/sum(count)*100),'%'),
         Sales_Amount = Total_Sales,
         Sale_Dollars = paste0("$",comma(Total_Sales)))

newdf <- new_datt_merge %>%
  gather(Bucket, value, -category) %>%
  filter(Bucket %in% c("Sales_Amount","percent"))

class(newdf$value)
#> [1] "character"

Created on 2018-07-17 by the reprex package (v0.2.0.9000).


#11

Mara:
Yup.
I am seeing it right now on my end.
Can I do this? I meant it is throwing an error right now but is this a right approach?

newdf <- new_datt_merge %>%
  gather(Bucket, value, -category) %>% 
  filter(Bucket %in% c("Sales_Amount","percent"))  %>%
 map_if(is.character, as.numeric)

#12

To make this much more minimal, look at the data frame you're actually working with at the end.

If you actually convert those to numeric, you're comparing their four wildly different values, so facetting doesn't turn out well.

library(ggplot2)
newdf <- data.frame(stringsAsFactors=FALSE,
           category = c("0%", "100%", "0%", "100%"),
           Bucket = c("percent", "percent", "Sales_Amount", "Sales_Amount"),
           value = c(54, 46, 8983.23, 2671.65)
)

newdf
#>   category       Bucket   value
#> 1       0%      percent   54.00
#> 2     100%      percent   46.00
#> 3       0% Sales_Amount 8983.23
#> 4     100% Sales_Amount 2671.65

ggplot(newdf)+
  geom_bar(aes(x=category,y=value, fill = category),stat='identity') + 
  theme(legend.position="none") +
  labs(x ="",y="")+
  facet_grid(Bucket~., scales = "free", space = "free") +
  geom_text(aes(x=category,y=value, label = (value)),vjust=-0.5) +
  theme(legend.position="none",axis.text.y = element_blank(),  axis.ticks = element_blank(),
        axis.line.y = element_blank())

Created on 2018-07-17 by the reprex package (v0.2.0.9000).


#13

Mara:
Yeah, I agree.

newdf <- new_datt_merge %>%
  gather(Bucket, value, -category) %>% 
  filter(Bucket %in% c("Sales_Amount","percent"))
newdf$value = as.numeric((as.character(newdf$value)))

newdf %>%  ggplot()+
  geom_bar(aes(x=category,y=value, fill = Bucket),stat='identity') + 
  theme(legend.position="none") +
  labs(x ="",y="")+
  facet_grid(Bucket~., scales = "free", space = "free") +
  geom_text(aes(x=category,y=value, label = comma(value)),vjust=-0.5) +
  theme(legend.position="none",axis.text.y = element_blank(),  axis.ticks = element_blank(),
        axis.line.y = element_blank())
View(newdf)
str(new_datt_merge)

I meant this gave me the right result but it does not give me two plots in the same grid.
This bumps me out.


#14

You've loaded cowplot, but you're not actually using its functions (e.g. plot_grid()) which would allow you to put two unrelated plots above each other easily.

See the vignette for how to use it.
https://cran.r-project.org/web/packages/cowplot/vignettes/plot_grid.html


#15

Mara:
yeah, but I wanna learn how to use facet_grid().


#16

Then you've fallen into an XY problem for this case, I think.


#17

Mara:
How can I rephrase my post then?
There is an original question and then there are follow-up questions...


#18

That's not about phrasing, it's that you want to do X to solve Y. Given how you've wrangled the data, using facet_grid() is not the solution I would recommend.


#19

Mara:
Thank you.
I edited my post anyway, to make it clearer.

I apologize if I did waste your time.
By no means, that has never been my intention.

For other members, I apologize as well!


#20

See if removing the space = "free" part of the facet_grid call helps get you closer to what you are after, but as previously mentioned (also by mara) facets are designed for data with common units.