3

enter image description here

I have a data set that I want to plot with a line graph. I have two "sets" of dates from 2006-2008 and then from 2015-2019. Ggplot keeps adding a line between these two points from 08-15 because technically there aren't any data gaps, it just spans over the two time periods. I don't want a line drawn between these two periods. How do I do this? I've tried adding a fake date between these two periods to trick R into adding a line break.

enter image description here

library(lubridate) # for working with dates
library(ggplot2)  # for creating graphs
library(scales)   # to access breaks/formatting functions
library(gridExtra) # for arranging plots

# Create Dataset

attach(TP)
df=data.frame(Site,Date,TP)
df %>% na.omit()
df$Date= as.Date(df$Date)
df$Site = factor(df$Site, levels = c("Site 1", "Site 2", "Site 3", "Site 4", "Site 5"))

ggplot(df, aes(x=Date, y=TP, group=Site)) + 
  geom_line(aes(color=Site))  +
  scale_y_continuous( limits = c(0, 0.5))+
  theme_classic() +
  labs(x="", y=(expression(TP~ (mg-P~L^{-1})))) +
  scale_x_date(limits = as.Date(c("2006-01-01","2019-12-30")),
               labels = date_format("%Y"))
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Kelsey
  • 41
  • 2
  • 1
    I'm afraid it is not really easy for us to help you as we cannot copy-paste the data readily in our R sessions. Would you be willing to post the output of `dput(TP)` instead (or just a relevant part of the `TP` data)? – teunbrand Jul 16 '20 at 19:56
  • Does this answer your question? [How to remove gaps on a date axis in ggplot2](https://stackoverflow.com/questions/61331557/how-to-remove-gaps-on-a-date-axis-in-ggplot2) – stefan Jul 16 '20 at 20:15
  • 1
    You could make a factor for each time period and then facet_wrap by this factor – Richard Telford Jul 16 '20 at 22:23

0 Answers0