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!