Home > Software engineering >  Barplot with countings in ggplot with facet_wrap and other factor
Barplot with countings in ggplot with facet_wrap and other factor

Time:12-21

Let's start with this dataframe:

x <- structure(list(Tooth = c("Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Incisor", "Incisor", "Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Incisor", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Incisor", "Incisor", 
"Incisor", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar", "Molar", "Molar", "Molar", "Molar"), Decil = c("DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", "DC1", 
"DC1", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", "DC2", 
"DC2", "DC2", "DC2", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", "DC3", 
"DC3", "DC3", "DC3", "DC3", "DC3"), Real = c(4, 5, 6, 8, 11, 
11, 11, 6, 8, 5, 9, 10, 6, 11, 15, 13, 17, 6, 7, 9, 9, 9, 10, 
10, 13, 15, 7, 8, 9, 9, 9, 9, 9, 10, 11, 11, 11, 11, 12, 7, 9, 
9, 9, 11, 7, 12, 13, 12, 13, 10, 10, 10, 18, 12, 10, 12, 12, 
11, 13, 9, 11, 17, 4, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 
10, 12, 7, 8, 11, 5, 9, 7, 9, 8, 7, 5, 7, 7, 6, 7, 6, 5, 13, 
9, 8, 9, 10, 9, 11, 10, 11, 7, 12, 6, 7, 9, 9, 11, 5, 8, 7, 4, 
6, 5, 5, 7, 5, 10, 7, 4, 6, 6, 5, 8, 12, 6, 8, 5, 6, 6, 6, 5, 
5, 7, 9, 3, 4, 5, 6, 8, 8, 10, 10, 7, 11, 11, 12, 8, 10, 5, 8, 
5, 9, 7, 6, 13, 16, 13, 16, 7, 8, 13, 12, 9, 13, 13, 15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 
1L, 0L, 2L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 2L, 0L, 2L, 3L, 2L, 0L, 
1L, 1L, 0L, 1L, 1L, 1L, 2L, 2L, 1L, 0L, 0L, 2L, 1L, 1L, 3L, 2L, 
1L, 1L, 1L, 1L, 3L, 2L, 0L, 2L, 1L, 2L, 3L, 1L, 1L, 2L, 0L, 0L, 
1L, 1L, 2L, 1L, 1L, 2L, 3L, 3L, 2L, 3L, 1L, 2L, 3L, 0L, 1L, 1L, 
2L, 1L, 1L, 1L, 1L, 0L, 0L, 2L, 3L, 4L, 4L, 0L, 2L, 2L, 0L, 2L, 
4L, 1L, 3L, 2L, 4L, 3L, 0L, 0L, 2L, 0L, 1L, 0L, 1L, 2L, 3L, 3L, 
0L, 3L, 1L, 1L, 1L, 2L, 3L, 1L, 2L, 1L, 2L, 4L, 1L, 0L, 1L, 1L, 
2L, 3L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 2L, 0L, 0L, 1L, 
1L, 2L, 1L, 2L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 2L, 1L, 0L, 0L, 
1L, 0L, 2L, 1L, 2L, 1L, 0L, 0L, 3L, 1L, 2L, 0L, 0L, 0L, 1L, 3L, 
1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 2L, 2L, 0L, 2L, 1L, 
1L, 1L, 1L, 2L, 1L, 0L, 1L, 1L, 2L, 1L, 4L, 0L, 1L, 1L, 0L, 1L, 
2L, 4L, 1L, 2L, 2L, 0L, 3L, 2L, 4L, 4L, 3L, 4L, 4L, 2L, 2L, 1L, 
2L, 1L, 4L, 5L, 2L, 2L, 0L, 2L, 1L, 2L, 2L)), class = "data.frame", row.names = c("165", 
"167", "168", "172", "174", "176", "177", "236", "246", "260", 
"263", "277", "280", "281", "282", "302", "315", "321", "322", 
"331", "332", "333", "335", "336", "339", "340", "105", "117", 
"132", "133", "134", "135", "136", "142", "148", "149", "150", 
"151", "154", "170", "173", "237", "238", "241", "251", "259", 
"261", "262", "268", "270", "272", "273", "283", "285", "301", 
"303", "304", "305", "314", "328", "337", "341", "97", "99", 
"100", "101", "102", "104", "106", "108", "111", "112", "113", 
"114", "115", "116", "118", "119", "120", "123", "124", "125", 
"126", "127", "128", "130", "131", "137", "138", "139", "140", 
"141", "143", "144", "145", "146", "147", "156", "169", "171", 
"175", "206", "222", "224", "225", "226", "227", "234", "239", 
"243", "249", "250", "252", "253", "258", "265", "275", "290", 
"291", "293", "294", "306", "307", "309", "313", "319", "323", 
"329", "330", "338", "98", "122", "202", "204", "208", "210", 
"211", "214", "217", "218", "219", "228", "230", "255", "257", 
"264", "266", "276", "284", "287", "288", "295", "296", "299", 
"300", "311", "312", "316", "317", "318", "320", "326", "327", 
"334", "1651", "1671", "1681", "1721", "1741", "1761", "1771", 
"2361", "2461", "2601", "2631", "2771", "2801", "2811", "2821", 
"3021", "3151", "3211", "3221", "3311", "3321", "3331", "3351", 
"3361", "3391", "3401", "1051", "1171", "1321", "1331", "1341", 
"1351", "1361", "1421", "1481", "1491", "1501", "1511", "1541", 
"1701", "1731", "2371", "2381", "2411", "2511", "2591", "2611", 
"2621", "2681", "2701", "2721", "2731", "2831", "2851", "3011", 
"3031", "3041", "3051", "3141", "3281", "3371", "3411", "971", 
"991", "1001", "1011", "1021", "1041", "1061", "1081", "1111", 
"1121", "1131", "1141", "1151", "1161", "1181", "1191", "1201", 
"1231", "1241", "1251", "1261", "1271", "1281", "1301", "1311", 
"1371", "1381", "1391", "1401", "1411", "1431", "1441", "1451", 
"1461", "1471", "1561", "1691", "1711", "1751", "2061", "2221", 
"2241", "2251", "2261", "2271", "2341", "2391", "2431", "2491", 
"2501", "2521", "2531", "2581", "2651", "2751", "2901", "2911", 
"2931", "2941", "3061", "3071", "3091", "3131", "3191", "3231", 
"3291", "3301", "3381", "981", "1221", "2021", "2041", "2081", 
"2101", "2111", "2141", "2171", "2181", "2191", "2281", "2301", 
"2551", "2571", "2641", "2661", "2761", "2841", "2871", "2881", 
"2951", "2961", "2991", "3001", "3111", "3121", "3161", "3171", 
"3181", "3201", "3261", "3271", "3341", "1652", "1672", "1682", 
"1722", "1742", "1762", "1772", "2362", "2462", "2602", "2632", 
"2772", "2802", "2812", "2822", "3022", "3152", "3212", "3222", 
"3312", "3322", "3332", "3352", "3362", "3392", "3402", "1052", 
"1172", "1322", "1332", "1342", "1352", "1362", "1422", "1482", 
"1492", "1502", "1512", "1542", "1702", "1732", "2372", "2382", 
"2412", "2512", "2592", "2612", "2622", "2682", "2702", "2722", 
"2732", "2832", "2852", "3012", "3032", "3042", "3052", "3142", 
"3282", "3372", "3412", "972", "992", "1002", "1012", "1022", 
"1042", "1062", "1082", "1112", "1122", "1132", "1142", "1152", 
"1162", "1182", "1192", "1202", "1232", "1242", "1252", "1262", 
"1272", "1282", "1302", "1312", "1372", "1382", "1392", "1402", 
"1412", "1432", "1442", "1452", "1462", "1472", "1562", "1692", 
"1712", "1752", "2062", "2222", "2242", "2252", "2262", "2272", 
"2342", "2392", "2432", "2492", "2502", "2522", "2532", "2582", 
"2652", "2752", "2902", "2912", "2932", "2942", "3062", "3072", 
"3092", "3132", "3192", "3232", "3292", "3302", "3382", "982", 
"1222", "2022", "2042", "2082", "2102", "2112", "2142", "2172", 
"2182", "2192", "2282", "2302", "2552", "2572", "2642", "2662", 
"2762", "2842", "2872", "2882", "2952", "2962", "2992", "3002", 
"3112", "3122", "3162", "3172", "3182", "3202", "3262", "3272", 
"3342"))

I would like to plot the countings in ggplot based on two factors (Tooth and Decil).

I can do it by using this code:

ggplot(data = x, aes(x=Pk_dif_int_cor))   
  geom_bar(aes(fill = Decil),stat="bin")  
  facet_wrap(~factor(Tooth, levels = c("Incisor", "Canine", "Premolar", "Molar"))   Decil, ncol = 3)

I would like to use the variable Tooth in facet_wrap (as is in the previous example), but the variable Decil to be represented their three factors (DC1, DC2, DC1) in one plot by Tooth, in three different columns per factor of Pk_dif_int_cor.

In the end, I would like to have 4 plots (Incisor, Canine, Premolar, Molar), with all the deciles in different columns from 0 to 6.

I tried by using fill and color with no success. Any idea?

CodePudding user response:

In response to OP comments maybe this is the intended output?




library(ggplot2)

library(dplyr)

x  |> 
  group_by(Tooth, Decil, Pk_dif_int_cor) |> 
  summarise(count = n()) |>
ggplot(aes(x = Decil, y = count))   
  geom_col(aes(fill = factor(Pk_dif_int_cor)), position = position_dodge2(preserve = "single"))  
  facet_wrap(~factor(Tooth, levels = c("Incisor", "Canine", "Premolar", "Molar")))
#> `summarise()` has grouped output by 'Tooth', 'Decil'. You can override using
#> the `.groups` argument.

Created on 2022-12-20 with reprex v2.0.2

  • Related