How to plot data containing NA's as "gaps" in geom_line

2,575 views
Skip to first unread message

Thiago V. dos Santos

unread,
Feb 18, 2014, 8:21:29 PM2/18/14
to ggp...@googlegroups.com
Hi all,

I'm having trouble plotting data containing NA's with ggplot.

I have several variables on my data.frame df1. Basically, I would like to create time-series (geom_line) plots of two of them: "latent" and "LE.W.M2.".

Latent line should be black, solid, and LE.W.M2. should be black and dashed. Also, I need a legend identifying each line.

However, LE.W.M2. contains some NA values. This is not a problem, as long as the NA's are shown as gaps in the lines. I would expect ggplot2 do this automatically, but it simply refuses to plot this line. Please run the code:

---------------------------------------------------------------------------------------------------------------------------------------------------------------
## Load package to
library(reshape2) # melt data.frames
library(ggplot2)  # create plots
library(scales)   # improve dates on graph axis
library(plyr)     # subset data while plotting
library(RCurl)    # download data

# Read data and define NA
file <- read.csv(textConnection(link), header=T, sep=';', stringsAsFactors = FALSE, as.is=TRUE)
file[file==-9999] <- NA

# Get date from character
file$date <- as.Date(as.character(file$date), "%m/%d/%Y")

# Open model data
mod <- read.csv(textConnection(link2), sep="", header=T)
tmp <- paste(mod$year,"-", mod$month, "-", mod$day, sep='')
mod$date <- as.Date(tmp)

# Reorder DF and discard old date characteres
mod <- mod[c(9,4,5,6,7,8)]

# Merge datasets by date
data <- merge (file, mod, by='date')
names(data)

# Then melt by date
df1 <- melt (data, id='date')

#################################################################
# Create Plot
#################################################################
p <- ggplot(df1, aes(x=date, y=value, colour=variable)) + 
       scale_x_date(breaks = '1 month', labels = date_format('%b-%y'),
                                limits = as.Date(c('2012-01-01','2013-01-01'))) +
       theme_bw(16) + 
       theme(panel.margin = unit(0,'lines'), axis.title.x = element_blank()) 

# Here I create the two geom_lines, subsetting desired data "one the fly"
comparison <- p + geom_line(subset = .(variable == 'latent'),   linetype='solid', colour='black') +
                                 geom_line(subset = .(variable == 'LE.W.M2.'), linetype='dashed',colour='black') +
                                 ylab (expression(paste("LE (","W.",m^-2,")", sep="")))

print(comparison) # But only 'latent' line is shown, perhaps because LW.W.M2. contains some NA's
---------------------------------------------------------------------------------------------------------------------------------------------------------------

How can I plot LE.W.M2. as a geom_line with gaps corresponding to NA values?

How can I add a legend identifying each line?

Thanks in advance,
--
Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota
http://www.laas.umn.edu/CurrentStudents/MeettheStudents/ThiagodosSantos/index.htm
Phone: (612) 323 9898

Janesh Devkota

unread,
Feb 18, 2014, 9:14:35 PM2/18/14
to Thiago V. dos Santos, ggp...@googlegroups.com

The problem here is with the naming of your variable. Because you have too many “.” In your variable names. I removed “.” From your variable names and it seems to be working now.

 

# Create a new variable1 by removing dots

df1$variable1 <- as.factor(gsub(pattern="\\.",replacement="",x=df1$variable))

 

# Now plot your data you will see two lines

p <- ggplot(subset(df1,variable1==c("NETWM2","latent")),na.rm=T) + geom_line(aes(date,value,linetype=variable1))

p

 

Best,

Janesh  Devkota

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

thi_v...@yahoo.com.br

unread,
Feb 20, 2014, 8:58:02 AM2/20/14
to Janesh Devkota, ggp...@googlegroups.com

Hi Janesh,

Thanks a lot, your tip did the trick. Is there any way to insert a legend and control the linetype of each line?

I have been trying it with scale_color_manual and it didn't work.

Best,
Thiago.



From: Janesh Devkota <janesh....@gmail.com>;
To: 'Thiago V. dos Santos' <thi_v...@yahoo.com.br>; <ggp...@googlegroups.com>;
Subject: RE: How to plot data containing NA's as "gaps" in geom_line
Sent: Wed, Feb 19, 2014 2:14:35 AM

Janesh Devkota

unread,
Feb 20, 2014, 12:54:09 PM2/20/14
to thi_v...@yahoo.com.br, ggp...@googlegroups.com
Hello Thiago, 

You can use scale_linetype_manual to control the linetype of each line. 

Best,
Janesh Devkota

thi_v...@yahoo.com.br

unread,
Feb 20, 2014, 2:21:35 PM2/20/14
to Janesh Devkota, ggp...@googlegroups.com

Janesh,

Thanks again, I made it.

Best,
Thiago.



From: Janesh Devkota <janesh....@gmail.com>;
To: thi_v...@yahoo.com.br <thi_v...@yahoo.com.br>;
Cc: ggp...@googlegroups.com <ggp...@googlegroups.com>;
Subject: Re: RE: How to plot data containing NA's as "gaps" in geom_line
Sent: Thu, Feb 20, 2014 5:54:09 PM
Reply all
Reply to author
Forward
0 new messages