Ggplot troubleshoot: Error: Aesthetics must be either length 1 or the same as the data (24): x, y, fill

ggplot2
rstudio

#1

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!


#2

@tbradley
Hello Tyler,
Did you comment or do something on this post?
I am curious because I got a notification with your name next to it and when I clicked on the notification, nothing showed.

Thanks!


#3

Hi, it looks like your code was not formatted correctly to make it easy to read for people trying to help you. Formatting code allows for people to more easily identify where issues may be occuring, and makes it easier to read, in general. I have edited you post to format the code properly.

In the future please put code that is inline (such as a function name, like mutate or filter) inside of backticks (`mutate`) and chunks of code can be put between sets of three backticks:

```
example <- foo %>%
  filter(a == 1)
```

This process can be done automatically by highlighting your code, either inline or in a chunk, ad clicking the </> button on the toolbar of the reply window!

This will help keep our community tidy and help you get the help you are looking for!

For more information, please take a look at the community's FAQ on formating code


#4

@tbradley
Any thought on the issue?


#5

The error is telling you that one of the aesthetics that you are providing is a different length than the others.

I tried to run your code with the data you provided and it looks like the data provided returns an empty tibble (zero rows) when filter(QtySold < 0) is used and when I removed that and tried to run it, it looks like there are no Handpieces in the dataset because the column is not created with your spread call.

Since I can't reproduce your code with the data provided, it is hard to tell you exactly where the error is occuring. When I run into this error, I typically comment out one portion of the ggplot code at a time (i.e. first comment the geom_bar function and see if you still get the error and then do the same for geom_text) to see which specific function is causing the error.


#6

@tbradley
I posted a new sample.
Please take a look at my original post.
I need help!

Thanks!


#7

The sample you provided now only contains "SUNDRY" after the summarize step. It may be better for you to subset your data using filter so you can better control what the output will contain rather than just simply subsetting by index. Also, if you use the reprex package then you will know whether the data you are providing have all the necessary data included.

Please see here about creating a reprex


#8

@tbradley

Summary
structure(list(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, 10L, 8L, 10L, 
20L, 10L, 5L, 1L, 3L, 30L, 20L, 10L, 10L, 90L, 10L, 12L, 10L, 
15L, 10L, 10L, 20L, 10L, 10L, 32L, 10L, 10L, 20L, 1L, 2L, 1L, 
2L, 2L, 5L, 2L, 1L, 3L, 3L, 1L, 1L, 2L, 4L, 2L, 4L, 1L, 1L, 2L, 
2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 3L, 
1L, 4L, 2L, 1L, 4L, 4L, 5L, 1L, 3L, 2L, 2L, 4L, 1L, 3L, 3L, 1L, 
3L, 6L, 7L, 1L, 3L, 7L, 2L, 1L, 4L, 2L, 3L, 3L, 5L, 2L, 1L, 1L, 
4L, 6L, 1L, 1L, 1L, 1L, 2L, 1L, 4L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 4L, 1L, 3L, 2L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 
5L, 1L, 1L, 1L, 1L, 3L, 1L, 2L, 1L, 2L, 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 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Handpieces", 
"PRIVATE LABEL", "SUNDRY"), class = "factor"), MAJOR_CATEGORY_KEY = structure(c(23L, 
23L, 23L, 23L, 21L, 21L, 21L, 23L, 23L, 23L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 25L, 19L, 19L, 4L, 4L, 9L, 9L, 
9L, 9L, 9L, 9L, 23L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 17L, 16L, 4L, 4L, 4L, 4L, 11L, 11L, 4L, 4L, 4L, 
4L, 11L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 21L, 12L, 12L, 12L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 13L, 13L, 7L, 18L, 7L, 
19L, 19L, 19L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 13L, 13L, 11L, 11L, 6L, 12L, 10L, 10L, 
4L, 4L, 4L, 4L, 4L, 4L, 10L, 19L, 19L, 19L, 19L, 19L, 4L, 4L, 
11L, 11L, 11L, 11L, 11L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 11L, 16L, 16L, 16L, 16L, 16L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 16L, 16L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 21L, 4L, 
4L, 4L, 19L, 19L, 19L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 17L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 12L, 12L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 13L, 
4L, 4L, 4L, 4L, 4L, 4L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 18L, 
21L, 21L, 18L, 18L, 18L, 18L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 19L, 19L, 18L, 18L, 4L, 11L), .Label = c("AIR ", 
"AML ", "ANS ", "ASE ", "ASP ", "B&D ", "BLE ", "C&P ", "CBL ", 
"CEM ", "CMP ", "CRN ", "END ", "FNP ", "GYP ", "HND ", "IMP ", 
"INS ", "OTH ", "P&P ", "PRE ", "RTC ", "SME ", "UCL ", "XRY "
), class = "factor")), row.names = c(NA, 500L), class = "data.frame")

Here is the new repex.


#9

Ok. So that is not quite a reprex. I recommend you look through some of the references in that link for building a reprex in the future. Nevertheless, this dataset did contain all of the columns that you call in your mutate call. However, I get a different error when I get the ggplot portion of your pipeline. The error I am getting has to do with the PRODUCT_SUB_LINE_DESCR column not existing because you spread that column with your spread step. If you want to use that as your x axis then you will need to gather it back using the gather function or pick a different column to use as your x axis


#11

@tbradley
New error

> sample = newdf %>% 
+   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=returns, 
+                fill = PRODUCT_SUB_LINE_DESCR),stat='identity', show.legend = F) +
+   geom_text(aes(x=PRODUCT_SUB_LINE_DESCR,y=returns,label=PercentageReturn_PL),vjust=-0.5) +
+   facet_wrap(~MAJOR_CATEGORY_KEY) + theme(axis.text=element_text(size =8))
> sample
Error in FUN(X[[i]], ...) : object 'returns' not found 

#12

This error is the same as what I mention in my previous post. You are using the spread command with the PRODUCT_SUB_LINE_DESCR and returns columns as your key and fill arguments, respectively. This will remove these columns from the dataset as the dataset has been transformed. You can not use either of them in your ggplot functions (since they are no longer in your dataset) unless you use gather to return them.

Also, it is discouraged to @name reference a user who has not engaged in a thread on their own. Please see the community faq about @name usage


#13

@tbradley
So before call "ggplot", i need to call

gather(PRODUCT_SUB_LINE_DESCR)

?


#14

if you want to have the PRODUCT_SUB_LINE_DESCR and the returns column then you would do this:

gather(key = PRODUCT_SUB_LINE_DESCR, value = returns, Handpieces:SUNDRY)

#15

@tbradley
All of my other codes work.
Only for this visualization does not work.
I am really confused.


#16

@tbradley

newdf = subset(data, select = c(QtySold, PRODUCT_SUB_LINE_DESCR, MAJOR_CATEGORY_KEY))
View(newdf)


sample = newdf %>% 
  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)) 


plot_sample = sample %>%
              gather(key = PRODUCT_SUB_LINE_DESCR, value = returns, Handpieces:SUNDRY) %>%
              summarise(count=n()) %>%
              mutate(percent= paste0(round(count/sum(count)*100,1),'%')) 

plot_sample %>%
  ggplot() + 
  geom_bar(aes(x=PRODUCT_SUB_LINE_DESCR,y=returns, fill = PRODUCT_SUB_LINE_DESCR),stat='identity')+
  geom_text(aes(x=PRODUCT_SUB_LINE_DESCR,y=returns,label=percent),vjust=-0.5)
> plot_sample %>%
+   ggplot() + 
+   geom_bar(aes(x=PRODUCT_SUB_LINE_DESCR,y=returns, fill = PRODUCT_SUB_LINE_DESCR),stat='identity')+
+   geom_text(aes(x=PRODUCT_SUB_LINE_DESCR,y=returns,label=percent),vjust=-0.5)
Error in FUN(X[[i]], ...) : object 'returns' not found

I am sorry Tyler.
I did not mean to complain.


#17

Now you are doing something different then originally posted and the summarize call without any grouping variables is causing the columns used in your ggplot call to be removed. You need to add a group_by() call before summarize to keep your desired columns.

And no need to apologize!


#18

@tbradley
I still have the same error message..

> plot_sample = sample %>%
+               group_by(PRODUCT_SUB_LINE_DESCR) %>%
+               summarise(count=n()) %>%
+               mutate(percent= paste0(round(count/sum(count)*100,1),'%'))
Error in grouped_df_impl(data, unname(vars), drop) : 
  Column `PRODUCT_SUB_LINE_DESCR` is unknown 

I am just splitting a whole chunk of code into small pieces.


#19

what does glimpse(sample) give you?


#20

It looks like you got an answer to your question on Stack Overflow.

FYI, it's good practice to let folks know when you've cross-posted a question on multiple websites. See the FAQ for this site for more details: FAQ: Is it OK if I cross-post?