我有一个数据框:
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
来自this:
rownames(histdf)<-c("svm","rpart")
histdf$rn <- row.names(histdf)
df <- melt(histdf, id.vars = "rn")
这给了我与之前尝试相同的输出。我的意思是我应该得到一个名字,价值表,对吗?不变,价值,NA - howma会描绘出来的吗?
我知道这对于像这样的分组ggplot直方图不起作用(我已经尝试了很多):https://stackoverflow.com/a/18162330/1152809
答案 0 :(得分:2)
我从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")