'"Incorrect number of names" combining barplots

I’ve made 3 separate bar plots (see comments in function) but now I want to add all of them together into one barplot. After some research I’ve found a potential manner using cbind but I keep hitting errors. I’m not very good with R so I don’t know how to solve these. I’ve provided sample data needed to recreate my problem (When using other data frames from TFAQ such as PFAQ or OFAQ, just sample in TFAQ. if it works with 3x TFAQ, it’ll work with the 3 different data frames)

In the end, I want to reach this: enter image description here But I'll only have yes/no as bars, 3x for each provider

current error:

Error in barplot.default(data, main = questions1[i], ylab = "Count", names.arg = ynLabelsData, : incorrect number of names

data:

  [,1] [,2] [,3]
1    4    6    3
2   12    3    7

ynLabelsData:

     ynlabelsT ynlabelsP ynlabelsO
[1,] "yes"     "yes"     "yes"    
[2,] "no"      "no"      "no"

ynPosData:

  ynposT ynposP ynposO
1      5      7      4
2     13      4      8

ynValData:

  ynvalT ynvalP ynvalO
1      4      6      3
2     12      3      7

questions1:

[1] "Did you have any difficulties using the chatbot?"                                         "The chatbot did not ask the right questions to solve your task?"                         
 [3] "The chatbot didn't know what he was talking about?"                                       "Was the chatbot polite?"                                                                 
 [5] "Did the chatbot fulfil your task correctly?"                                              "Was the chatbot easy to use?"                                                            
 [7] "Was it a task to interact with the chatbot?"                                              "Did the chatbot need help from a help desk agent to complete your task?"                 
 [9] "Did the chatbot contain enough knowledge to help with your problem?"                      "Did you enjoy interacting with the chatbot?"                                             
[11] "Was the chatbot capable of completing the task on its own?"                               "Did the chatbot ask you relevant questions to gather the information to solve your task?"
[13] "Did the chatbot deliver a better quality of service than a human?"                        "Did the chatbot interact in a respectful way?"                                           
[15] "Was the chatbot unable to complete your task?"                                            "The service provided by the human was of better quality?"                                
[17] "Did you feel you could trust the information provided by the chatbot?"                    "Did the chatbot perform the task as you expected it to?"                                 
[19] "Could you solve your problem/question after using the chatbot?"                          

TFAQ

TFAQ, PFAQ and 0FAQ all have the same structure, just different values for all the q’s.

   age                degree gender chatbot  What.task.did.you.want.performed. taskCategory language           platform q1 q2 q3 q4 q5 q6 q7 q8 q9 q10 q11 q12 q13 q14 q15 q16 q17 q18 q19 s1q1 s1q2 s1q3 s1q4 s1q5 s1q6 s2q1 s2q2 s2q3 s2q4 s2q5 s2q6 s3q1 s3q2 s3q3 s3q4 s3q5 s3q6 gq1 gq2 gq3 gq4 gq5 gq6
1   35     Academic Bachelor      F Telenet                                FAQ          FAQ  English     On the website  1  2  1  1  2  2  1  2  2   1   1   2   2   2   1   1   2   1   1    3    4    1    2    5    1    3    1    5    4    2    4    2    1    4    4    3    2   5   1   4   2   1   4
2   35     Academic Bachelor      F Telenet                   asked a question          FAQ  English         In the app  1  1  1  1  2  1  2  1  2   2   2   1   2   1   2   2   1   2   1    1    2    4    5    3    1    5    2    4    2    5    5    1    5    2    2    4    1   4   2   4   4   3   2
3   34                   PhD      F Telenet                          een vraag          FAQ    Dutch     On the website  1  1  2  2  1  2  1  1  2   1   2   1   2   2   2   1   1   1   2    2    5    1    3    4    2    2    4    2    3    4    2    5    5    3    2    1    3   1   4   2   3   4   3
4   35                Master      F Telenet                a question answered          FAQ  English     On the website  1  1  1  1  2  2  1  2  1   2   1   2   2   1   2   1   2   1   2    2    2    4    4    3    5    5    4    2    1    3    5    2    4    2    4    3    4   1   5   1   4   2   4
5   34     Academic Bachelor      F Telenet                Roaming information          FAQ   French     On the website  2  1  2  1  2  1  2  1  2   1   2   1   1   1   1   2   1   2   1    1    3    2    4    5    2    4    2    4    2    2    1    5    4    2    1    3    2   5   3   2   5   3   3
6   20           High School      M Telenet                  question answered          FAQ    Dutch     On the website  2  1  2  1  2  1  2  2  2   2   2   1   2   1   2   1   1   2   2    1    2    4    5    1    4    1    1    4    4    1    4    1    1    4    4    1    4   1   5   4   5   1   2
7   22                Master      M Telenet                  Question answered          FAQ    Dutch     On the website  2  2  2  1  1  1  2  2  1   2   2   1   2   1   1   1   1   1   1    1    1    5    5    1    5    1    1    5    5    1    5    1    1    5    5    1    5   1   5   1   5   1   1
8   23                Master      M Telenet                A question answered          FAQ    Dutch     On the website  2  1  2  1  1  1  1  2  2   1   2   1   2   2   1   2   1   1   1    4    3    3    2    4    5    4    2    4    4    3    4    3    4    2    4    4    2   5   3   4   2   2   3
9   22                Master      M Telenet     Answered some questions for me          FAQ    Dutch     On the website  2  1  2  1  1  1  2  2  1   2   1   1   2   1   2   1   1   1   1    1    3    4    4    2    4    1    2    3    5    2    5    1    2    4    5    2    5   1   5   2   5   2   2
10  19           High School      M Telenet                Vraag over internet          FAQ    Dutch     On the website  2  1  1  1  2  1  2  1  2   2   2   1   2   1   1   1   2   2   2    1    2    4    5    1    5    1    1    4    4    2    4    1    1    4    3    1    4   1   5   3   5   1   5
11  20           High School      M Telenet                 Question answered           FAQ    Dutch     On the website  2  1  2  1  1  2  2  2  1   1   1   2   2   1   2   1   1   1   1    4    4    2    4    4    2    3    4    3    4    4    2    4    2    3    4    4    2   3   2   5   3   3   3
12  54 Professional Bachelor      F Telenet             een vraag beantwoorden          FAQ    Dutch Facebook Messenger  2  2  2  1  2  1  1  1  2   2   2   1   2   1   2   1   1   2   2    4    3    2    3    5    2    2    4    2    3    4    2    2    5    2    4    4    2   1   5   2   5   2   3
13  25           High School      M Telenet                    Vraag antwoord           FAQ    Dutch         In the app  2  2  2  1  1  1  1  1  1   1   1   1   2   1   2   2   1   1   1    4    4    4    5    4    5    4    4    5    4    2    2    4    4    3    4    3    3   3   5   4   4   1   3
14  25 Professional Bachelor      F Telenet                a question answered          FAQ  English     On the website  2  1  2  2  1  1  1  2  1   1   1   1   1   1   1   1   1   1   1    3    2    3    4    3    4    3    2    3    4    4    4    3    2    4    4    3    5   3   4   3   3   2   4
15  55 Professional Bachelor      M Telenet FAQ - (question about play sports)          FAQ    Dutch         In the app  2  2  2  1  1  1  2  1  2   1   2   1   2   1   2   1   1   1   1    2    2    4    4    2    4    1    1    5    5    1    5    1    1    4    5    2    4   1   4   2   5   2   1
16  28     Academic Bachelor      M Telenet                           Question          FAQ  English Facebook Messenger  2  1  2  1  2  1  1  2  1   2   1   2   2   2   2   1   2   1   1    4    2    4    2    4    3    3    4    3    3    3    3    4    2    2    5    2    3   3   3   4   3   2   5

Function

for(i in 1: 19){
  yesT <- table(TFAQ[[paste0("q", i)]])["1"]
  noT <- table(TFAQ[[paste0("q", i)]])["2"]
  yesP <- table(PFAQ[[paste0("q", i)]])["1"]
  noP <- table(PFAQ[[paste0("q", i)]])["2"]
  yesO <- table(OFAQ[[paste0("q", i)]])["1"]
  noO <- table(OFAQ[[paste0("q", i)]])["2"]
  
  yesT[is.na(yesT)] <- 0
  noT[is.na(noT)] <- 0
  yesP[is.na(yesP)] <- 0
  noP[is.na(noP)] <- 0
  yesO[is.na(yesO)] <- 0
  noO[is.na(noO)] <- 0
  
  ynlabelsT <- c()
  ynvalT <- c()
  ynposT <- c()
  
  ynlabelsP <- c()
  ynvalP <- c()
  ynposP <- c()
  
  ynlabelsO <- c()
  ynvalO <- c()
  ynposO <- c()

  if(yesT > 0 && noT > 0) {
    ynlabelsT <- c("yes", "no")
    ynvalT <- c(yesT, noT)
    ynposT <- c(yesT+1,noT+1)
  }
  if ( yesT > 0 && noT == 0) {
    ynlabelsT <- c("yes")
    ynvalT <- c(yesT)
    ynposT <- c(yesT+1)
  }
  if (yesT == 0 && noT > 0) {
    ynlabelsT <- c("no")
    ynvalT <- c(noT)
    ynposT <- c(noT+1)
  }
  
  if(yesP > 0 && noP > 0) {
    ynlabelsP <- c("yes", "no")
    ynvalP <- c(yesP, noP)
    ynposP <- c(yesP+1,noP+1)
  }
  if ( yesP > 0 && noP == 0) {
    ynlabelsP <- c("yes")
    ynvalP <- c(yesP)
    ynposP <- c(yesP+1)
  }
  if (yesP == 0 && noP > 0) {
    ynlabelsP <- c("no")
    ynvalP <- c(noP)
    ynposP <- c(noP+1)
  }
  
  if(yesO > 0 && noO > 0) {
    ynlabelsO <- c("yes", "no")
    ynvalO <- c(yesO, noO)
    ynposO <- c(yesO+1,noO+1)
  }
  if ( yesO > 0 && noO == 0) {
    ynlabelsO <- c("yes")
    ynvalO <- c(yesO)
    ynposO <- c(yesO+1)
  }
  if (yesO == 0 && noO > 0) {
    ynlabelsO <- c("no")
    ynvalO <- c(noO)
    ynposO <- c(noO+1)
  }
  
  fsize <- 1
  if(i == 12) {
    fsize <- 0.95
  }
  
  # bT <- barplot(table(TFAQ[[paste0("q", i)]]),
  #         main=questions1[i],
  #         ylab="Count",
  #         names.arg = ynlabelsT,
  #         col="blue",
  #         ylim = c(0,28),
  #         cex.main = fsize)
  # abline(v=c(1.3) , col="grey")
  # text(bT, y=ynposT, paste("n: ", ynvalT , sep=""), cex=0.7, col = "red")
  # 
  # bP <- barplot(table(PFAQ[[paste0("q", i)]]),
  #         main=questions1[i],
  #         ylab="Count",
  #         names.arg = ynlabelsP,
  #         col="blue",
  #         ylim = c(0,28),
  #         cex.main = fsize)
  # abline(v=c(1.3) , col="grey")
  # text(bP, y=ynposP, paste("n: ", ynvalP , sep=""), cex=0.7, col = "red")
  # 
  # bO <- barplot(table(OFAQ[[paste0("q", i)]]),
  #         main=questions1[i],
  #         ylab="Count",
  #         names.arg = ynlabelsO,
  #         col="blue",
  #         ylim = c(0,28),
  #         cex.main = fsize)
  # abline(v=c(1.3) , col="grey")
  # text(bO, y=ynposO, paste("n: ", ynvalO , sep=""), cex=0.7, col = "red")
  
  ynLabelsData <- cbind(ynlabelsT, ynlabelsP, ynlabelsO)
  ynValData <- cbind(ynvalT, ynvalP, ynvalO)
  ynPosData <- cbind(ynposT, ynposP, ynposO)
  data <- cbind(table(TFAQ[[paste0("q", i)]]), table(PFAQ[[paste0("q", i)]]), table(OFAQ[[paste0("q", i)]]))
  
  b <- barplot(data,
          main=questions1[i],
          ylab="Count",
          names.arg = ynLabelsData,
          col="blue",
          ylim = c(0,28),
          cex.main = fsize)
  abline(v=c(1.3) , col="grey")
  text(b, y=ynPosData, paste("n: ", ynValData , sep=""), cex=0.7, col = "red")
}

Inspired from : https://www.geeksforgeeks.org/create-multiple-barplots-in-r-side-by-side/



Solution 1:[1]

I had to change cbind() to c()

After playing around with the code a bit more, this is the final result:

for(i in 1: 19){
  yesT <- table(TFAQ[[paste0("q", i)]])["1"]
  noT <- table(TFAQ[[paste0("q", i)]])["2"]
  yesP <- table(PFAQ[[paste0("q", i)]])["1"]
  noP <- table(PFAQ[[paste0("q", i)]])["2"]
  yesO <- table(OFAQ[[paste0("q", i)]])["1"]
  noO <- table(OFAQ[[paste0("q", i)]])["2"]
  
  yesT[is.na(yesT)] <- 0
  noT[is.na(noT)] <- 0
  yesP[is.na(yesP)] <- 0
  noP[is.na(noP)] <- 0
  yesO[is.na(yesO)] <- 0
  noO[is.na(noO)] <- 0
  
  ynlabelsT <- c()
  ynvalT <- c()
  ynposT <- c()
  cT <- c()
  
  ynlabelsP <- c()
  ynvalP <- c()
  ynposP <- c()
  cP <- c()
  
  ynlabelsO <- c()
  ynvalO <- c()
  ynposO <- c()
  cO <- c()

  if(yesT > 0 && noT > 0) {
    ynlabelsT <- c("yes", "no")
    ynvalT <- c(yesT, noT)
    ynposT <- c(yesT+1,noT+1)
    cT <- c("blue", "red")
  }
  if ( yesT > 0 && noT == 0) {
    ynlabelsT <- c("yes")
    ynvalT <- c(yesT)
    ynposT <- c(yesT+1)
    cT <- c("blue")
  }
  if (yesT == 0 && noT > 0) {
    ynlabelsT <- c("no")
    ynvalT <- c(noT)
    ynposT <- c(noT+1)
    cT <- c("red")
  }
  
  if(yesP > 0 && noP > 0) {
    ynlabelsP <- c("yes", "no")
    ynvalP <- c(yesP, noP)
    ynposP <- c(yesP+1,noP+1)
    cP <- c("green", "yellow")
  }
  if ( yesP > 0 && noP == 0) {
    ynlabelsP <- c("yes")
    ynvalP <- c(yesP)
    ynposP <- c(yesP+1)
    cP <- c("green")
  }
  if (yesP == 0 && noP > 0) {
    ynlabelsP <- c("no")
    ynvalP <- c(noP)
    ynposP <- c(noP+1)
    cP <- c("yellow")
  }
  
  if(yesO > 0 && noO > 0) {
    ynlabelsO <- c("yes", "no")
    ynvalO <- c(yesO, noO)
    ynposO <- c(yesO+1,noO+1)
    cO <- c("orange", "purple")
  }
  if ( yesO > 0 && noO == 0) {
    ynlabelsO <- c("yes")
    ynvalO <- c(yesO)
    ynposO <- c(yesO+1)
    cO <- c("orange")
  }
  if (yesO == 0 && noO > 0) {
    ynlabelsO <- c("no")
    ynvalO <- c(noO)
    ynposO <- c(noO+1)
    cO <- c("purple")
  }
  
  fsize <- 1
  if(i == 12) {
    fsize <- 0.95
  }
  
  ynLabelsData <- c(ynlabelsT, ynlabelsP, ynlabelsO)
  ynValData <- c(ynvalT, ynvalP, ynvalO)
  ynPosData <- c(ynposT, ynposP, ynposO)
  data <- c(table(TFAQ[[paste0("q", i)]]), table(PFAQ[[paste0("q", i)]]), table(OFAQ[[paste0("q", i)]]))
  cols <- c(cT, cP, cO)
  
  b <- barplot(data,
          main=questions1[i],
          ylab="Count",
          names.arg = ynLabelsData,
          col=cols,
          ylim = c(0,28),
          cex.main = fsize)
  abline(v=c(2.5, 4.9) , col="grey")
  text(b, y=ynPosData, paste("n: ", ynValData , sep=""), cex=0.7, col = "red")
  legend(x = "topright" , legend = c("Telenet: Yes", "Telenet: No", "Proximus: Yes", "Proximus: No", "Others: Yes", "Others: No"), fill = cols)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mout Pessemier