Melt 正在用我的数据创造 Cronenburg 怪兽
Melt is creating Cronenburg monsters out of my data
我有一个数据框:
structure(list(diag = structure(list(svm.ca = 0.894598155467721, rpart.ca = 1), .Names = c("svm.ca", "rpart.ca")), kappa = structure(list( svm.ca = 0.838953088986906, rpart.ca = 1), .Names = c("svm.ca", "rpart.ca")), rand = structure(list(svm.ca = 0.871202561348254, rpart.ca = 1), .Names = c("svm.ca", "rpart.ca")), crand = structure(list( svm.ca = 0.715005579974998, rpart.ca = 1), .Names = c("svm.ca", "rpart.ca"))), .Names = c("diag", "kappa", "rand", "crand"), row.names = c("svm", "rpart"), class = "data.frame")
看起来像这样:
diag kappa rand crand
svm.ca 0.8945982 0.8389531 0.8712026 0.7150056
rpart.ca 1 1 1 1
我正在尝试遵循 svm 方向 here(第 3 页),并且我想显示这些值的分组直方图。所以,我融化了我的 df:
df <- melt(as.matrix(histdf))
这让我觉得很奇怪:
X1 X2 value.diag value.kappa value.rand value.crand
1 svm diag 0.8945982 0.8389531 0.8712026 0.7150056
2 rpart diag 1 1 1 1
3 svm kappa 0.8945982 0.8389531 0.8712026 0.7150056
4 rpart kappa 1 1 1 1
5 svm rand 0.8945982 0.8389531 0.8712026 0.7150056
6 rpart rand 1 1 1 1
7 svm crand 0.8945982 0.8389531 0.8712026 0.7150056
8 rpart crand 1 1 1 1
我也试过:
> df <- melt(histdf)
Using as id variables
> df
variable value NA
1 diag 0.8945982 1
2 diag 0.8945982 1
3 kappa 0.8389531 1
4 kappa 0.8389531 1
5 rand 0.8712026 1
6 rand 0.8712026 1
7 crand 0.7150056 1
8 crand 0.7150056 1
来自 :
rownames(histdf)<-c("svm","rpart")
histdf$rn <- row.names(histdf)
df <- melt(histdf, id.vars = "rn")
这给了我与上次尝试相同的输出。我的意思是我应该得到一个名称,值 table,对吧?不变量,值,NA - 怎么绘制它?
我知道这不适用于像这样的分组 ggplot 直方图(我尝试了很多次):
我从classAgreement
重构了原始变量如下:
svm.ca <- structure(list(diag = 0.704225352112676, kappa = 0.589482378854626,
rand = 0.746881287726358, crand = 0.367673479030732), .Names = c("diag",
"kappa", "rand", "crand"))
rpart.ca <- structure(list(diag = 0.732394366197183, kappa = 0.607049228080396,
rand = 0.720724346076459, crand = 0.361367036482943), .Names = c("diag",
"kappa", "rand", "crand"))
这些都是列表。假设我们 rbind
列表中的每个元素,然后将它们放入数据框中:
dd <- as.data.frame(Map(rbind,svm.ca,rpart.ca))
现在这是一个正常的、不包含列表的数据框。 reshape2::melt(dd)
或 tidyr::gather(dd,"variable","value")
,工作正常。
这是最终代码:
df<-as.data.frame(Map(rbind,svm.ca,rpart.ca))
df$Names<-c("svm","rpart")
df.m<-melt(df)
ggplot(df.m, aes(Names, value),group) +
geom_bar(aes(fill = variable), position = "dodge", stat="identity")
我有一个数据框:
structure(list(diag = structure(list(svm.ca = 0.894598155467721, rpart.ca = 1), .Names = c("svm.ca", "rpart.ca")), kappa = structure(list( svm.ca = 0.838953088986906, rpart.ca = 1), .Names = c("svm.ca", "rpart.ca")), rand = structure(list(svm.ca = 0.871202561348254, rpart.ca = 1), .Names = c("svm.ca", "rpart.ca")), crand = structure(list( svm.ca = 0.715005579974998, rpart.ca = 1), .Names = c("svm.ca", "rpart.ca"))), .Names = c("diag", "kappa", "rand", "crand"), row.names = c("svm", "rpart"), class = "data.frame")
看起来像这样:
diag kappa rand crand
svm.ca 0.8945982 0.8389531 0.8712026 0.7150056
rpart.ca 1 1 1 1
我正在尝试遵循 svm 方向 here(第 3 页),并且我想显示这些值的分组直方图。所以,我融化了我的 df:
df <- melt(as.matrix(histdf))
这让我觉得很奇怪:
X1 X2 value.diag value.kappa value.rand value.crand
1 svm diag 0.8945982 0.8389531 0.8712026 0.7150056
2 rpart diag 1 1 1 1
3 svm kappa 0.8945982 0.8389531 0.8712026 0.7150056
4 rpart kappa 1 1 1 1
5 svm rand 0.8945982 0.8389531 0.8712026 0.7150056
6 rpart rand 1 1 1 1
7 svm crand 0.8945982 0.8389531 0.8712026 0.7150056
8 rpart crand 1 1 1 1
我也试过:
> df <- melt(histdf)
Using as id variables
> df
variable value NA
1 diag 0.8945982 1
2 diag 0.8945982 1
3 kappa 0.8389531 1
4 kappa 0.8389531 1
5 rand 0.8712026 1
6 rand 0.8712026 1
7 crand 0.7150056 1
8 crand 0.7150056 1
来自
rownames(histdf)<-c("svm","rpart")
histdf$rn <- row.names(histdf)
df <- melt(histdf, id.vars = "rn")
这给了我与上次尝试相同的输出。我的意思是我应该得到一个名称,值 table,对吧?不变量,值,NA - 怎么绘制它?
我知道这不适用于像这样的分组 ggplot 直方图(我尝试了很多次):
我从classAgreement
重构了原始变量如下:
svm.ca <- structure(list(diag = 0.704225352112676, kappa = 0.589482378854626,
rand = 0.746881287726358, crand = 0.367673479030732), .Names = c("diag",
"kappa", "rand", "crand"))
rpart.ca <- structure(list(diag = 0.732394366197183, kappa = 0.607049228080396,
rand = 0.720724346076459, crand = 0.361367036482943), .Names = c("diag",
"kappa", "rand", "crand"))
这些都是列表。假设我们 rbind
列表中的每个元素,然后将它们放入数据框中:
dd <- as.data.frame(Map(rbind,svm.ca,rpart.ca))
现在这是一个正常的、不包含列表的数据框。 reshape2::melt(dd)
或 tidyr::gather(dd,"variable","value")
,工作正常。
这是最终代码:
df<-as.data.frame(Map(rbind,svm.ca,rpart.ca))
df$Names<-c("svm","rpart")
df.m<-melt(df)
ggplot(df.m, aes(Names, value),group) +
geom_bar(aes(fill = variable), position = "dodge", stat="identity")