Jesinsky
Jesinsky

Reputation: 81

How to eliminate unwanted sections of geom_line in ggplot2

I have a geom_line plot with data from 6 separate data collection periods (deployments) from 2 different sampling sites. The problem I have is that geom_line draws a line between the deployments. Is there a simple way to stop a line between deployments?enter image description here

Here's my code:

        pT=ggplot(Data1day,
          aes(x=Date2,
              y=Temp1_C,
              color=Observatory,
              shape=Observatory))+
  geom_line()+
  scale_shape_manual(values=c(4,3))+
  scale_colour_manual(values=c("dodgerblue4","orange")) + 
    ylab(expression(atop("Temp.","(°C)"))) + xlab("")  + 
    xlim(as.Date(c('1/2/2009', '1/9/2014'), format="%d/%m/%Y") )+ 
    theme(legend.position="none")+
    theme_bw() + theme(panel.border = element_blank(), panel.grid.major = 
    element_blank(),panel.grid.minor = element_blank(), axis.line = 
   element_line(colour = "black"))

And a very small bit of sample code:

Observatory Date    Deployment  Temp1_C
NF  19/02/2010  3   4.077021277
NF  20/02/2010  3   4.095833333
NF  21/02/2010  3   4.031666667
NF  22/02/2010  3   4.026666667
NF  23/02/2010  3   4.017291667
NF  24/02/2010  3   4.0575
FF  18/02/2010  3   4
FF  19/02/2010  3   3.995
FF  20/02/2010  3   4.008541667
FF  21/02/2010  3   4.035833333
FF  22/02/2010  3   4.010833333
NF  18/08/2010  4   3.99047619
NF  19/08/2010  4   3.977916667
NF  20/08/2010  4   3.994166667
NF  21/08/2010  4   3.986666667
NF  22/08/2010  4   3.967916667
FF  30/08/2010  4   3.9255
FF  31/08/2010  4   3.935
FF  01/09/2010  4   3.93
FF  02/09/2010  4   3.939166667
FF  03/09/2010  4   3.910416667
FF  04/09/2010  4   3.874166667
FF  05/09/2010  4   3.8725

Upvotes: 0

Views: 501

Answers (1)

Robin Gertenbach
Robin Gertenbach

Reputation: 10816

In your case you can really just add group=paste(Observatory, Deployment). This will make each deployment for each observatory its own line segment but keep color consistent across Deployments.

ggplot(
  Data1day,
  aes(
    Date2, Temp1_C, 
    color = Observatory, 
    group = paste(Observatory, Deployment)))

Upvotes: 2

Related Questions