函数内的ggplot:未找到数据

时间:2016-03-24 14:18:21

标签: r function ggplot2

我用ggplot()编写了一个函数:

plot_y_title_obs<-function(mydata,mylimit,mybreak,title,ytitle){
    p1<-ggplot(data=mydata, aes(x=mydata[,2],y=mydata[,1]))+
            geom_point(colour="dodgerblue4",size=1.5) + 
            geom_line(aes(x=mydata[,2],group=mydata[,16],y=mydata[,1]),color="dodgerblue",size=0.7) + 
            labs(x="Age at testing (months)",y=ytitle)+
            theme_bw()+
            scale_y_continuous(limit=mylimit,breaks=mybreak)+
            scale_x_continuous(breaks=c(24,36,48,60,72,84))+
            ggtitle(title)+
            theme(axis.title.y=element_text(face="bold"),
                  axis.title.x=element_blank(),
                  panel.border = element_blank(),
                  axis.text.x=element_blank(),
                  panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
                  panel.background = element_blank(), axis.line = element_line(colour = "black"),
                  plot.title = element_text(size = rel(1.8),face = "bold", colour = "Black",family="Palatino"),
                  axis.title = element_text(size = rel(1.8),family = "Times New Roman"),
                  axis.text=element_text(size = rel(1.5)),
                  plot.margin=unit(c(1,-0.2,1,1), "cm"))
    return(p1)
}

我用我的数据

尝试了
plot_y_title_obs(mydata=data.test,mylimit=c(20,135),mybreak=c(0,25,50,75,100,125),title="Deletion: observed",ytitle="Verbal IQ")

我一直收到错误:“eval中的错误(expr,envir,enclos):找不到对象'mydata' “

但是,如果我在函数外部指定mydata=data.test,则该函数可以正常工作。

我查了一些类似的问题,但它们更多的是关于函数内的变量。我不知道为什么数据无法分配到函数中。

我的data.test如下:

> dput(data.test)
structure(list(VIQ = c(50L, 68L, 95L, 105L, 105L, 95L, 97L, 105L, 
100L, 111L, 65L, 95L, 82L, 101L, 104L, 105L, 72L, 94L, 80L, 76L
), cogn.age = c(20L, 24L, 36L, 48L, 60L, 45L, 49L, 60L, 73L, 
84L, 24L, 36L, 36L, 48L, 60L, 72L, 46L, 61L, 85L, 38L), individual = 1:20, 
    age = c(20L, 24L, 36L, 48L, 60L, 45L, 49L, 60L, 73L, 84L, 
    24L, 36L, 36L, 48L, 60L, 72L, 46L, 61L, 85L, 38L), family_type = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor"), 
    sex = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L), .Label = c("female", 
    "male"), class = "factor"), dx.clinical_asd = c(0, 0, 0, 
    0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), dx.adhd = c(0, 
    0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0), 
    dx.anxiety_ocd_phobia = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), dx.articulation_disorder = c(1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
    dx.behavior_disorder = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), dx.coordination_disorder = c(1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1), 
    dx.language_disorder = c(1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 
    1, 1, 1, 1, 1, 0, 0, 0, 0), dx.mr_id = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), dx.number_of_diagnoses = c(2L, 
    2L, 1L, 2L, 3L, 2L, 2L, 4L, 4L, 4L, 2L, 1L, 1L, 1L, 1L, 2L, 
    3L, 2L, 4L, 4L), Cluster_ID = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 10L), fitted = c(68.4998947535235, 
    71.9147367087161, 82.1592625742941, 92.403788439872, 102.64831430545, 
    91.6892415658348, 93.6358358583402, 98.9889701627301, 105.315401613373, 
    110.668535917763, 76.852384975948, 84.5459212555003, 84.5459212555003, 
    92.2394575350527, 99.9329938146051, 107.626530094157, 77.2019204210415, 
    82.1713634603668, 90.1224723232873, 76.2368729217388), fix_fitted = structure(c(69.5314339165969, 
    71.501639107495, 77.4122546801892, 83.3228702528834, 89.2334858255775, 
    81.8452163597098, 83.8154215506079, 89.2334858255775, 95.6366526959962, 
    101.054716970966, 71.501639107495, 77.4122546801892, 77.4122546801892, 
    83.3228702528834, 89.2334858255775, 95.1441013982717, 82.3377676574343, 
    89.7260371233021, 101.54726826869, 78.3973572756382), .Dim = c(20L, 
    1L), .Dimnames = list(c("2", "5", "7", "9", "11", "13", "15", 
    "18", "21", "25", "40", "43", "44", "46", "48", "50", "64", 
    "67", "72", "112"), NULL))), .Names = c("VIQ", "cogn.age", 
"individual", "age", "family_type", "sex", "dx.clinical_asd", 
"dx.adhd", "dx.anxiety_ocd_phobia", "dx.articulation_disorder", 
"dx.behavior_disorder", "dx.coordination_disorder", "dx.language_disorder", 
"dx.mr_id", "dx.number_of_diagnoses", "Cluster_ID", "fitted", 
"fix_fitted"), row.names = c(2L, 5L, 7L, 9L, 11L, 13L, 15L, 18L, 
21L, 25L, 40L, 43L, 44L, 46L, 48L, 50L, 64L, 67L, 72L, 112L), class = "data.frame")

0 个答案:

没有答案