我使用 ggplot 和一些数据制作了一个图表,这些数据代表了过去十年中特定管理行动的发生情况。该图看起来很棒,除了一些出现在绘制线内的垂直空白。有没有办法删除这些行?我猜它们是由我的数据分组(组= 1)引起的。任何帮助,将不胜感激。我的代码如下。
另外,我使用这个stackoverflow 问题作为指导。
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