R script ggplot:if(nrow(layer_data)== 0)return()出错:参数长度为零

时间:2016-02-19 10:23:07

标签: r ggplot2

我正在尝试在R中制作一个镶板图,显示单个数据和每个面板的方法。这个原始图可以工作,但是当我尝试根据另一个变量更改单个数据的颜色代码中断时,我收到错误消息:if(nrow(layer_data)== 0)return()中的错误:参数是长度为零。

我已粘贴1)样本数据,2)有效的原始代码和3)带有不起作用的颜色变量的代码。

我真的很感激你能提供任何帮助来解决这个问题。

谢谢! 娜塔莉

    library(ggplot2)

    sample <- structure(list(id = c(2108L, 2123L, 2718L, 2760L, 2133L, 2162L,   2216L, 2234L, 2298L, 2108L, 2123L, 2718L, 2760L, 2133L, 2162L, 2216L, 2234L, 2298L), Timeline_Code = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Baseline", "Post"), class = "factor"), Treatment_Code = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Health Ed", "MBSR"), class = "factor"), Group = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("Baseline/Health Ed", "Baseline/MBSR", "Post/Health Ed", "Post/MBSR"), class = "factor"), Y_mb = c(1.58, 1.66, 2.27, 1.87, 1.75, 1.65, 1.82, 1.12, 2.05, 2.29, 2.11, 1.55, 1.72, 2.28, 1.71, 1.69, 1.98, 2.28), slope = c(0.71, 0.45, -0.72, -0.15, 0.53, 0.06, -0.13, 0.86, 0.23, 0.71, 0.45, -0.72, -0.15, 0.53, 0.06, -0.13, 0.86, 0.23), slope_group = structure(c(2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L), .Label = c("NEGATIVE", "POSITIVE"), class = "factor")), .Names = c("id", "Timeline_Code", "Treatment_Code", "Group", "Y_mb", "slope", "slope_group"), class = "data.frame", row.names = c(NA, -18L))

这是有效的

    p <- ggplot(data = sample, aes(x = Timeline_Code, y = Y_mb, group = id)) + geom_line(col = "dark grey") + scale_x_discrete(limits=c("Baseline","Post")) + facet_grid(. ~Treatment_Code) + stat_summary(aes(group = 1)) + stat_smooth(aes(group = 1), method = "lm", size = 3, col = "black", se = FALSE)+ labs(x="", y = "Y_mb") + theme_classic()

    p 

现在我想保留上图的结构,但是根据斜率是正还是负来改变各条线的颜色。我将col = slope_group添加到ggplot。部分代码可以工作并绘制图形,但不会产生均值。似乎是stat_summary的问题,因为在此之前一切正常。

    p <- ggplot(data = sample, aes(x = Timeline_Code, y = Y_mb, group = id, col = slope_group)) + geom_line() + scale_x_discrete(limits=c("Baseline","Post")) + facet_grid(. ~Treatment_Code) + stat_summary(aes(group = 1)) + stat_smooth(aes(group = 1), method = "lm", size = 3, col = "black", se = FALSE)+ labs(x="", y = "Y_mb") + theme_classic() 

    p

错误消息

Error in if (nrow(layer_data) == 0) return() : argument is of length zero

会话信息

# sessionInfo()
#   R version 2.15.2 (2012-10-26)
#   Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

#locale:
#[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

#attached base packages:
#[1] stats     graphics  grDevices utils     datasets  methods   base     

#other attached packages:
#[1] ggplot2_0.9.3.1

#loaded via a namespace (and not attached):
# [1] colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4       grid_2.15.2       
# [5] gtable_0.1.2       labeling_0.2       MASS_7.3-22        munsell_0.4       
# [9] plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5 reshape2_1.2.2    
# [13] scales_0.2.3       stringr_0.6.2      tools_2.15.2  

0 个答案:

没有答案