I'm trying to make a grouped barplot for a multiple intra-class coefficient of the same quantitative variables, estimated before and after standardisation on different factors("constructeur","coup"..."pitch"). Here's the 5 first rows of my dataset:
ICC_intra_observ_3D <- read.csv2("~/Documents/ICC_intra_observ_3D_av_ap_H.csv")
ICC_intra_observ_3D[c(1:5),]
Texture_Feature ICC_intra_observ_3D_av_H ICC_ap_H_constructeur ICC_ap_H_coup ICC_ap_H_detect ICC_ap_H_filter ICC_ap_H_kv ICC_ap_H_mAs ICC_ap_H_pitch
1 CONVENTIONAL_HUmin 11.18 22.26 11.3 13.86 22.94 11.74 18.84 14.26
2 CONVENTIONAL_HUmean 91.16 91.06 91.05 92.09 89.33 90.79 88.2 92.26
3 CONVENTIONAL_HUstd 60.16 62.09 60.34 62.26 63.64 60.22 61.94 59.96
4 CONVENTIONAL_HUmax 76.36 77.09 76.41 80.12 74.75 75.74 73.37 77.21
5 CONVENTIONAL_HUQ1 88.81 88.86 88.7 90.04 87.29 88.46 86.17 90.62
Here's some transformations so that the barplot function will do with the convenient matrix
rownames(ICC_intra_observ_3D)=ICC_intra_observ_3D$Texture_Feature
ICC_intra_observ_3D=ICC_intra_observ_3D[,-1]
ICC_intra_observ_3D=t(ICC_intra_observ_3D)
The result after that is:
CONVENTIONAL_HUmin CONVENTIONAL_HUmean CONVENTIONAL_HUstd CONVENTIONAL_HUmax CONVENTIONAL_HUQ1 CONVENTIONAL_HUQ2 CONVENTIONAL_HUQ3
ICC_intra_observ_3D_av_H "11.18" "91.16" "60.16" "76.36" "88.81" "89.91" "91.1"
ICC_ap_H_constructeur "22.26" "91.06" "62.09" "77.09" "88.86" "89.89" "91.04"
ICC_ap_H_coup "11.3" "91.05" "60.34" "76.41" "88.7" "89.84" "91.1"
ICC_ap_H_detect "13.86" "92.09" "62.26" "80.12" "90.04" "90.96" "91.47"
ICC_ap_H_filter "22.94" "89.33" "63.64" "74.75" "87.29" "88.12" "89.07"
ICC_ap_H_kv "11.74" "90.79" "60.22" "75.74" "88.46" "89.62" "90.79"
ICC_ap_H_mAs "18.84" "88.2" "61.94" "73.37" "86.17" "87.03" "87.92"
ICC_ap_H_pitch "14.26" "92.26" "59.96" "77.21" "90.62" "91.26" "91.88"
But when running:
barplot(ICC_intra_observ_3D,beside=T)
the error message: Error in -0.01 * height : non-numeric argument to binary operator
knowing that with the default argument beside = F
I have a stacked barplot seems to function properly but I need to compare the different ICC so putting them beside is more suitable.
Note that I didn't try with ggplot()
because it seems to be even more difficult to reorganize my input dataset, but any suggestion is welcome.
Thanks for your help
I would suggest a tidyverse
approach. Using your final data as df
, here the code:
First the data:
#Data
df <- structure(list(Var = c("ICC_intra_observ_3D_av_H", "ICC_ap_H_constructeur",
"ICC_ap_H_coup", "ICC_ap_H_detect", "ICC_ap_H_filter", "ICC_ap_H_kv",
"ICC_ap_H_mAs", "ICC_ap_H_pitch"), CONVENTIONAL_HUmin = c(11.18,
22.26, 11.3, 13.86, 22.94, 11.74, 18.84, 14.26), CONVENTIONAL_HUmean = c(91.16,
91.06, 91.05, 92.09, 89.33, 90.79, 88.2, 92.26), CONVENTIONAL_HUstd = c(60.16,
62.09, 60.34, 62.26, 63.64, 60.22, 61.94, 59.96), CONVENTIONAL_HUmax = c(76.36,
77.09, 76.41, 80.12, 74.75, 75.74, 73.37, 77.21), CONVENTIONAL_HUQ1 = c(88.81,
88.86, 88.7, 90.04, 87.29, 88.46, 86.17, 90.62), CONVENTIONAL_HUQ2 = c(89.91,
89.89, 89.84, 90.96, 88.12, 89.62, 87.03, 91.26), CONVENTIONAL_HUQ3 = c(91.1,
91.04, 91.1, 91.47, 89.07, 90.79, 87.92, 91.88)), class = "data.frame", row.names = c(NA,
-8L))
Now the code.
In order to get ggplot2
functions working it is better if you reshape your variables. You can move all of your columns to rows with pivot_longer
from tidyverse
and then sketch the plot with geom_bar()
:
df %>% pivot_longer(cols = -Var) %>%
ggplot(aes(x=Var,y=value,fill=name))+
geom_bar(stat = 'identity',position = position_dodge2(width = 0.9,preserve = 'single'))
Output:
That was using your first variable in x-axis, another approach is to move all measures to x-axis:
df %>% pivot_longer(cols = -Var) %>%
ggplot(aes(x=name,y=value,fill=Var))+
geom_bar(stat = 'identity',position = position_dodge2(width = 0.9,preserve = 'single'))
Output:
Update: In order to arrange Var
in the desired order use this code:
#Order x-axis
df %>% pivot_longer(cols = -Var) %>%
mutate(Var=factor(Var,levels = unique(u1$Var),ordered=T)) %>%
ggplot(aes(x=Var,y=value,fill=name))+
geom_bar(stat = 'identity',position = position_dodge2(width = 0.9,preserve = 'single'))
Output: