0

我正在尝试为相同定量变量的多个类内系数制作分组条形图,在不同因素(“constructeur”、“coup”...“pitch”)的标准化前后估计。这是我的数据集的前 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)

错误消息:-0.01 * height 中的错误:二元运算符的非数字参数

知道使用默认参数beside = F我有一个堆叠的条形图似乎可以正常运行,但我需要比较不同的 ICC,所以将它们放在旁边更合适。

请注意,我没有尝试过,ggplot()因为重组我的输入数据集似乎更加困难,但欢迎提出任何建议。

谢谢你的帮助

4

1 回答 1

0

我会建议一种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_longerfrom将所有列移动到行,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 轴上使用您的第一个变量,另一种方法是将所有度量移动到 x 轴:

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'))

输出:

在此处输入图像描述

于 2020-08-27T17:29:26.220 回答