one stack is missing in stacked barplot (ggplot2)

I have 5 methods and I would like to have a stack bar for each of them in a way that each bar has three stacks that each stack shows a range of values. fail: values less that 0.3, intermediate: values between 0.3 and 0.7, and high: values are more thatn 0.7
I have specific colors for methods and specific patterns for stacks. this is output of my code.


as you can see, "fail" stack is missing although I have fail vin my dataframe. also like to mention that for some values I had NAs in value column that I replaced with 0.
melted_df has four columns, named: Name, methods, values, stack_level

this is my code:

library(dplyr)
library(ggplot2)
library(reshape2)
library(ggpattern)

melted_df <- melt(AA, id.vars = "Name", variable.name = "method")
 melted_df$value[is.na(melted_df$value)] <- 0
 melted_df$value[(melted_df$value) < = 0] <- 0

breaks <- c(0, 0.3, 0.7, 1)
# Function to assign stack level based on value
assign_stack <- function(value) {
  if (value <= 0.3 && value >= 0) {
    return("fail")
  } else if (0.3 < value  && value < 0.7) {
    return("intermediate")
  } else if (value >= 0.7){
    return("high")
  }
}

# Apply the function to create a new column for stacking
melted_df$stack_level <- sapply(melted_df$value, assign_stack)
melted_df$stack_level <- factor(melted_df$stack_level, levels = c("high", "intermediate", "fail"))
Final_plot <- ggplot(melted_df, aes(x = method, y = value, fill = method)) +
  geom_col_pattern(aes(fill=method, pattern=stack_level, pattern_type = stack_level)
                   # ,colour='red'
                   ,pattern_density = 0.15
                   ,pattern_colour = "white"
                   ,pattern_fill = "white"
  ) + 
  
  labs(title = "Stacked Barplot with Value Ranges") + 
  geom_bar(stat = "identity",position = "dodge") +
  scale_fill_manual(values = c(gRF_Cor_test = "#33BBEE",STITCHIT_cor = "#b0b0b0",NN_Cor_test = "#083954",ugRF_test_cor = "#fae039", CNN_test_cor = "#ab0303"))+
  scale_pattern_manual(values=c(high = "circle",intermediate =  "stripe",fail = "none")) +
  scale_pattern_type_manual(values=c(NA, NA,NA ))+
  scale_pattern_fill_viridis_d() +
  theme_bw() +
  labs(x = "Method", title = "Stacked Barplot with Value Ranges") 
ggsave("/path/", plot = Final_plot, device = "pdf")

does anyone know what the problem is?
thanks in advance

It looks to me like the fail bars are there, but are very short. What happens if you run the following?:

melted_df |> 
  count(stack_level, method)

@dromano thanks for your reply, Actually Im getting this error
Error in FUN(X[[i]], ...) : object 'high' not found

but these are number of different stacks:
high:782
fail: 216
intermediate: 1106

BTW, I also ran the code for a bigger dataset that it has same number of gene for every method (it means, height of all 5 bars should be same), again bars just have two stacks and different height, thats why Im sure the fail part is missing.

Could you run the statement dput(AA) and paste the result here, like this?

``` r
<-- place output here
```

I think I found where this error comes from, but I dont know how to fix it.

ggplot(melted_df, aes(x = method, y = value, fill = method)) +

in the above line Im assigning value to y. I got stack_level based on my value but I should assign count of my stack levels to y. I mean (number of fail, intermediate and high), then the plot works properly. but how can I do that?

I need to mention that fail is not missing but its really small which is wrong, because the bars should have same height.

structure(list(gene = c("ENSG00000000003", "ENSG00000000005", 
"ENSG00000000419", "ENSG00000000457", "ENSG00000000460", "ENSG00000000938", 
"ENSG00000000971", "ENSG00000001036", "ENSG00000001084", "ENSG00000001167", 
"ENSG00000001460", "ENSG00000001461", "ENSG00000001497", "ENSG00000001561", 
"ENSG00000001617", "ENSG00000001626", "ENSG00000001629", "ENSG00000001630", 
"ENSG00000001631", "ENSG00000002016", "ENSG00000002079", "ENSG00000002330", 
"ENSG00000002549", "ENSG00000002586", "ENSG00000002587", "ENSG00000002726", 
"ENSG00000002745", "ENSG00000002746", "ENSG00000002822", "ENSG00000002834", 
"ENSG00000002919", "ENSG00000002933", "ENSG00000003056", "ENSG00000003096", 
"ENSG00000003137", "ENSG00000003147", "ENSG00000003249", "ENSG00000003393", 
"ENSG00000003400", "ENSG00000003402", "ENSG00000003436", "ENSG00000003509", 
"ENSG00000003756", "ENSG00000003987", "ENSG00000003989", "ENSG00000004059", 
"ENSG00000004139", "ENSG00000004142", "ENSG00000004399", "ENSG00000004455", 
"ENSG00000004468", "ENSG00000004478", "ENSG00000004487", "ENSG00000004534", 
"ENSG00000004660", "ENSG00000004700", "ENSG00000004766", "ENSG00000004776", 
"ENSG00000004777", "ENSG00000004779", "ENSG00000004799", "ENSG00000004809", 
"ENSG00000004838", "ENSG00000004846", "ENSG00000004848", "ENSG00000004864", 
"ENSG00000004866", "ENSG00000004897", "ENSG00000004939", "ENSG00000004948", 
"ENSG00000004961", "ENSG00000004975", "ENSG00000005001", "ENSG00000005007", 
"ENSG00000005020", "ENSG00000005022", "ENSG00000005059", "ENSG00000005073", 
"ENSG00000005075", "ENSG00000005100", "ENSG00000005102", "ENSG00000005108", 
"ENSG00000005156", "ENSG00000005175", "ENSG00000005187", "ENSG00000005189", 
"ENSG00000005194", "ENSG00000005206", "ENSG00000005238", "ENSG00000005243", 
"ENSG00000005249", "ENSG00000005302", "ENSG00000005339", "ENSG00000005379", 
"ENSG00000005381", "ENSG00000005421", "ENSG00000005436", "ENSG00000005448", 
"ENSG00000005469", "ENSG00000005471"), NN_Cor_test = c(0.832224574203483, 
0.590804816060271, 0.443223902957184, 0.121556269417118, 0.205112927679365, 
0.860840794711375, 0.598520627602098, 0.59872934682316, 0.745142705677465, 
0.331660132854179, 0.730170158041497, 0.703585609442008, 0.732534545335106, 
0.571352825459686, 0.87274672476937, 0.697393190129906, 0.496450155341674, 
0.716867918554736, 0.359762608434218, 0.574621060006212, NA, 
0.620992030835743, 0.434447484505526, 0.742443131362284, 0.79252486088689, 
0.608259800007138, 0.515992373967954, 0.77204284444361, 0.66060601905121, 
0.492726456517004, 0.606628207437187, 0.807894039153654, 0.545147974902156, 
0.736532260875535, 0.657074591677833, 0.639134776703675, 0.829590355530574, 
0.519722857967488, 0.396251009670398, 0.408701473705744, 0.776488162968967, 
0.403263224161901, 0.379571824272711, 0.69586562864193, 0.891138299961418, 
0.44585520864646, 0.706350272557484, 0.665895134287254, 0.900947420895701, 
0.630589698443031, 0.576564991941189, 0.646955233141269, 0.715824384566344, 
0.461862768722242, 0.369272426793341, 0.561291693989522, 0.62441849883784, 
0.778707474074472, 0.665890220728622, 0.647554936185385, 0.858295236971743, 
0.792379097320797, 0.35032708099394, 0.430599761872206, 0.712789728116424, 
0.525047938196252, 0.717775594920297, 0.451040436706579, 0.510861514756883, 
0.702331923784983, 0.645148961008526, 0.538510272177662, 0.850411939522457, 
0.463480064090945, 0.635822122237365, 0.625494356200948, 0.804188686282324, 
0.848998650098695, 0.424893377239682, 0.62052549293526, 0.729465528713193, 
0.789821554892105, 0.581538693691143, 0.381820285736887, 0.625039949609369, 
0.708156707157401, 0.623441009666918, 0.622203372252775, 0.638111293061851, 
0.796651526506383, 0.78374628967299, 0.226767426796758, 0.409776799191625, 
0.4609225334783, 0.757039406159013, 0.647879830109472, 0.677752996555798, 
0.781560496592342, 0.617508285644271, 0.805797475667237), gRF_Cor_test = c(0.877477486781529, 
0.65753138812683, 0.558838814724195, 0.455790043672135, 0.594473654803494, 
0.897951354216556, 0.718129656533667, 0.793140672545805, 0.767549075261784, 
0.554364678215229, 0.777288198805611, 0.760597727244169, 0.702534187562549, 
0.595539718111345, 0.873896714376128, 0.810440571752971, 0.60418351817269, 
0.753390966691027, 0.603442908438122, 0.633418019175336, 0.65771504290298, 
0.706543487871083, 0.639320749273947, 0.830350772648051, 0.798997492587796, 
0.745837116800169, 0.627848412695196, 0.782494730109428, 0.690616346541044, 
0.620998725186054, 0.683490443291223, 0.856116268351648, 0.681588909045906, 
0.776779311565538, 0.795973411040349, 0.736198176716186, 0.845486923400461, 
0.609206466087794, 0.604584738836039, 0.5627820455504, 0.854095988875932, 
0.528449275761981, 0.459985821098689, 0.761777236617448, 0.893952488928847, 
0.583819786310068, 0.734666105071241, 0.678899996811741, 0.882468762839123, 
0.654280805066029, 0.70680233885987, 0.744368870735452, 0.767348361819228, 
0.553008868404554, 0.530634101100139, 0.629980463028853, 0.666045612121873, 
0.832835489940688, 0.709625050195045, 0.704300224489478, 0.884835289696268, 
0.859609251104902, 0.638701543857686, 0.617710992111183, 0.733906339274128, 
0.607174964825238, 0.797612458045949, 0.595169812462197, 0.694748391689239, 
0.692626076494633, 0.670680125196751, 0.636170770974607, 0.845390390243113, 
0.564683011986854, 0.792529599802777, 0.685924700986793, 0.875659543989518, 
0.774846832510679, 0.556876741423126, 0.659927115623093, 0.740305657884713, 
0.818999188690451, 0.621429516834923, 0.496074626638736, 0.692420676232337, 
0.787826460827994, 0.673797616448254, 0.681071878925206, 0.792472373254988, 
0.837030833523476, 0.835874405191025, 0.467757607681401, 0.495186367374959, 
0.67051150661776, 0.854539415829592, 0.74608510883022, 0.699082719927195, 
0.760083204962235, 0.676157886276065, 0.785364239252188), ugRF_test_cor = c(0.82, 
0.62, 0.58, 0.43, 0.52, 0.89, 0.7, 0.77, 0.76, 0.52, 0.75, 0.75, 
0.68, 0.58, 0.87, 0.79, 0.54, 0.73, 0.57, 0.63, 0.64, 0.68, 0.61, 
0.82, 0.8, 0.71, 0.55, 0.74, 0.68, 0.6, 0.64, 0.85, 0.67, 0.75, 
0.76, 0.71, 0.83, 0.57, 0.57, 0.54, 0.83, 0.5, 0.43, 0.71, 0.88, 
0.54, 0.72, 0.68, 0.88, 0.62, 0.66, 0.75, 0.75, 0.56, 0.51, 0.58, 
0.66, 0.82, 0.7, 0.71, 0.88, 0.85, 0.6, 0.62, 0.64, 0.59, 0.79, 
0.59, 0.67, 0.67, 0.67, 0.62, 0.85, 0.58, 0.78, 0.68, 0.84, 0.78, 
0.52, 0.66, 0.74, 0.81, 0.61, 0.48, 0.68, 0.74, 0.66, 0.68, 0.77, 
0.82, 0.81, 0.51, 0.53, 0.67, 0.84, 0.74, 0.69, 0.75, 0.64, 0.73
), STITCHIT_cor = c(0.838473456000038, 0.601868054240087, NA, 
NA, NA, 0.893208280080676, 0.683429609422409, 0.78852366418661, 
NA, NA, 0.776182869808885, NA, NA, 0.570241463581494, 0.872846419709397, 
0.771748191642737, NA, NA, NA, NA, 0.687489600810051, NA, NA, 
0.800213494348795, 0.83306405968965, 0.658744233654551, 0.562966459060794, 
0.803056640281919, NA, NA, NA, 0.831505486655324, NA, 0.747440359013762, 
0.796906275657074, 0.743628194824356, 0.837507071059296, NA, 
0.585381076378612, NA, 0.778232438429244, NA, NA, 0.749578906101235, 
0.840287188474843, NA, 0.730275857122028, NA, 0.866325166532293, 
NA, 0.677686757806398, NA, NA, NA, 0.588295960497041, NA, NA, 
0.800860940399742, 0.68227072168685, NA, 0.846581290471473, 0.874124589615253, 
0.593926285428081, 0.610734888489574, 0.734720886878445, NA, 
0.815337421254408, NA, 0.666219491042155, 0.600017771294207, 
NA, NA, 0.845963120789839, NA, 0.78508078891918, NA, 0.854293997482819, 
0.755217191869734, NA, NA, 0.755666881585406, 0.791881047943738, 
NA, NA, 0.599519608892389, 0.743766665007564, NA, NA, NA, 0.81357311915741, 
0.811150425852019, NA, NA, 0.607943992568336, 0.813365937346751, 
0.733996332846304, 0.709962993426094, 0.794470771824201, 0.654147039371511, 
0.770387325641648), CNN_test_cor = c(0.866709882518444, 0.727775842275534, 
0.547009508899654, 0.446193527618854, 0.464176123230579, 0.890813269844092, 
0.720906760425075, 0.733532983084386, 0.77293547374463, 0.48993611438148, 
0.776505117580659, 0.757539648034829, 0.708873492654496, 0.372975252609314, 
0.84841010332672, 0.751945022605219, 0.608713515378636, 0.74334487234134, 
0.597953604237655, 0.557885696188016, 0.642740504114924, 0.734618421431177, 
0.651948837997953, 0.760267806394843, 0.7973741659227, 0.688386384984786, 
0.576788674810588, 0.846611157073756, 0.69345594903405, 0.563813650406079, 
0.586251580434156, 0.876546819651163, 0.673688057431321, 0.712329172809654, 
0.822377637803332, 0.606665386945109, NA, 0.54279967977966, 0.616779317969267, 
0.448678892082689, 0.74986238629076, 0.583902716755463, 0.44856612123682, 
0.774857543610951, 0.883496272593202, 0.499914768676506, 0.736938106656133, 
0.604756163163693, 0.863505041430591, 0.667470688044327, 0.659682946237704, 
0.669479597454863, 0.757549669113211, 0.535868161001644, 0.529590844798804, 
0.586404223197224, 0.588854781406874, 0.855567649270914, 0.737846810809651, 
0.716188233334852, 0.835164310005356, 0.825062847076639, 0.514158613178498, 
0.602575621660529, 0.703224541804137, 0.579214310643308, 0.817020835144337, 
0.576579652700372, 0.640164185035999, 0.568984892868629, 0.600706616007017, 
0.714130476295995, 0.878584923350745, 0.471019204851996, 0.728770486890202, 
0.711507681504311, 0.863610967771032, 0.825376884690755, 0.50429925180718, 
0.681091187000385, 0.803750115349548, 0.763592999339252, 0.630950291866857, 
0.492374012313766, 0.690728059961605, 0.69129399623842, 0.703838185787265, 
0.707699644613282, 0.744792250122474, 0.833432709159662, 0.814711103740713, 
0.401701651046808, 0.439843659938339, 0.600472271797873, 0.757848890761478, 
0.761492118764506, 0.637570830360452, 0.804540618717598, 0.662706280035836, 
0.767251603837606)), row.names = c(NA, 100L), class = "data.frame")

this is first 100 rows of the AA.

Thanks, though this is a different table from the one you used in the code for your original post — the melt() command fails on it because the column names are different. Could you share the original so that the code works?