how to plot NA gaps using ggplot2

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

As an example see:

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

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

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

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?

Answers


This happens because you set both x and y to NA. (And the ggplot2 behaviour is more sensible IMHO than the base plot behaviour.) If only the y values are NA, you get the desired behaviour. Here is a workaround:

df$g <- cumsum(apply(df, 1, anyNA))
ggplot(data=df,aes(x,y, group=g))+geom_line()


Replace geom_line with geom_path.

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


Need Your Help

Linq orderby national character sets

linq internationalization order generic-list

I have a List which i OrderBy alfabetically on a property.

Cache UIViewController for presentation/pushing

ios model-view-controller caching uiviewcontroller uinavigationcontroller

Is it possible/practical/beneficial to cache UIViewControllers at the startup of an app so that when they need to be presented (or pushed by a UINavigationController), the memory does not have to be

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.