1

Updated to explain how this is not a duplicate question.

I would like to remove the lines that connect the "data gaps" between the groups of lines in picture 1: Facet plot to fix, below. I am trying to recreate picture 2. (The lines in picture 2 connecting raw and corrected datasets are nice but not needed).

The facet plot is showing the values of two parameters recorded from the same data logger. Each parameter has one raw value dataset and one corrected dataset, where erroneous values were deleted (so 4 total datasets). I combined them all with gather() in order to make the facet plot and added a column type (raw or corrected).

I have read that keeping NA's prevents the line graph from plotting a point. To do that, it seems I would need to have 4 parameter types (parameter1 raw, parameter 1 corrected, parameter 2 raw, parameter 2 corrected). Once I do that though, I do not know how to make a facet plot where parameter 1 raw and parameter 1 corrected are plotted together on the top graph and parameter 2 raw and parameter 2 corrected are on the bottom graph of the facet.

Link1: Connecting across missing values with geom_line

Plotting Groups: https://www3.nd.edu/~steve/computing_with_data/13_Facets/facets.html

Link 2: geom_line - different colour in the same line I understand how to apply a different color to the same line (at least one way -- see example code below). This answer does not explain how to do this in a facet plot. I have experimented with the aes() placement and groups, but have not found a solution that allows for two facet plots each with two line groupings that does not connect the line between the groups. Would I need a separate geom_line() call for each facet plot (is that possible)?

Facet plot to fix: lines have grouped values and gaps between groups are connected. Facet plot to fix: lines have grouped values and gaps between groups are connected.

Example of what I want my plot to look like Example of what I want my plot to look like

Here is an example dataset:

df<- data.frame(DateTime=c("08/29/2011 00:00", "08/29/2011 01:00", "08/29/2011 02:00", "08/29/2011 03:00", "08/29/2011 04:00", "08/29/2011 05:00", 
                        "08/29/2011 06:00", "08/29/2011 07:00", "08/29/2011 08:00", "08/29/2011 09:00", "08/29/2011 10:00", "08/29/2011 11:00",
                        "08/29/2011 12:00", "08/29/2011 13:00", "08/29/2011 14:00", "08/29/2011 15:00", "08/29/2011 16:00", "08/29/2011 17:00",
                        "08/29/2011 18:00", "08/29/2011 19:00", "08/29/2011 20:00", "08/29/2011 21:00", "08/29/2011 22:00", "08/29/2011 23:00", 
                        "08/30/2011 00:00", "08/29/2011 00:00", "08/29/2011 01:00", "08/29/2011 02:00", "08/29/2011 03:00", "08/29/2011 04:00", "08/29/2011 05:00", 
                        "08/29/2011 06:00", "08/29/2011 07:00", "08/29/2011 08:00", "08/29/2011 09:00", "08/29/2011 10:00", "08/29/2011 11:00",
                        "08/29/2011 12:00", "08/29/2011 13:00", "08/29/2011 14:00", "08/29/2011 15:00", "08/29/2011 16:00", "08/29/2011 17:00",
                        "08/29/2011 18:00", "08/29/2011 19:00", "08/29/2011 20:00", "08/29/2011 21:00", "08/29/2011 22:00", "08/29/2011 23:00", 
                        "08/30/2011 00:00"),
             Type=c("Corrected", "Corrected", "Raw", "Raw", "Corrected", "Corrected", "Corrected", "Corrected",
                    "Raw", "Raw","Raw","Raw","Raw","Raw", "Corrected", "Corrected",
                    "Corrected", "Corrected","Raw","Raw","Corrected","Corrected","Corrected", "Corrected",
                    "Raw", "Corrected", "Corrected", "Raw", "Raw", "Corrected", "Corrected", "Corrected", "Corrected",
                    "Raw", "Raw","Raw","Raw","Raw","Raw", "Corrected", "Corrected",
                    "Corrected", "Corrected","Raw","Raw","Corrected","Corrected","Corrected", "Corrected",
                    "Raw"))
df$DateTime<-strptime(df$DateTime,"%m/%d/%Y %H:%M")
df$DateTime<-as.POSIXct(df$DateTime, tz="EST") 
df$Parameter[1:25]<-"Par1"
df$Parameter[26:50]<-"Par2"
df$Value<-sample(c(11:60))
df$Value<-ifelse(df$Type=="Raw", 1, df$Value)

Here is my graph:

df %>% ggplot(aes(DateTime, Value, color=Type))+
 geom_point()+
 geom_line()+
 theme_bw()+
 facet_grid(Parameter ~., scales="free")+ 
 scale_color_manual(values=c("#CC79A7","#000000")) + 
 labs(x="Date-Time")+
 theme(text=element_text(family="serif"), 
    strip.text.y=element_text(face="bold"), strip.background = element_rect(fill=NA, colour="black"),
    axis.text=element_text(color="#000000"), axis.title=element_text(face="bold"))
  • Thanks for the link, edited to explain how it is not a duplicate question. – Ecologist711 Feb 28 '18 at 18:32
  • Try this: df %>% ggplot(aes(DateTime, Value, color=(Type == "Corrected")))+ geom_point()+ geom_line(aes(group = Parameter))+ theme_bw()+ facet_grid(Parameter ~., scales="free")+ scale_color_manual(values=c("#CC79A7","#000000")) + labs(x="Date-Time")+ theme(text=element_text(family="serif"), strip.text.y=element_text(face="bold"), strip.background = element_rect(fill=NA, colour="black"), axis.text=element_text(color="#000000"), axis.title=element_text(face="bold")) – Hallie Swan Feb 28 '18 at 23:52
  • Thank you!! That's exactly what I was looking for. Just had to manually fix the legend after that. Can you explain the color = (Type=="Corrected")). How was color=Type causing the data gaps to be connected? – Ecologist711 Mar 01 '18 at 18:08

1 Answers1

1

My comment was actually more complicated than it needed to be. All you have to do is add a group aesthetic to geom_line. By not adding Type == Corrected, you won't have to manually change your legend.

As this answer quotes, here's Hadley on why:

The important thing [for a line graph with a factor on the horizontal axis] is to manually specify the grouping. By default ggplot2 uses the combination of all categorical variables in the plot to group geoms - that doesn't work for this plot because you get an individual line for each point. Manually specify group = 1 indicates you want a single line connecting all the points.

Here's your example:

df %>% ggplot(aes(DateTime, Value, color=Type))+
        geom_point()+
        geom_line(aes(group = Parameter))+
        theme_bw()+
        facet_grid(Parameter ~., scales="free")+ 
        scale_color_manual(values=c("#CC79A7","#000000")) + 
        labs(x="Date-Time")+
        theme(text=element_text(family="serif"), 
              strip.text.y=element_text(face="bold"), strip.background = element_rect(fill=NA, colour="black"),
              axis.text=element_text(color="#000000"), axis.title=element_text(face="bold"))

enter image description here

Hallie Swan
  • 2,714
  • 1
  • 15
  • 23