1

I have a quick question about R. I am trying to make a layered histogram from some data I am pulling out of files but I am having a hard time getting ggplot to work with me. I keep getting this error and I have been looking around for an answer but I haven't seen much.

    Error: ggplot2 doesn't know how to deal with data of class uneval
    Execution halted

Here is a brief look at my program so far.

library("ggplot2")
ex <- '/home/Data/run1.DOC'
ex2 <- '/home/Data/run2.DOC'
...   
ex<- read.table(ex,header=TRUE)
ex2<- read.table(ex2,header=TRUE)
...
colnames(ex) <- c(1:18)
colnames(ex2) <- c(1:18)
...
Ex <- c(ex$'14')
Ex2 <- c(ex2$'14')
...
ggplot()+
geom_histogram(data = Ex, fill = "red", alpha = 0.2) +
    geom_histogram(data = Ex2, fill = "blue", alpha = 0.2) 

And my data is in the files and look a bit like this:

head(ex,10)
        1     2      3     4      5  6   7   8     9  10    11    12
    1  1:28   400   0.42   400   0.42  1   1   2  41.8   0   0.0   0.0
    2  1:96  5599  39.99  5599  39.99 34  42  50 100.0 100 100.0 100.0
    3  1:53   334   0.63   334   0.63  1   2   2  62.1   0   0.0   0.0
    4  1:27  6932  49.51  6932  49.51 48  52  57 100.0 100 100.0 100.0
    5  1:36 27562 124.15 27562 124.15 97 123 157 100.0 100 100.0 100.0
    6  1:14  2340  16.71  2340  16.71 13  17  21 100.0 100 100.0  95.7
    7  1:96  8202  49.71  8202  49.71 23  43  80 100.0 100 100.0 100.0
    8  1:34  3950  28.21  3950  28.21 22  33  36 100.0 100 100.0 100.0
    9  1:60  5563  24.62  5563  24.62 11  24  41 100.0 100  96.5  75.2
    10 1:06  1646   8.11  1646   8.11  7   8  13 100.0 100  87.2  32.0
          13    14    15    16   17   18
    1    0.0   0.0   0.0   0.0  0.0  0.0
    2   93.6  82.9  57.9  24.3  0.0  0.0
    3    0.0   0.0   0.0   0.0  0.0  0.0
    4  100.0  97.1  87.1  57.1  0.0  0.0
    5  100.0 100.0 100.0 100.0 88.3 71.2
    6   40.0   0.0   0.0   0.0  0.0  0.0
    7   81.2  66.7  54.5  47.9 29.1  0.0
    8   76.4  55.7   0.0   0.0  0.0  0.0
    9   57.5  35.4  26.5   4.4  0.0  0.0
    10   0.0   0.0   0.0   0.0  0.0  0.0

But much larger. This means that ex and ex2 will be a percentage from 0 to 100. The colnames line changes the column heads like %_above_30 to something R likes better so I change it to number each column name.

Does anyone know/see the problem here because I am not really getting it. Thanks!!

smillig
  • 5,073
  • 6
  • 36
  • 46
Stephopolis
  • 1,765
  • 9
  • 36
  • 65
  • 1
    Please provide a sample og what your data looks like. What do `ex` and `ex2` look like? Use `head(ex, 10)` and `head(ex2, 10)` to show us. – Tyler Rinker Jul 30 '12 at 18:13
  • @TylerRinker Ok, I have added the head(ex,10). Does that help? – Stephopolis Jul 30 '12 at 18:25
  • It's a bit messy in that you have it spillin gout onto two lines. Try `dput(head(ex, 10))` and `dput(head(ex2))`. Do them both because that provides us with sample data to work with. – Tyler Rinker Jul 30 '12 at 18:34
  • With that data set can you even get one histogram to run? – Tyler Rinker Jul 30 '12 at 18:36
  • @TylerRinker I don't think you will want to see the output for dput. It is excessive. And no, I can't. I have made some changes to the code which I am going to update in a second... – Stephopolis Jul 30 '12 at 18:37
  • the dput I mention above only gives you 10 rows of data b/c I use head first. – Tyler Rinker Jul 30 '12 at 18:39
  • @Look at my example and see how I'm sending one column `prediction` rather than the whole data frame. You haven't decide which column you're wanting a histogram for. – Tyler Rinker Jul 30 '12 at 18:40
  • There are about 798653 characters, just that show up in my terminal that cannot be pasted here if I do your dput command. ..."39", "390", "391", "392", "393", "394", "395", "396", "397", "398", "399", "4", "40", "400", "401", "402", "403", "404", "405", "406", "407", "408", "409", "41", "410", "411", "412", "414", "415", "416", "417", "418", "419", "42", "420", "421", "422", "423", "424", "425", "426", "427", "428", "429", "43", "430", "431", "432", "433", "434", "435", "436", "437", "438", "439", "44", "440", "441", "442", "443", "444", "445", "446", "447", "448", "449", "45", "455"... – Stephopolis Jul 30 '12 at 18:46
  • My file is rather large, as I mentioned in the question – Stephopolis Jul 30 '12 at 18:47
  • @TylerRinker I am choosing the columns here: Ex <- c(ex$'14') Ex2 <- c(ex2$'14') Aren't I? – Stephopolis Jul 30 '12 at 18:48

1 Answers1

4

Maybe try combining the two data frames in one and supply that to one geom_histogram:

#maybe reshape it something like this (base reshape or the 
#reshape package may be a better tool)
dat <- data.frame(rbind(ex, ex2), 
    colvar=factor(c(rep("ex", nrow(ex)), rep("ex2", nrow(ex2))))

ggplot(data = dat, fill = colvar)+
    geom_histogram(position="identity", alpha = 0.2)

This is untested as your code isn't reproducible (please see this link on how to make a reproducible example).

Here's the idea I'm talking about with a reproducible example:

library(ggplot2) 
path = "http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/SAheart.data" 

saheart <- read.table(path, sep=",",head=T,row.names=1) 
fmla <- "chd ~ sbp + tobacco + ldl + adiposity + famhist + typea + obesity" 
model <- glm(fmla, data=saheart, family=binomial(link="logit"), 
    na.action=na.exclude) 
dframe <- data.frame(chd=as.factor(saheart$chd), 
    prediction=predict(model, type="response")) 

ggplot(dframe, aes(x=prediction, fill=chd)) + 
    geom_histogram(position="identity", binwidth=0.05, alpha=0.5) 
Community
  • 1
  • 1
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519