Tou Mou
Tou Mou

Reputation: 1274

Unable to plot a matrix using ggplotr : Error in FUN(X[[i]], ...) : object 'Var1' not found

Good afternoon ,

Under R , i tried to plot the following :

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

I got the following error :

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

I know that there is something i don't understand with 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

I already did another try but it doesn't give the expected plots :

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

enter image description here

For example , the plot of sensitivity should be something like this :

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

enter image description here

The expected plot should be :

enter image description here

Thank you for help !

Upvotes: 0

Views: 139

Answers (2)

Tou Mou
Tou Mou

Reputation: 1274

I had found a possible solution :

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

This gives :

enter image description here

Upvotes: 0

Pedro Alencar
Pedro Alencar

Reputation: 1089

First you have to create another column with the name of each model. Maybe with the names of the rows:

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

Then you can plot as a bar plot:

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

Upvotes: 1

Related Questions