Hello,
I have a sample data set.
Here is the dataset
dput(sample_query[100:235,])
Summary
structure(list(ITEM_CATEGORY_KEY = c(90090L, 90090L, 90090L,
90090L, 58095L, 58095L, 58095L, 90090L, 90090L, 90090L, 16030L,
16030L, 16030L, 16030L, 16030L, 16030L, 16030L, 16030L, 16030L,
16030L, 89010L, 29030L, 29030L, 62070L, 62070L, 31010L, 31010L,
31010L, 31010L, 31010L, 31010L, 18060L, 29030L, 29030L, 29030L,
29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 29030L,
29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 29030L,
29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 29030L,
29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 29030L,
29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 29030L,
29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 29030L, 32082L,
175L, 62013L, 62013L, 62013L, 62013L, 15010L, 15010L, 62072L,
62072L, 62072L, 62072L, 16030L, 62009L, 62009L, 62009L, 62009L,
62009L), ITEM_NUMBER = c(70107490L, 70107490L, 70107532L, 70107532L,
70107573L, 70107573L, 70107573L, 70108035L, 70108035L, 70108035L,
70110700L, 70110700L, 70110700L, 70110700L, 70113910L, 70113910L,
70113910L, 70113910L, 70113910L, 70114207L, 70115436L, 70115469L,
70115477L, 70115501L, 70115501L, 70121681L, 70121681L, 70121699L,
70121699L, 70121707L, 70121715L, 70121848L, 70123661L, 70123661L,
70123661L, 70123661L, 70123661L, 70123661L, 70123661L, 70123661L,
70123661L, 70123661L, 70123661L, 70123661L, 70123661L, 70123661L,
70123661L, 70123661L, 70123661L, 70123661L, 70123661L, 70123661L,
70123661L, 70123661L, 70123661L, 70123661L, 70123661L, 70123661L,
70123661L, 70123661L, 70123661L, 70123661L, 70123661L, 70123661L,
70123661L, 70123661L, 70123661L, 70123661L, 70123661L, 70123661L,
70123661L, 70123661L, 70123661L, 70123661L, 70123661L, 70123661L,
70123661L, 70123661L, 70123661L, 70123661L, 70123661L, 70123661L,
70123695L, 70132530L, 70154179L, 70154179L, 70154179L, 70154179L,
70179366L, 70179556L, 70182238L, 70182238L, 70182238L, 70182238L,
70183509L, 70184283L, 70184283L, 70184531L, 70184531L, 70184531L
), PRODUCT_SUB_LINE_DESCR = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Handpieces",
"SUNDRY"), class = "factor"), MAJOR_CATEGORY_KEY = structure(c(8L,
8L, 8L, 8L, 7L, 7L, 7L, 8L, 8L, 8L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 9L, 6L, 6L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 8L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 5L, 4L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 1L,
1L, 1L, 1L), .Label = c("ASE ", "CBL ", "CMP ", "HND ", "IMP ",
"OTH ", "PRE ", "SME ", "XRY "), class = "factor"), CUSTOMER_NUMBER = c(760378672L,
763200488L, 743466256L, 760431665L, 368200186L, 744200059L, 744200187L,
236334505L, 448200070L, 450200153L, 240128689L, 240442932L, 648386723L,
766200236L, 301210923L, 454201248L, 636200291L, 703312261L, 768194873L,
744811535L, 763331588L, 648498142L, 648498142L, 360200113L, 448742985L,
301241419L, 454515450L, 438386467L, 728651885L, 438215698L, 728651885L,
422884787L, 209200424L, 212200394L, 212420716L, 230783963L, 236598300L,
236811995L, 240290762L, 240681719L, 301542511L, 304287427L, 304612573L,
328340042L, 328783124L, 370168236L, 373200436L, 373787674L, 375200789L,
375527560L, 402127513L, 402200489L, 408487877L, 410200886L, 422104057L,
422445049L, 422669086L, 438200078L, 444720366L, 444861385L, 454200978L,
610105238L, 610109064L, 610200244L, 610253710L, 632388190L, 642200435L,
642835770L, 648321934L, 704242795L, 710200669L, 728439152L, 728465386L,
728526403L, 728798108L, 743605614L, 743831451L, 744326981L, 745200257L,
760581324L, 766668517L, 768115598L, 212200324L, 763200225L, 240837606L,
345306881L, 656450578L, 744699107L, 341200191L, 212454498L, 230200169L,
236829865L, 374199006L, 768611734L, 408107714L, 212200620L, 743542749L,
212200569L, 212405353L, 220281275L), CUST_BRANCH_DESCR = structure(c(32L,
35L, 7L, 32L, 3L, 20L, 20L, 28L, 34L, 25L, 29L, 29L, 19L, 36L,
5L, 37L, 11L, 2L, 4L, 20L, 35L, 19L, 19L, 43L, 34L, 5L, 37L,
17L, 27L, 17L, 27L, 23L, 41L, 14L, 14L, 22L, 28L, 28L, 29L, 29L,
5L, 16L, 16L, 21L, 21L, 39L, 40L, 40L, 31L, 31L, 1L, 1L, 38L,
13L, 23L, 23L, 23L, 17L, 33L, 33L, 37L, 8L, 8L, 8L, 8L, 9L, 15L,
15L, 19L, 6L, 12L, 27L, 27L, 27L, 27L, 7L, 7L, 20L, 10L, 32L,
36L, 4L, 14L, 35L, 29L, 30L, 24L, 20L, 26L, 14L, 22L, 28L, 42L,
4L, 38L, 14L, 7L, 14L, 14L, 18L), .Label = c("ALBUQUERQUE ",
"ATLANTA ", "AUSTIN ", "BALTIMORE",
"BIRMINGHAM ", "BOSTON", "CHARLESTON ",
"CHICAGO ", "CLEVELAND", "COLUMBIA", "COLUMBUS",
"CONNECTICUT", "DENVER ", "DES MOINES ",
"DETROIT", "DFW ", "EAST BAY ",
"FARGO ", "GRAND RAPIDS", "GREENVILLE ",
"HOUSTON ", "KANSAS CITY ", "LOS ANGELES ",
"LOUISVILLE", "MEDFORD ", "MEMPHIS ",
"METRO NY/NJ", "MILWAUKEE ", "MINNESOTA ",
"NEW ORLEANS ", "ORLANDO ", "PHILADELPHIA",
"PHOENIX ", "PORTLAND ", "RALEIGH",
"ROCHESTER", "SACRAMENTO ", "SALT LAKE CITY ",
"SAN ANTONIO ", "SOUTH FLORIDA ", "ST LOUIS ",
"TAMPA ", "WICHITA "), class = "factor"),
PROGRAM_LEVEL_DESCR = structure(c(7L, 7L, 6L, 4L, 6L, 6L,
7L, 7L, 2L, 6L, 6L, 7L, 8L, 7L, 4L, 3L, 1L, 4L, 2L, 5L, 7L,
4L, 4L, 7L, 7L, 6L, 7L, 1L, 7L, 4L, 7L, 4L, 8L, 7L, 7L, 7L,
1L, 4L, 7L, 7L, 4L, 1L, 8L, 6L, 4L, 8L, 4L, 2L, 1L, 8L, 6L,
1L, 7L, 8L, 2L, 2L, 6L, 1L, 8L, 5L, 1L, 7L, 7L, 8L, 4L, 7L,
4L, 7L, 6L, 4L, 6L, 1L, 7L, 7L, 3L, 7L, 4L, 1L, 7L, 2L, 4L,
4L, 8L, 6L, 1L, 6L, 4L, 1L, 3L, 1L, 7L, 2L, 4L, 6L, 8L, 7L,
7L, 2L, 7L, 7L), .Label = c("Club", "Diamond", "Enrollment",
"Gold", "Institutional", "No Program", "Platinum", "Silver"
), class = "factor"), CUST_STATE_KEY = structure(c(25L, 18L,
6L, 20L, 27L, 18L, 18L, 30L, 24L, 3L, 9L, 16L, 15L, 22L,
1L, 3L, 23L, 6L, 14L, 18L, 18L, 15L, 15L, 10L, 24L, 1L, 3L,
3L, 22L, 3L, 22L, 3L, 17L, 7L, 7L, 17L, 30L, 30L, 16L, 16L,
1L, 27L, 27L, 27L, 27L, 27L, 5L, 5L, 5L, 5L, 21L, 21L, 8L,
4L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 9L, 9L, 9L, 9L, 23L, 15L,
15L, 15L, 13L, 29L, 20L, 22L, 20L, 20L, 26L, 26L, 26L, 26L,
25L, 22L, 31L, 7L, 18L, 7L, 12L, 11L, 26L, 17L, 7L, 17L,
30L, 5L, 9L, 28L, 7L, 26L, 7L, 7L, 19L), .Label = c("AL ",
"AZ ", "CA ", "CO ", "FL ", "GA ", "IA ", "ID ", "IL ", "KS ",
"KY ", "LA ", "MA ", "MD ", "MI ", "MN ", "MO ", "NC ", "ND ",
"NJ ", "NM ", "NY ", "OH ", "OR ", "PA ", "SC ", "TX ", "UT ",
"VT ", "WI ", "WV "), class = "factor"), CUST_REGION_DESCR = structure(c(3L,
6L, 6L, 3L, 5L, 6L, 6L, 2L, 1L, 1L, 2L, 2L, 4L, 3L, 5L, 7L,
4L, 6L, 3L, 6L, 6L, 4L, 4L, 5L, 1L, 5L, 7L, 7L, 3L, 7L, 3L,
7L, 2L, 2L, 2L, 5L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L,
6L, 6L, 6L, 6L, 5L, 5L, 1L, 1L, 7L, 7L, 7L, 7L, 1L, 1L, 7L,
2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 6L,
6L, 6L, 6L, 3L, 3L, 3L, 2L, 6L, 2L, 5L, 4L, 6L, 6L, 2L, 5L,
2L, 6L, 3L, 1L, 2L, 6L, 2L, 2L, 2L), .Label = c("MOUNTAIN WEST REGION",
"NORTH CENTRAL REGION", "NORTH EAST REGION ", "OHIO VALLEY REGION ",
"SOUTH CENTRAL REGION ", "SOUTH EAST REGION ",
"WESTERN REGION "), class = "factor"), CUST_CITY = structure(c(13L,
94L, 75L, 83L, 70L, 33L, 6L, 25L, 9L, 3L, 23L, 66L, 39L,
88L, 34L, 16L, 92L, 7L, 32L, 6L, 29L, 21L, 21L, 50L, 24L,
11L, 73L, 46L, 91L, 77L, 91L, 74L, 90L, 87L, 10L, 42L, 51L,
52L, 64L, 27L, 20L, 28L, 5L, 36L, 82L, 18L, 80L, 54L, 40L,
48L, 1L, 71L, 43L, 31L, 45L, 84L, 69L, 65L, 53L, 93L, 72L,
76L, 76L, 76L, 76L, 8L, 55L, 79L, 67L, 61L, 4L, 62L, 59L,
15L, 86L, 63L, 57L, 78L, 60L, 44L, 14L, 49L, 17L, 68L, 89L,
19L, 47L, 30L, 58L, 38L, 41L, 12L, 81L, 22L, 35L, 85L, 56L,
2L, 37L, 26L), .Label = c("ALBUQUERQUE ", "Ames ",
"Arcata ", "Arlington ", "ARLINGTON ",
"Asheville ", "ATHENS ", "AVON ",
"Beaverton ", "BETTENDORF ", "BIRMINGHAM", "BROOKFIELD ",
"BRYN MAWR ", "BUFFALO ", "CLARK ",
"Clovis ", "Coralville ", "CORPUS CHRISTI ",
"DESTREHAN ", "DOTHAN ", "East Lansing ",
"Effingham ", "EFFINGHAM ", "EUGENE ",
"EVANSVILLE ", "FARGO ", "Farmington ",
"Frisco ", "GREENSBORO ", "Greenville ",
"Greenwood Villag", "HAGERSTOWN ", "Hayesville ",
"HAZEL GREEN ", "HIGHLAND ", "Houston ",
"HUXLEY ", "INDEPENDENCE ", "Jackson ",
"Jacksonville ", "Joplin ", "KANSAS CITY ",
"KETCHUM ", "LANCASTER ", "LONG BEACH ",
"Los Altos ", "LOUISVILLE ", "MAITLAND ",
"Martinsburg ", "McPherson ", "Menasha ",
"MENOMONEE FALLS ", "MESA ", "MIAMI ",
"MIDLAND ", "MONCKS CORNER ", "MYRTLE BEACH ",
"New Madrid", "New York ", "Newberry ", "Newton ",
"NORTH BERGEN ", "North Charleston", "North Oaks ",
"Oakland ", "OSSEO ", "Petoskey ",
"Raleigh ", "REDONDO BEACH ", "Round Rock ",
"Ruidoso ", "Sacramento ", "SACRAMENTO ",
"SANTA PAULA ", "SAVANNAH ", "Schaumburg ",
"SOQUEL ", "SPARTANBURG", "ST CLAIR SHORES ", "Stuart ",
"Tallahassee ", "THE WOODLANDS ", "TOMS RIVER ",
"TORRANCE ", "Van Horne ", "VERONA ",
"Waterloo ", "Watertown ", "WAUKON ",
"Webster Groves ", "WEST HARRISON ", "WESTERVILLE ",
"WINSLOW ", "Winston Salem "), class = "factor"),
COMM_SUB_LINE_KEY = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("HP", "SD"), class = "factor"), COMM_SUB_LINE_DESCR = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Handpieces",
"SUNDRIES"), class = "factor"), Sales = c(209.97, -76.15,
275.6, 138.7, 226, 115, 210.7, 29, 29, 46.32, 159.86, 441.3,
209.62, 209.62, 129.5, 294.9, 106.51, 147.45, 294.9, 49.4,
372, 36.76, 54, 189.3, 62.8, 166.95, 333.9, 135.21, 166.95,
166.95, 166.95, 46.95, 72.8, 59.95, 62.09, 56.76, 64.57,
68.16, 71.75, 66.25, 136.32, 57.13, 71.75, 145.6, 65.34,
143.5, 133.96, 232.96, 61.88, 56.95, 72.8, 72.8, 143.5, 72.8,
137.5, 123.76, 55.4, 122.34, -72.8, 71.75, 122.66, 102, 102,
51, 51, 71.75, 68.24, 62.99, 32.25, 287, 54.11, 116.16, 126.32,
63.95, 0, 71.75, 125.7, 194.97, 71.75, 123.76, 68, 71.75,
29.65, 1350, 7.35, 22.2, 13, 6.66, 174.34, 161.07, 153.28,
206, 41.2, 113.01, 133.05, 25.64, 243, 26.3, 131.5, 71.52
), QtySold = c(3L, -1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 6L, 2L, 1L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 2L, 1L, 2L, 2L, 4L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,
1L, 2L, -1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 4L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 3L, 1L, 2L, 1L, 1L, 1L, 1L, 1L,
3L, 2L, 1L, 1L, 1L, 4L, 5L, 1L, 3L, 1L, 2L, 30L, 2L, 10L,
6L), MFGCOST = c(134.55, -44.85, 162.5, 81.25, 136, 68, 136,
18.85, 18.85, 37.7, 132.4, 264.8, 132.4, 132.4, 88.48, 176.96,
88.48, 88.48, 176.96, 38.19, 225.75, 23.86, 36.5, 93.6, 31.2,
108.18, 216.36, 108.18, 108.18, 108.18, 108.18, 30.43, 43.7,
43.7, 43.7, 43.7, 43.7, 43.7, 43.7, 43.7, 87.4, 43.7, 43.7,
87.4, 43.7, 87.4, 87.4, 174.8, 43.7, 43.7, 43.7, 43.7, 87.4,
43.7, 87.4, 87.4, 43.7, 87.4, -43.7, 43.7, 87.4, 87.4, 87.4,
43.7, 43.7, 43.7, 43.7, 43.7, 43.7, 174.8, 43.7, 87.4, 87.4,
43.7, 43.7, 43.7, 87.4, 131.1, 43.7, 87.4, 43.7, 43.7, 17.3,
941.85, 3.81, 11.43, 7.62, 3.81, 113.69, 113.69, 90.64, 113.3,
22.66, 67.98, 76.6, 14.9, 223.5, 14.9, 74.5, 44.7), MarginDollars = c(72.72,
-30.4, 109.84, 55.82, 87.28, 45.64, 71.98, 9.77, 9.77, 7.86,
24.81, 171.2, 74.57, 74.57, 39.25, 114.4, 16.26, 57.2, 114.4,
10.45, 141.73, 12.42, 16.78, 93.84, 30.98, 56.61, 113.22,
24.87, 56.61, 56.61, 56.61, 15.91, 28.67, 15.82, 17.96, 12.63,
20.44, 24.03, 27.62, 22.12, 48.06, 13, 27.62, 57.34, 21.21,
55.24, 45.7, 56.44, 17.75, 12.82, 28.67, 28.67, 55.24, 28.67,
49.24, 35.5, 11.27, 34.08, -28.67, 27.62, 34.4, 13.74, 13.74,
6.87, 6.87, 27.62, 24.11, 18.86, -11.88, 110.48, 9.98, 27.9,
38.06, 19.82, -44.13, 27.62, 37.44, 62.58, 27.62, 35.5, 23.87,
27.62, 12, 389.31, 3.46, 10.53, 5.22, 2.77, 58.38, 45.11,
61.08, 90.75, 18.15, 43.86, 54.92, 10.44, 15, 11.1, 55.5,
25.92)), row.names = c(NA, 100L), class = "data.frame")
sample = data %>%
filter(QtySold < 0) %>%
group_by(PRODUCT_SUB_LINE_DESCR,MAJOR_CATEGORY_KEY) %>%
summarise(returns = sum(QtySold)) %>%
spread(PRODUCT_SUB_LINE_DESCR,returns, fill=0) %>%
mutate(total_returns = `PRIVATE LABEL` + SUNDRY + Handpieces,
PercentageReturn_PL = round(`PRIVATE LABEL`/ total_returns*100,1)) %>%
ggplot() +
geom_bar(aes(x=PRODUCT_SUB_LINE_DESCR,y=total_returns,
fill = PRODUCT_SUB_LINE_DESCR),stat='identity', show.legend = F) +
geom_text(aes(x=PRODUCT_SUB_LINE_DESCR,y=total_returns,label=PercentageReturn_PL),vjust=-0.5)
facet_wrap(~MAJOR_CATEGORY_KEY) + theme(axis.text=element_text(size =8))
Error: Aesthetics must be either length 1 or the same as the data (24): x, y, fill
My scenario:
- I have a data set of three products: Sundry, Handpieces and Private Label.
- I filtered the data set based on (QtySold < 0) category and group the new one based on "PRODUCT_SUB_LINE_DESCR" and
"MAJOR_CATEGORY_KEY". - I want to see the percentage of returned items which belong to Private Label category. My code has a mutate command with a new column "PercentageReturn_PL".
- Finally, when I am done with the math, I want to plot this whole thing with the MAJOR_CATEGORY_KEY on the Y-axis and the stacked barplot show the segments of percentage break down for three products within one MAJOR_CATEGORY_KEY.
Issue:
Error: Aesthetics must be either length 1 or the same as the data ** (24): x, y, fill
The error occurs right after the geom_bar()!
How can I fix this error?
Thank you!