【问题标题】:Unable to plot a matrix using ggplotr : Error in FUN(X[[i]], ...) : object 'Var1' not found无法使用 ggplotr 绘制矩阵:FUN(X[[i]], ...) 中的错误:找不到对象“Var1”
【发布时间】:2021-08-27 12:31:51
【问题描述】:

下午好,

在 R 下,我尝试绘制以下内容:

m1 <-structure(list(Sensitivity = c(0, 0.701244813278008, 0.701244813278008, 
0.705394190871369, 0.711297071129707, 0.711297071129707, 0.7125, 
0.714876033057851), Specificity = c(0, 0.295454545454545, 0.295454545454545, 
0.318181818181818, 0.347826086956522, 0.347826086956522, 0.355555555555556, 
0.372093023255814), `Pos Pred Value` = c(0, 0.845, 0.845, 0.85, 
0.85, 0.85, 0.855, 0.865), `Neg Pred Value` = c(0, 0.152941176470588, 
0.152941176470588, 0.164705882352941, 0.188235294117647, 0.188235294117647, 
0.188235294117647, 0.188235294117647), Precision = c(0, 0.845, 
0.845, 0.85, 0.85, 0.85, 0.855, 0.865), Recall = c(0, 0.701244813278008, 
0.701244813278008, 0.705394190871369, 0.711297071129707, 0.711297071129707, 
0.7125, 0.714876033057851), F1 = c(0, 0.766439909297052, 0.766439909297052, 
0.770975056689342, 0.774487471526196, 0.774487471526196, 0.777272727272727, 
0.782805429864253), Prevalence = c(0, 0.845614035087719, 0.845614035087719, 
0.845614035087719, 0.83859649122807, 0.83859649122807, 0.842105263157895, 
0.849122807017544), `Detection Rate` = c(0, 0.592982456140351, 
0.592982456140351, 0.596491228070175, 0.596491228070175, 0.596491228070175, 
0.6, 0.607017543859649), `Detection Prevalence` = c(0, 0.701754385964912, 
0.701754385964912, 0.701754385964912, 0.701754385964912, 0.701754385964912, 
0.701754385964912, 0.701754385964912), `Balanced Accuracy ` = c(0, 
0.498349679366277, 0.498349679366277, 0.511788004526594, 0.529561579043114, 
0.529561579043114, 0.534027777777778, 0.543484528156833)), row.names = c("m_metric", 
"X", "X.1", "X.2", "X.3", "X.4", "X.5", "X.6"), class = "data.frame")

print(m1)

print(colnames(m1))

library(ggplot2)
library(reshape2)

print(melt(m1))
ggplot(melt(m1), aes(x=Var1, y=value, col=Var2))+geom_line()+ labs(title = "TITLE", x = "X", y = "Y", color = "COLOR")

我收到以下错误:

No id variables; using all as measure variables
Error in FUN(X[[i]], ...) : object 'Var1' not found
Calls: <Anonymous> ... <Anonymous> -> f -> scales_add_defaults -> lapply -> FUN
Execution halted

我知道melt(m1) 有一些我不明白的地方:

               variable     value
1           Sensitivity 0.0000000
2           Sensitivity 0.7012448
3           Sensitivity 0.7012448
4           Sensitivity 0.7053942
5           Sensitivity 0.7112971
6           Sensitivity 0.7112971
7           Sensitivity 0.7125000
8           Sensitivity 0.7148760
9           Specificity 0.0000000
10          Specificity 0.2954545
11          Specificity 0.2954545
12          Specificity 0.3181818
13          Specificity 0.3478261
14          Specificity 0.3478261
15          Specificity 0.3555556
16          Specificity 0.3720930
17       Pos Pred Value 0.0000000
18       Pos Pred Value 0.8450000
19       Pos Pred Value 0.8450000
20       Pos Pred Value 0.8500000
21       Pos Pred Value 0.8500000
22       Pos Pred Value 0.8500000
23       Pos Pred Value 0.8550000
24       Pos Pred Value 0.8650000
25       Neg Pred Value 0.0000000
26       Neg Pred Value 0.1529412
27       Neg Pred Value 0.1529412
28       Neg Pred Value 0.1647059
29       Neg Pred Value 0.1882353
30       Neg Pred Value 0.1882353
31       Neg Pred Value 0.1882353
32       Neg Pred Value 0.1882353
33            Precision 0.0000000
34            Precision 0.8450000
35            Precision 0.8450000
36            Precision 0.8500000
37            Precision 0.8500000
38            Precision 0.8500000
39            Precision 0.8550000
40            Precision 0.8650000
41               Recall 0.0000000
42               Recall 0.7012448
43               Recall 0.7012448
44               Recall 0.7053942
45               Recall 0.7112971
46               Recall 0.7112971
47               Recall 0.7125000
48               Recall 0.7148760
49                   F1 0.0000000
50                   F1 0.7664399
51                   F1 0.7664399
52                   F1 0.7709751
53                   F1 0.7744875
54                   F1 0.7744875
55                   F1 0.7772727
56                   F1 0.7828054
57           Prevalence 0.0000000
58           Prevalence 0.8456140
59           Prevalence 0.8456140
60           Prevalence 0.8456140
61           Prevalence 0.8385965
62           Prevalence 0.8385965
63           Prevalence 0.8421053
64           Prevalence 0.8491228
65       Detection Rate 0.0000000
66       Detection Rate 0.5929825
67       Detection Rate 0.5929825
68       Detection Rate 0.5964912
69       Detection Rate 0.5964912
70       Detection Rate 0.5964912
71       Detection Rate 0.6000000
72       Detection Rate 0.6070175
73 Detection Prevalence 0.0000000
74 Detection Prevalence 0.7017544
75 Detection Prevalence 0.7017544
76 Detection Prevalence 0.7017544
77 Detection Prevalence 0.7017544
78 Detection Prevalence 0.7017544
79 Detection Prevalence 0.7017544
80 Detection Prevalence 0.7017544
81   Balanced Accuracy  0.0000000
82   Balanced Accuracy  0.4983497
83   Balanced Accuracy  0.4983497
84   Balanced Accuracy  0.5117880
85   Balanced Accuracy  0.5295616
86   Balanced Accuracy  0.5295616
87   Balanced Accuracy  0.5340278
88   Balanced Accuracy  0.5434845

我已经做了另一次尝试,但它没有给出预期的情节:

ggplot(melt(m1), aes(x=variable, y=value, col=variable))+geom_line()+ labs(title = "TITLE", x = "X", y = "Y", color = "COLOR")

例如,敏感度图应该是这样的:

plot(melt(m1)[1:16,2],type="s")

预期的情节应该是

感谢您的帮助!

【问题讨论】:

  • 您的数据框中没有名为 Var1 和 Var2 的列。你只有列变量和值。
  • 是的,我知道错误的含义,melt() 函数仅包含变量和值列。所以我需要根据迭代次数(等于 16)绘制 11 个级别(准确度、灵敏度、...)中的每一个
  • 既不在m1 也不在melt(m1)
  • 有人可以帮忙吗!
  • 你到底想要什么?您显示的最后一个图是敏感性和特异性的混合。您想要一张用于 sens 的图、一张用于规格的图,等等,还是要将所有这些测量值组合在一张图中?

标签: r ggplot2


【解决方案1】:

首先,您必须使用每个模型的名称创建另一列。也许是行的名称:

m1$model <- rownames(m1)
m1 <- melt(m1)

然后你可以绘制成条形图:

ggplot(m1, aes(x=variable, y=value, fill=model))+
  geom_bar(stat = 'identity', position = 'dodge')+ 
  labs(title = "TITLE", x = "X", y = "Y", color = "COLOR")

【讨论】:

  • 谢谢,但我正在寻找多线情节,请看帖子!
【解决方案2】:

我找到了一个可能的解决方案:

m1 <-structure(list(Sensitivity = c(0, 0.701244813278008, 0.701244813278008, 
0.705394190871369, 0.711297071129707, 0.711297071129707, 0.7125, 
0.714876033057851), Specificity = c(0, 0.295454545454545, 0.295454545454545, 
0.318181818181818, 0.347826086956522, 0.347826086956522, 0.355555555555556, 
0.372093023255814), `Pos Pred Value` = c(0, 0.845, 0.845, 0.85, 
0.85, 0.85, 0.855, 0.865), `Neg Pred Value` = c(0, 0.152941176470588, 
0.152941176470588, 0.164705882352941, 0.188235294117647, 0.188235294117647, 
0.188235294117647, 0.188235294117647), Precision = c(0, 0.845, 
0.845, 0.85, 0.85, 0.85, 0.855, 0.865), Recall = c(0, 0.701244813278008, 
0.701244813278008, 0.705394190871369, 0.711297071129707, 0.711297071129707, 
0.7125, 0.714876033057851), F1 = c(0, 0.766439909297052, 0.766439909297052, 
0.770975056689342, 0.774487471526196, 0.774487471526196, 0.777272727272727, 
0.782805429864253), Prevalence = c(0, 0.845614035087719, 0.845614035087719, 
0.845614035087719, 0.83859649122807, 0.83859649122807, 0.842105263157895, 
0.849122807017544), `Detection Rate` = c(0, 0.592982456140351, 
0.592982456140351, 0.596491228070175, 0.596491228070175, 0.596491228070175, 
0.6, 0.607017543859649), `Detection Prevalence` = c(0, 0.701754385964912, 
0.701754385964912, 0.701754385964912, 0.701754385964912, 0.701754385964912, 
0.701754385964912, 0.701754385964912), `Balanced Accuracy ` = c(0, 
0.498349679366277, 0.498349679366277, 0.511788004526594, 0.529561579043114, 
0.529561579043114, 0.534027777777778, 0.543484528156833)), row.names = c("m_metric", 
"X", "X.1", "X.2", "X.3", "X.4", "X.5", "X.6"), class = "data.frame")

rownames(m1)<-NULL

print(m1)

print(colnames(m1))

library(ggplot2)
library(reshape2)

print(melt(m1))

V1=nrow(melt(m1))/length(which(melt(m1)[,"variable"] == "Sensitivity"))

melted=cbind(Var1=rep(which(melt(m1)[,"variable"] == "Sensitivity"),V1),melt(m1))
ggplot(melted, aes(x=Var1, y=value, group=variable, color=variable))+geom_line()+ labs(title = "TITLE", x = "X", y = "Y", color = "COLOR")

plot(melt(m1)[1:16,2],type="s")

这给出了:

【讨论】:

    猜你喜欢
    • 2020-07-22
    • 2018-06-28
    • 2017-12-12
    • 2018-02-11
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多