【问题标题】:R: Overlapping ggplotsR:重叠的ggplots
【发布时间】:2012-07-30 17:59:40
【问题描述】:

我有一个关于 R 的快速问题。我正在尝试从我从文件中提取的一些数据中制作分层直方图,但我很难让 ggplot 与我一起工作。我不断收到此错误,我一直在寻找答案,但我没有看到太多。

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

这是迄今为止我的程序的简要介绍。

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) 

我的数据在文件中,看起来有点像这样:

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

但要大得多。这意味着 ex 和 ex2 将是从 0 到 100 的百分比。 colnames 行将 %_above_30 之类的列标题更改为 R 更喜欢的内容,因此我将其更改为对每个列名进行编号。

有没有人知道/看到这里的问题,因为我并没有真正理解它。 谢谢!!

【问题讨论】:

  • 请提供样本以及您的数据是什么样的。 exex2 看起来像什么?使用head(ex, 10)head(ex2, 10) 向我们展示。
  • @TylerRinker 好的,我已经添加了头部(例如,10)。这有帮助吗?
  • 有点乱,因为你把痛风泼到了两条线上。试试dput(head(ex, 10))dput(head(ex2))。两者都做,因为这为我们提供了可以使用的示例数据。
  • 有了这个数据集,你甚至可以运行一个直方图吗?
  • @TylerRinker 我认为您不会希望看到 dput 的输出。这是过度的。不,我不能。我已经对代码进行了一些更改,我将在一秒钟内更新......

标签: r ggplot2 histogram


【解决方案1】:

也许可以尝试将两个数据帧合二为一,并将其​​提供给一个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 link 了解如何制作可重现的示例)。

这是我通过一个可重现的例子谈论的想法:

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) 

【讨论】:

    猜你喜欢
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    相关资源
    最近更新 更多