多个 ICC 分组条形图 R
multiple ICC grouped bar plot R
我正在尝试为相同定量变量的多个内部 class 系数制作分组条形图,这些变量是在标准化前后根据不同因素(“constructeur”、“coup”... “沥青”)。
这是我的数据集的前 5 行:
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
这里有一些转换,以便 barplot 函数可以使用方便的矩阵
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)
之后的结果是:
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"
但是当运行:
barplot(ICC_intra_observ_3D,beside=T)
the error message: Error in -0.01 * height :
non-numeric argument to binary operator
知道使用默认参数 beside = F
我有一个堆叠条形图似乎可以正常运行,但我需要比较不同的 ICC,因此将它们放在一起更合适。
请注意,我没有尝试使用 ggplot()
,因为重组我的输入数据集似乎更加困难,但欢迎提出任何建议。
感谢您的帮助
我建议采用 tidyverse
方法。使用您的最终数据作为 df
,这里的代码:
首先是数据:
#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))
现在是代码。
为了使 ggplot2
函数正常工作,最好重塑变量。您可以使用 pivot_longer
从 tidyverse
将所有列移动到行,然后使用 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'))
输出:
那是在 x-axis 中使用您的第一个变量,另一种方法是将所有度量移至 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'))
输出:
更新: 要按所需顺序排列 Var
,请使用此代码:
#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'))
输出:
我正在尝试为相同定量变量的多个内部 class 系数制作分组条形图,这些变量是在标准化前后根据不同因素(“constructeur”、“coup”... “沥青”)。 这是我的数据集的前 5 行:
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
这里有一些转换,以便 barplot 函数可以使用方便的矩阵
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)
之后的结果是:
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"
但是当运行:
barplot(ICC_intra_observ_3D,beside=T)
the error message: Error in -0.01 * height : non-numeric argument to binary operator
知道使用默认参数 beside = F
我有一个堆叠条形图似乎可以正常运行,但我需要比较不同的 ICC,因此将它们放在一起更合适。
请注意,我没有尝试使用 ggplot()
,因为重组我的输入数据集似乎更加困难,但欢迎提出任何建议。
感谢您的帮助
我建议采用 tidyverse
方法。使用您的最终数据作为 df
,这里的代码:
首先是数据:
#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))
现在是代码。
为了使 ggplot2
函数正常工作,最好重塑变量。您可以使用 pivot_longer
从 tidyverse
将所有列移动到行,然后使用 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'))
输出:
那是在 x-axis 中使用您的第一个变量,另一种方法是将所有度量移至 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'))
输出:
更新: 要按所需顺序排列 Var
,请使用此代码:
#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'))
输出: