如何在线图中显示一行NA作为中断?

时间:2013-09-19 14:18:09

标签: r plot ggplot2

我正在尝试使用ggplot2制作一个非常简单的情节。数据包括一行NA(见下文);据我了解,ggplot2忽略NA,因此将所有内容绘制成一条连续线。这通常很好,但在这种特殊情况下,我想在NA行的行中休息一下。有可能吗? 另外,为什么当我执行下面的代码时图例没有出现?

a <- c(0.0000000, 0.5166667, 1.0333333, 1.5500000, 2.0666667, 2.5833333, 3.1000000, 3.6166667, 4.1333333, 4.6500000, 5.1666667, 5.6833333, NA, 5.8333333, 6.3500000, 6.8666667, 7.3833333, 7.9000000, 8.4166667, 8.9333333)
b <- c(10.915762, 61.805769,  9.217244,  0.000000,  4.201053,  4.301649, 30.726614, 15.364261,  0.000000, 15.785032,  0.000000, 5.155405, NA, 0.000000, 11.267748, 17.968796, 12.005235, 57.365812, 34.903027, 34.849097)
df <- data.frame(TIME=a, M79=b)

library(ggplot2)
ggplot(data=df, aes(x=TIME,y=M79)) + geom_line()

2 个答案:

答案 0 :(得分:2)

您可以将geom_pathna.rm = FALSE

一起使用
ggplot(data = df, aes(x = TIME, y = M79)) +
geom_path(na.rm = FALSE)

enter image description here

答案 1 :(得分:1)

要获得换行符,我认为您需要包含另一个变量并使用group美学。例如,

df$na = factor(cumsum(is.na(df$M79)))
ggplot(data=df, aes(x=TIME,y=M79, group=na)) + 
  geom_line()

我真的不明白你对这个传说的评论。传奇应该展示什么?