Barplot with 9 columns, can only make 3 Confidence Intervals

Hello.

I struggled the whole afternoon with a problem. I cannot solved it or find a solution on Google, despite I think it is very simple.

Summary :
I draw the barplot of a vector of nine variables (length 9). Then, I want to draw the Confidence Interval of each bar. It works for bar #1, #2 and #3. Then for #4 and above it says :

"Error in bb[4, ] : indice hors limites"

= "Error in bb[4, ] : subscript out-of-limits"

This is the code I uses for creating this barplot. Since this is an ongoing academic project, I cannot make him a reproducible example by giving the datas. However, the code is very basic.

#Vector of values to plot
type_crt <- c(mean(data_crt$err_2,na.rm = TRUE),mean(data_crt$intu_2,na.rm = TRUE),mean(data_crt$refl_2,na.rm = TRUE),mean(data_crt$err_3,na.rm = TRUE),mean(data_crt$intu_3,na.rm = TRUE),mean(data_crt$refl_3,na.rm = TRUE),mean(data_crt$err_4,na.rm = TRUE),mean(data_crt$intu_4,na.rm = TRUE),mean(data_crt$refl_4,na.rm = TRUE))
type_crt <- round(type_crt,3)
type_crt

#Barplot
barplot(type_crt, col = c("cornflowerblue"), yaxt = "n", xaxt = "n", ylim = c(0,1.2))
axis(2, at = c(0,0.2,0.4,0.6,0.8,1),las = 1)

#X-axis Labels
mtext("Error", side=1, line=1, cex = 0.5, at=0.7)
mtext("Intuitive", side=1, line=1, cex = 0.5, at=1.9)
mtext("Reflective", side=1, line=1, cex = 0.5, at=3.1)
mtext("Error", side=1, line=1, cex = 0.5, at=4.3)
mtext("Intuitive", side=1, line=1, cex = 0.5, at=5.5)
mtext("Reflective", side=1, line=1, cex = 0.5, at=6.7)
mtext("Error", side=1, line=1, cex = 0.5, at=7.9)
mtext("Intuitive", side=1, line=1, cex = 0.5, at=9.1)
mtext("Reflective", side=1, line=1, cex = 0.5, at=10.3)

#Y-axis labels
text(0.7, type_crt[1] + 0.05, labels = type_crt[1], cex=.8)
text(1.9, type_crt[2] + 0.05, labels = type_crt[2], cex=.8)
text(3.1, type_crt[3] + 0.05, labels = type_crt[3], cex=.8)
text(4.3, type_crt[4] + 0.05, labels = type_crt[4], cex=.8)
text(5.5, type_crt[5] + 0.05, labels = type_crt[5], cex=.8)
text(6.7, type_crt[6] + 0.05, labels = type_crt[6], cex=.8)
text(7.9, type_crt[7] + 0.05, labels = type_crt[7], cex=.8)
text(9.1, type_crt[8] + 0.05, labels = type_crt[8], cex=.8)
text(10.3, type_crt[9] + 0.05, labels = type_crt[9], cex=.8)

#Confidence Intervals

#IC where it works
t.test(data_crt$err_2,conf.level=0.95)
segments(bb[1,],0.0036,bb[1,],0.238, lwd = 1)
arrows(bb[1,],0.0036,bb[1,],0.238, lwd = 1,angle = 90, code = 3, length = 0.05)
t.test(data_crt$intu_2,conf.level=0.95)
segments(bb[2,],-0.025,bb[2,],0.146, lwd = 1)
arrows(bb[2,],-0.025,bb[2,],0.146, lwd = 1,angle = 90, code = 3, length = 0.05)
t.test(data_crt$refl_2,conf.level=0.95)
segments(bb[3,],0.679,bb[3,],0.957, lwd = 1)
arrows(bb[3,],0.679,bb[3,],0.957, lwd = 1,angle = 90, code = 3, length = 0.05)

#IC where it does not work
t.test(data_crt$err_3,conf.level=0.95)
segments(bb[4,],0.072,bb[4,],0.302, lwd = 1)
arrows(bb[4,],0.072,bb[4,],0.302, lwd = 1,angle = 90, code = 3, length = 0.05)
t.test(data_crt$intu_3,conf.level=0.95)
segments(bb[5,],-0.008,bb[5,],0.133, lwd = 1)
arrows(bb[5,],-0.008,bb[5,],0.133, lwd = 1,angle = 90, code = 3, length = 0.05)
t.test(data_crt$refl_3,conf.level=0.95)
segments(bb[6,],0.622,bb[6,],0.877, lwd = 1)
arrows(bb[6,],0.622,bb[6,],0.877, lwd = 1,angle = 90, code = 3, length = 0.05)
t.test(data_crt$err_4,conf.level=0.95)
segments(bb[7,],0.004,bb[7,],0.254, lwd = 1)
arrows(bb[7,],0.004,bb[7,],0.254, lwd = 1,angle = 90, code = 3, length = 0.05)
t.test(data_crt$intu_4,conf.level=0.95)
segments(bb[8,],-0.013,bb[8,],0.207, lwd = 1)
arrows(bb[8,],-0.013,bb[8,],0.207, lwd = 1,angle = 90, code = 3, length = 0.05)
t.test(data_crt$refl_4,conf.level=0.95)
segments(bb[9,],0.618,bb[9,],0.930, lwd = 1)
arrows(bb[9,],0.618,bb[9,],0.930, lwd = 1,angle = 90, code = 3, length = 0.05)

#Axis Label
mtext("CRT type", side = 1, line = 3, at = 5.5, cex = 1.2)
mtext("Consistency (Frequency)", side = 2, line = 3, at = 0.5, cex = 1.2)

I cannot understand why this is happening. I previously perfectly made barplots of 4 columns. I think this is an overall problem, because when I try to redo these barplots of 4 columns, it fails. If someone has an intuition of what is going on, thank you !

I know you have said that it is an ongoing project and you cannot provide data, but it is very difficult to try to solve it without some data.

Can you at least provide some sample data (made up even). Perhaps even a picture showing your plot and what is wrong with it.

1 Like

Unfortunately no, but if I can give some details about the dataset.

But anyway, I used the method with the dlpyr package to solve that ! I do not have my answer on why in my barplot I have the #1,#2 and #3 spaces available, but not for #4 and above.

Dataset : data_crt

Variables :
err_2, intu_2, refl_2 = "Error for Grade 2", "Intuitive for Grade 2", "Reflective for Grade 2"
err_3, intu_3, refl_3 = "Error for Grade 3", "Intuitive for Grade 3", "Reflective for Grade 3"
err_4, intu_4, refl_4 = "Error for Grade 4", "Intuitive for Grade 4", "Reflective for Grade 4"

Error / Intuitive / Reflective are the different type of subjects, according to how they reply on the Cognitive Reflective Task.

Grade 2/3/4 gives the grade of the student.

Each variable is a : 0/1 = No/Yes. If you belong to the category, you are "1", otherwise you are "0".

Variables are initialized as NA, then variables for your year becomes "0" and the correct category for your year becomes a "1".
So every line of the dataset is made of six "NA" two "0" and one "1".

Like

err_2 intu_2 refl_2 err_3 intu_3 refl_3 err_4 intu_4 refl_4
1 0 0 NA NA NA NA NA NA
0 1 0 NA NA NA NA NA NA
NA NA NA NA NA NA 1 0 0
NA NA NA 0 1 0 NA NA NA

But with 250 lines.

We do not have enough information to help.

You have one vector to plot, type_crt , but you are calling on what appears to be 2 data.frames/tibbles, data_crt and bb that we know nothing about.

I do not see how we can help you without a representative example..You can generate the data using a random number generator. We need to see the data structure. The actual data is not important.

1 Like

Ok thanks, I am not good enough for doing this. Next time I'll ask if I can do what you said.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.