6

我有一个数据集,其中包含多个 NA 值。绘制此数据时,ggplot 的 geom_line() 选项将跨 NA 值的线连接起来。有什么方法可以让 ggplot 跳过加入跨 NA 值的行?

编辑:向所有相关人员致歉。我在操作数据框时犯了一个错误。我发现了我的问题。创建子集时,我的 x 轴不连续。缺失的数据没有被 NA 替换,因此数据被链接,因为在行之间的子集中没有创建 NA。

4

1 回答 1

16

geom_line确实为列中的NAs打断y,但它跨列NA中的值连接x

# 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()

在此处输入图像描述

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

ggplot(d, aes(x=dependant, y=independant)) + geom_line() +
  coord_flip()

在此处输入图像描述

大概你的代码是:

ggplot(crew.twelves, aes(x=laffcu, y=as.POSIXlt(date)) + geom_line() +
  coord_flip()
于 2013-08-10T17:39:44.580 回答