为什么geom_smooth()会自动排除某些值?

时间:2013-10-02 09:07:37

标签: r ggplot2

我制作了一个像这样的ggplot2图片:

ggplot(dat, aes(x=timepoint, y=y, size=Status, shape=Status)) + 
            geom_point(fill="red") + 
            geom_smooth(method=lm, se=FALSE, size=1, linetype="twodash") +
            facet_grid(Test ~ Batch, scales="free_y")

它给出了: enter image description here

我的代码中还有其他选项来控制图例外观等,但我没有要求geom_smooth()排除某些值,正如您所见,它会自动排除Status=="FAIL"的点! / p>

您可以使用以下数据框生成没有其他选项(但显示相同问题)的此类绘图:

dat <- structure(list(Test = structure(c(2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("PH", 
"ANTIGENIC ACTIVITY BY ELISA", "WATER CONTENT BY µKARL FISCHER"
), class = "factor"), Batch = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("HB07", 
"HB08"), class = "factor"), timepoint = c(0, 1, 2, 3, 0, 1, 2, 
3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3), Status = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("PASS", "FAIL"), class = "factor"), y = c(11.7, 
12.7, 12.8, 17.6, 8.6, 9.6, 16.5, 15.4, 10.1, 9, 11.7, 12.5, 
7.9, 9.3, 15.5, 14.7, 12.9, 10.8, 14.5, 16.5)), .Names = c("Test", 
"Batch", "timepoint", "Status", "y"), row.names = c(NA, -20L), class = "data.frame")

1 个答案:

答案 0 :(得分:4)

通过提供形状,您可以对数据进行分组。如果该组仅由一个成员组成,则geom_smooth无法为该组打印任何内容。

可能你想要这个:

geom_smooth(aes(shape=NA),method=lm, se=FALSE, size=1, linetype="twodash")