ggplot position dodge with position stack

Bonjour,
I use the ggplot function to represent bar graph.
I would like to know if I can use the position dodge(1 var) and position stack in the same graph

thanks in advance to your help!

Can you give an example of the code you're trying to use? What happens when you try to run it?

If your goal is to have a single chart with stacked and grouped bar charts, it appears to be possible. See here:

From answer above:

Hello, thanks to your help!

You will find hereafter my code.
I have 3 variables.
I want to represent these three variable as follow :
_ 2 variables on 1 bar
_ 1 variable on 1 bar
for each position x

I created 2 arrays :
_ kpi.project_1.tdy for 2 variables
_ kpi.project_2.tdy for 1 variables

Error message :

`data` must be a data frame, or other object coercible by `fortify()`, not a character vector
kpi.project_1.tdy<-kpi.project %>%
        gather("types","heures",3:4)
      print(kpi.project_1.tdy)
      
      #creation de la version tidy de kpi.project heures commandées
      kpi.project_2.tdy<-kpi.project %>%
        gather("types","heures",2:2)
      print(kpi.project_2.tdy)
      
      var_1<-factor(kpi.project_1.tdy$pas, levels = (unique(kpi.project_1.tdy$pas)), ordered=FALSE)
      var_2<-factor(kpi.project_2.tdy$pas, levels = (unique(kpi.project_2.tdy$pas)), ordered=FALSE)
      
      barwidth = 0.35
      ggplot() + 
        geom_bar(data = kpi.project_1.tdy$pas,
             mapping = aes(x=var_1 , y=heures,fill=types),
             stat ="identity",
             position = "stack",
             width = barwidth) +
        geom_bar(data = kpi.project_2.tdy$pas,
                 mapping = aes(x=var_2 + barwidth + 0.01 , y=heures,fill=types),
                 stat ="identity",
                 position = "stack",
                 width = barwidth) +
        xlab("pas") + ylab("heures") +
        labs(fill = "type d'heure")+
        theme(
          panel.background = element_blank(),
          panel.grid.minor = element_blank(), 
          panel.grid.major = element_line(color = "gray50", size = 0.5),
          panel.grid.major.x = element_blank(),
          axis.text.y = element_text(colour="#68382C", size = 9))

thanks in advance to your help!

As it notes in your error message, you need to have your data in a data frame.

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ.

I found the solution to the message d'error :

I changed geom_bar(data = kpi.project_1.tdy$pas by geom_bar(data = kpi.project_1.tdy  ,....

Now I have a new error message :

Warning: Ignoring unknown aesthetics: labels
Warning: Error in : Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?

I'm going to install the repex packages

You will find an extract from reprex.

 output$graph<- renderPlot({
      kpi.project<-tdonne()
      print(kpi.project)
      #kpi.project
      kpi.project.tdy<-kpi.project %>%
        gather("types","heures",2:4)
      kpi.project.tdy<-as.data.frame(kpi.project.tdy)
      print(kpi.project.tdy)
      
      #creation de la version tidy de kpi.project heures engagées & RAF
      kpi.project_1.tdy<-kpi.project %>%
        gather("types","heures",3:4)
      kpi.project_1.tdy<-as.data.frame(kpi.project_1.tdy)
      print(kpi.project_1.tdy)
      
      
      #creation de la version tidy de kpi.project heures commandées
      kpi.project_2.tdy<-kpi.project %>%
        gather("types","heures",2:2)
      kpi.project_2.tdy<-as.data.frame(kpi.project_2.tdy)
      print(kpi.project_2.tdy)
      
      var_1<-factor(kpi.project_1.tdy$pas, levels = (unique(kpi.project_1.tdy$pas)), ordered=FALSE)
      var_2<-factor(kpi.project_2.tdy$pas, levels = (unique(kpi.project_2.tdy$pas)), ordered=FALSE)
      
      barwidth = 0.35
      ggplot() + 
        geom_bar(data = kpi.project_1.tdy,
             mapping = aes(x=var_1 , y=heures,fill=as.factor(types)),
             stat ="identity",
             position = "stack",
             width = barwidth) +
        geom_text(data = kpi.project_1.tdy,
                  aes(x=var_1 , y=heures,label=heures))
        geom_bar(data = kpi.project_2.tdy,
              mapping = aes(x=var_2 + barwidth + 0.01 , y=heures,fill=as.factor(types)),
              stat ="identity",
              position = "stack",
              width = barwidth) +
          geom_text(data = kpi.project_2.tdy,
                   aes(x=var_2 + barwidth + 0.01 , y=heures, labels = heures))
        labs(fill  = "types")
        
    })

Hello,

Right now I have the error message

Warning in Ops.factor(var_1, as.numeric(barwidth)) :
  ‘+’ not meaningful for factors
Warning: Removed 14 rows containing missing values (position_stack).

It's very difficult to use ggplot2 ( certainly I'm not expert with R code...)

Other point, I try to use the example code and it's not ok , the message is

Error in UseMethod("filter_") : 
  no applicable method for 'filter_' applied to an object of class "function"

https://stackoverflow.com/questions/46597278/how-to-plot-a-stacked-and-grouped-bar-chart-in-ggplot/46597859#46597859

Breaking news !!! I find the solution.

The problem is that the var_1 is declared as factor so it's not possible to add factor and numeric value. I added as.numeric to var_2.
Now it's possible to use ggplot to have the position dodge with stack position. Cool !! :sunglasses:

...mapping = aes(x=as.numreric(var_1) + barwidth + 0.01 , ...

2 Likes

Hey... Can you send me the whole code...
I am using facet grid(to compensate one variable) in this case, but it is not satisfying.