如何使用ggplot2绘制NA间隙 [英] how to plot NA gaps using ggplot2

查看:50
本文介绍了如何使用ggplot2绘制NA间隙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中进行基本绘图时,如果ggplot2数据序列中存在NA,则将绘制间隙:

In basic plotting in R if there is an NA in a data series ggplot2 a gap will be plotted:

作为示例,请参见:

df=data.frame(x=c(1:10),y=c(1:10))
df[5:7,]=NA
plot(df,type="l")

但是,ggplot2会删除缺失的值并绘制一条直线没有差距.

However, ggplot2 removes the missing values and plots a straight line with no gap.

ggplot(data=df,aes(x,y))+geom_line()

我想保持差距.它们是我正在使用的数据中的宝贵信息.有没有一种简单的方法可以告诉ggplot2停止忽略差距?

I'd like to keep the gaps. They are valuable info in the data I am working with. Is there a simple way to tell ggplot2 to stop ignoring gaps?

推荐答案

geom_path 替换 geom_line .

library(ggplot2)
ggplot(data = df, aes(x, y)) +
  geom_path()

这篇关于如何使用ggplot2绘制NA间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆