Delete vertical white lines in a ggplot line chart

I created a graph using ggplot and some data that represent the emergence of specific management actions over the past decade. The graph looks great except for some vertical white spaces that appear in the drawn lines. Is there any way to remove these lines? I assume they are caused by the grouping of my data (group = 1). Any help would be greatly appreciated. My code is below.

In addition, I used this stackoverflow question as a guide.

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 
+6
source share
1 answer

Interesting story! I think that if I understand your problem, you will either need

  • add expand = c(0,0) to scale_x_date to get rid of the space at the borders
  • and / or use geom_linerange to create adjacent color blocks

But you need to specify ymax and ymax to use geom_linerange . filler_value seems like a natural choice for ymin , so let's make a7.data$filler_value2 <- 1 our ymax and use geom_linerange and geom_linerange 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 these changes, I get a plot that looks like this ... enter image description here

+5
source

Source: https://habr.com/ru/post/944074/


All Articles