Search code examples
rggplot2geom

How to get r to not remove a row in ggplot - geom_line


I'm trying to produce a graph of growth rates over time based upon the following data which has blanks in two groups.

When I try to make a growth plot of this using geom_line to join points there is no line for group c.

I'm just wondering if there is anyway to fix this

enter image description here


Solution

  • One option would be to get rid of the missing values which prevent the points to be connected by the line:

    Making use of the code from the answer I provided on your previous question but adding tidyr::drop_na:

    Growthplot <- data.frame(
      Site = letters[1:4],
      July = 0,
      August = c(1, -1, NA, 2),
      September = c(3, 2, 3, NA)
    )
    
    library(ggplot2)
    library(tidyr)
    library(dplyr, warn=FALSE)
    
    growth_df <- Growthplot %>%
      pivot_longer(-Site, names_to = "Month", values_to = "Length") %>%
      mutate(Month = factor(Month, levels = c("July", "August", "September"))) %>%
      drop_na()
    
    ggplot(growth_df, aes(x = Month, y = Length, colour = Site, group = Site)) +
      geom_point() +
      geom_line()+
      labs(color = "Site", x = "Month", y = "Growth in cm")  +  
      theme(axis.line = element_line(colour = "black", size = 0.24))