添加新参数时,绘图功能出错

时间:2014-12-11 01:05:05

标签: r function plot polygon

我是一名初学者,使用R Studio和R版本3.1.2(2014-10-31) - "南瓜头盔"在Windows 7中。

数据我正在使用......

   > dput(head(data,20))
structure(list(case = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1), age = c(37, 42, 44, 40, 26, 29, 42, 26, 
18, 56, 29, 66, 71, 26, 30, 48, 39, 65, 65, 48), bmi = c(25.95, 
29.07, 27.63, 27.4, 25.34, 31.38, 25.08, 28.01, 24.69, 25.06, 
27.68, 23.51, 29.86, 21.72, 25.95, 22.86, 23.53, 21.3, 33.2, 
29.39), ord.bmi = c(3, 3, 3, 3, 3, 4, 3, 3, 2, 3, 3, 2, 3, 2, 
3, 2, 2, 2, 4, 3), alcohol = c(2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 
2, 1, 1, 1, 1, 1, 2, 2, 1, 1), tobacco = c(1, 1, 1, 2, 2, 1, 
2, 2, 1, 1, 2, 2, 1, 2, 1, 1, 2, 2, 1, 1), dent.amalgam = c(1, 
2, 2, 1, 1, 1, 2, 2, 1, 1, 1, 2, 1, 2, 1, 2, 1, 1, 2, 1), exp.years = c(7, 
9, 9, 5, 2, 10, 15, 5, 1, 40, 10, 50, 50, 1, 12, 22, 22, 30, 
40, 30), mn = c(0, 0, 0, 1.5, 1.5, 1, 0, 0, 0.5, 0.5, 1, 1, 0, 
0, 0, 0.5, 0, 0.5, 2, 1), bn = c(2.5, 5, 2.5, 2, 1.5, 4, 2, 1.5, 
4.5, 4.5, 2.5, 2, 6, 2, 5, 4, 1, 1.5, 7, 1.5), ln = c(0.5, 1.5, 
0, 2, 1.5, 1.5, 1, 0.5, 2, 2, 1, 1, 4.5, 0, 2, 1, 3, 2, 3, 3), 
    pn = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 
    0.5, 0.5, 0, 0), cc = c(0, 1, 0, 2, 2, 4, 1, 1.5, 4.5, 2, 
    0, 3.5, 2, 1.5, 2, 1.5, 0.5, 1, 2, 1.5), kr = c(0, 0, 0, 
    0, 0, 0, 0.5, 0, 0.5, 1, 0, 0.5, 1.5, 0.5, 0.5, 0.5, 0, 0.5, 
    0, 0), kl = c(0.5, 2, 0, 1.5, 1.5, 0, 2, 0, 2, 2, 0, 1.5, 
    1.5, 1, 4, 3, 2, 3.5, 4.5, 2)), .Names = c("case", "age", 
"bmi", "ord.bmi", "alcohol", "tobacco", "dent.amalgam", "exp.years", 
"mn", "bn", "ln", "pn", "cc", "kr", "kl"), row.names = c(NA, 
20L), class = "data.frame")

我正在绘制两种不同的密度(我使用density.a <- lapply(data[which(data$case == 0),], density)density.b <- lapply(data[which(data$case == 1),], density)),一切似乎都运行正常:

plot.densities <- function(sample.a, sample.b){  # declaring the function arguments
    for(i in seq(length(sample.a))){  # for every element in the first argument (expected equal lengths)
        plot(range(sample.a[[i]]$x, sample.b[[i]]$x), # generate a plot
             range(sample.a[[i]]$y, sample.b[[i]]$y), 
             xlab = names(sample.a[i]), ylab = "Density", main = paste(names(sample.a[i]), "density plot"))
        lines(sample.a[[i]], col = "red")  # red lines
        lines(sample.b[[i]], col = "green") #green lines
    }
}

当我调用该函数时,我会得到这样的情节: GOOD (?) PLOT

然后,如果我想填充两条曲线之间的线,我添加了多边形函数,如下所示:

filled.plot <- function(sample.a, sample.b){  # declaring the function arguments
    for(i in seq(length(sample.a))){  # for every element in the first argument (expected equal lengths)
        plot(range(sample.a[[i]]$x, sample.b[[i]]$x), # generate a plot
             range(sample.a[[i]]$y, sample.b[[i]]$y), 
             xlab = names(sample.a[i]), ylab = "Density", 
             main = paste(names(sample.a[i])))
        lines(sample.a[[i]], col = "red")  # red lines
        lines(sample.b[[i]], col = "green") #green lines
        polygon(x = c(range(sample.a[[i]]$x, sample.b[[i]]$x), 
                      rev(range(sample.a[[i]]$x, sample.b[[i]]$x))), 
                y = c(range(sample.a[[i]]$y, sample.b[[i]]$y), 
                      rev(range(sample.a[[i]]$x, sample.b[[i]]$x))),
                col = "skyblue")         
    }
}

但是当我调用filled.plot函数时,我会得到这样的情节:

BAD PLOT

我被困了,一些帮助就好了!

提前致谢。

1 个答案:

答案 0 :(得分:1)

尝试使用ggplot(我已将行11:20的大小写值更改为2):

ggplot()+
geom_density(data=testdf[testdf$case==1,], aes(age),fill='red', alpha=0.5)+
geom_density(data=testdf[testdf$case==2,], aes(age), fill='green', alpha=0.5)

enter image description here