H2O_Research
H2O_Research

Reputation: 175

Removing Vertical White Lines in ggplot Line Graph

I've produced a graph using ggplot and some data that represents the occurrence of specific management actions over the past decade. The graph looks great except for some vertical white spaces that appear within the plotted lines. Is there a way to remove these lines? I'm guessing they are caused by the grouping of my data (group=1). Any help would be appreciated. My code is below.

Also, I've used this stackoverflow question as guidance.

library(scales)

# Build data frame.
a7.data <- data.frame(date = seq(as.Date("2002/01/01"), as.Date("2013/05/01"),     by="day"))
a7.data$year <- as.numeric(format(as.Date(a7.data$date), format="%Y"))
a7.data$month <- as.numeric(format(as.Date(a7.data$date), format="%m"))
a7.data$day <- as.numeric(format(as.Date(a7.data$date), format="%d"))
a7.data$status <- "Yes"
a7.data$filler_value <- 0

# Edit data frame for dates in which the management action was "no".
a7.data$status[a7.data$date >= "2002/01/01" & a7.data$date < "2002/07/01"] <- "No"
a7.data$status[a7.data$date >= "2005/05/20" & a7.data$date < "2005/08/25"] <- "No"
a7.data$status[a7.data$date >= "2005/12/31" & a7.data$date < "2006/04/12"] <- "No"
a7.data$status[a7.data$date >= "2006/11/06" & a7.data$date < "2006/12/31"] <- "No"
a7.data$status[a7.data$date >= "2007/01/31" & a7.data$date < "2007/07/02"] <- "No"
a7.data$status[a7.data$date >= "2008/02/01" & a7.data$date < "2009/08/11"] <- "No"
a7.data$status[a7.data$date >= "2010/02/28" & a7.data$date < "2010/03/15"] <- "No"
a7.data$status[a7.data$date >= "2010/05/09" & a7.data$date < "2010/07/07"] <- "No"

# Create a new column that creates a dummy year with which to plot the data in ggplot using faceting.
a7.data <- transform(a7.data, doy = as.Date(paste(1970, month, day, sep="/")))

# Custom colors.
ccolors <- c("#086CA2", "#FF8B00")

# ggplot code.
bb <- ggplot(a7.data, aes(doy, filler_value)) + 
geom_line(aes(color=status, group=1), size=15, alpha=0.9) + 
scale_x_date(label=date_format("%b"), breaks = "month") + 
xlab("") + ylab("") + facet_grid(year~., scales="free") + 
theme_bw() + theme(axis.text.y=element_blank()) + 
theme(axis.ticks.y=element_blank()) + 
scale_color_manual(values=ccolors, name="Article VII Restrictions?")

# Display plot.
bb 

Upvotes: 6

Views: 2057

Answers (1)

Simon O&#39;Hanlon
Simon O&#39;Hanlon

Reputation: 59970

Interesting plot! I think if I understand your problem you either need

  • to add an expand = c(0,0) to scale_x_date to get rid of the space at the boundaries
  • and/or use geom_linerange to make contiguous colour blocks

But you need to specify both a ymin and a ymax value to use geom_linerange. filler_value seems to be a natural choice for ymin so lets make a7.data$filler_value2 <- 1 to be our ymax and use a geom_linerange and include an expand argument:

a7.data$filler_value2 <- 1
bb <- ggplot(a7.data, aes(x = doy)) + 
geom_linerange( aes(ymin = filler_value , ymax = filler_value2 , color=status, group=1), size=15, alpha=0.9) + 
scale_x_date(label=date_format("%b"), breaks = "month" , expand = c(0,0)) + 
xlab("") + ylab("") + facet_grid(year~., scales="free") + 
theme_bw() + theme(axis.text.y=element_blank()) + 
theme(axis.ticks.y=element_blank()) + 
scale_color_manual(values=ccolors, name="Article VII Restrictions?")

# Display plot.
bb 

If I make those changes I get a plot that looks like this... enter image description here

Upvotes: 7

Related Questions