0

I have a dataframe:

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

It looks like this:

              diag     kappa      rand     crand
svm.ca   0.8945982 0.8389531 0.8712026 0.7150056
rpart.ca         1         1         1         1

I'm trying to follow the svm directions here (page 3), and I want to show a grouped histogram of these values. So, I melt my df:

df <- melt(as.matrix(histdf))

Which gives me this monstrosity here:

     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

I've also tried:

> 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

And from this:

rownames(histdf)<-c("svm","rpart")
histdf$rn <- row.names(histdf)
df <- melt(histdf, id.vars = "rn")

Which gives me the same output as the previous attempt. I mean I should be getting a Name, value table, right? Not variable, value, NA - howma gonna plot that?

I know this isn't going to work for a grouped ggplot histogram like this (I have attempted a lot): https://stackoverflow.com/a/18162330/1152809

Community
  • 1
  • 1
Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
  • 3
    It is because the columns are all list. Please check the `str(histdf)` What is your expected output. Perhaps `melt(sapply(histdf, unlist))` – akrun Dec 03 '16 at 16:42
  • 4
    It might be best to go upstream and think about how you ended up with this data structure, rather than coming up with a fix for this particular problem ... – Ben Bolker Dec 03 '16 at 16:47
  • @BenBolker I ran classAgreement on svm and rpart models, as suggested in this svm vignette: ftp://cran.r-project.org/pub/R/web/packages/e1071/vignettes/svmdoc.pdf (page 3), and combined them into a dataframe with `histdf<-as.data.frame(rbind(svm.ca,rpart.ca))` – Travis Heeter Dec 03 '16 at 16:56

1 Answers1

2

I reconstructed the original variables from classAgreement as follows:

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

These are each lists. Suppose we rbind each element in the lists, then put them into a data frame:

dd <- as.data.frame(Map(rbind,svm.ca,rpart.ca))

This is now a normal, non-list-containing data frame. reshape2::melt(dd), or tidyr::gather(dd,"variable","value"), work fine.


Here's the final code:

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")
Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453