ggplot2 geom_line()跳过NA值

Reu*_*hew 3 r ggplot2

我有一个数据集,其中包含多个NA值.绘制此数据时,ggplot的geom_line()选项可以跨NA值连接线.有没有办法让ggplot跳过跨越NA值的线?

编辑:向所有参与者致歉.我在操纵数据框时犯了一个错误.我想出了我的问题.当我创建子集时,我的x轴不连续.缺少的数据没有被NA替换,因此数据被链接,因为在行之间的子集中没有创建NA.

Dre*_*een 12

geom_line确实为列中的NAs 创建了中断y,但它连接NAx列中的值.

# Set up a data frame with NAs in the 'x' column
independant <- c(0, 1, NA, 3, 4)
dependant <- 0:4
d <- data.frame(independant=independant, dependant=dependant)

# Note the unbroken line
ggplot(d, aes(x=independant, y=dependant)) + geom_line()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我假设你的NA价值观在你的as.POSIXlt(date).如果是这样,一种解决方案是将NA值映射到列y,然后用于coord_flip使y轴水平:

ggplot(d, aes(x=dependant, y=independant)) + geom_line() +
  coord_flip()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

大概你的代码是:

ggplot(crew.twelves, aes(x=laffcu, y=as.POSIXlt(date)) + geom_line() +
  coord_flip()
Run Code Online (Sandbox Code Playgroud)

  • 欢迎来到SO.请注意,提供任何有用的答案(通过单击答案顶部附近的小向上箭头)并"接受"最佳答案(通过单击答案的绿色箭头)是一个很好的形式.这样做将激励未来的用户回答您的问题. (2认同)
  • 如果你相信的话,我需要有 15 的声誉才能支持你的回复。我向你保证,当我的声望达到 15 时,我会回到这里来支持你的回复。我现在的声望是6。 (2认同)