我有一个数据集,其中包含多个NA值。在绘制此数据时,ggplot的geom_line()选项会连接跨越NA值的线条。有没有办法让ggplot跳过连接跨越NA值的线条?
编辑:对于所有涉及的人,我向你们表示千万个歉意。我在操作数据框时犯了一个错误。我找到了我的问题所在。当我创建子集时,我的x轴不是连续的。由于子集之间没有创建NA,因此缺失数据没有被替换为NA,所以数据被链接起来了。
geom_line
会对y
列中的NA
进行分割,但是会跨越x
列中的NA
值进行连接。# 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()
crew.twelves
数据框,所以无法对您的代码做出太多贡献)。有关良好可重现示例的说明,请参见此处:https://dev59.com/eG025IYBdhLWcg3whGSx - Drew Steen