Search code examples
rggplot2geom-bar

geom_bar(position="dodge") and errorbar not dodging properly


So I got a little issue.... Here's my code :

ggplot(GFAPdata_numb, aes(x=Level, y=Pos.Area, fill=Statut))+
            geom_bar(stat="identity", color="black", position = "dodge")+
            geom_errorbar(aes(ymin=lower, ymax=higher), width=.2, position=position_dodge(.9))

And for some weird and unknown reason, my plot look like this : weird dodge

And I don't know why ! The dodge seems to have work somehow but it like looks like the "ghost" of the data are still stacked and screwing up with my errorbars...

Do you guys have any ideas what's causing that ?

Edit : I was asked to put some data with dput so here it is (first time using this function so I'm not sure I did it right)

> dput(head(GFAPdata_numb))
structure(list(Agneau = c(1L, 1L, 1L, 1L, 2L, 2L), Statut = c("terme", 
"terme", "terme", "terme", "terme", "terme"), Area = c(6.53, 
6.53, 6.53, 6.53, 4.93, 4.93), Level = c("Weak", "Pos", "Strong", 
"Neg", "Weak", "Pos"), Values = c(6744015L, 5076648L, 787615L, 
13099676L, 5356151L, 3978924L), Positivity = c(0.262331844844596, 
0.197473824638087, 0.0306370160768142, 0.509557314440504, 0.275961978086681, 
0.205003880155091), Pos.Area = c(0.0401733299915154, 0.0302410144928157, 
0.00469173293672499, 0.0780332793936453, 0.0559760604638299, 
0.041582937151134), moyenne = c(0.0382848392036753, 0.0382848392036753, 
0.0382848392036753, 0.0382848392036753, 0.050709939148073, 0.050709939148073
), ecart.type = c(0.0304231534615388, 0.0304231534615388, 0.0304231534615388, 
0.0304231534615388, 0.0391149666608345, 0.0391149666608345), 
    SEM = c(0.0152115767307694, 0.0152115767307694, 0.0152115767307694, 
    0.0152115767307694, 0.0195574833304173, 0.0195574833304173
    ), lower = c(0.00847014881136729, 0.00847014881136729, 0.00847014881136729, 
    0.00847014881136729, 0.0123772718204552, 0.0123772718204552
    ), higher = c(0.0680995295959834, 0.0680995295959834, 0.0680995295959834, 
    0.0680995295959834, 0.0890426064756909, 0.0890426064756909
    )), row.names = c(NA, -6L), groups = structure(list(Agneau = 1:2, 
    .rows = structure(list(1:4, 5:6), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

Solution

  • Ok I think I managed to correct the issue by myself after a couple of days not working on it to refresh my brain.

    I was using "Pos.Area" as my y-value (which mean the value for each of my smaples) instead of the mean of the Pos.Area to create my plot. And I guess that's why my errorbars were so wild : I had the errorbars for each values of Pos.Area

    Once I changed that, the plot was way better.