Home > Mobile >  Is there a way to skip NA values for a line plot in ggplot2
Is there a way to skip NA values for a line plot in ggplot2

Time:10-09

I am trying to plot 4 line graphs sharing a y axis but one of the graphs has a missing value (NA). I want to be able to connect the two points either side of the NA value for the CI-TR graph. Graphs

Here is the data I am working with (Note CI-TR on the right hand side has the NA value)

table of data

This is my code, I have read in the table from an excel file:

#read the excel file (same as the table attached)
data <- read_csv("test3.csv", col_types = cols(.default = col_guess())

# gather the data for age and depth
plots <-  data %>% filter(core_id == "BKM0817") %>%
  gather(key = param, value = value, -core_id, -Age, -depth)

#this is to relabel the graph titles (NB the added a is to order alphabetically in the order I want them to appear)
plots %>%  
  mutate(facet_label = fct_recode(
    param,
    "delta ^ 13 * C[OM] ~(`\u2030 V-PDB`)" = "ad13com",
    "delta ^ 15 * N ~(`\u2030 AIR`)" = "d15N",
    "'C/N'" = "C/N",
    "CI-TR" = "aaCI-TR"
  )) %>%
          
# now plot the graphs
  ggplot(aes(y = Age, x = value))  
  geom_hline(yintercept = c(10720, 10568, 10620), col = "black", alpha = 0.8, lty = 2)  
  geom_lineh(colour = "black", size = 0.5)  
  geom_point(size = 2)  
  facet_wrap(~facet_label, scales = "free_x", labeller = label_parsed, ncol = 4)  
  scale_y_reverse(# Features of the first axis
    name = "Age (Cal. yrs BP)",

 # Add a second axis and specify its features
    sec.axis = sec_axis( trans=~./17.927, name="Depth (cm)")
  )  
  labs(x = NULL, y = "Age (Cal. yrs BP)")  
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))`

CodePudding user response:

The line geom will but cut if there are any NA values, so the simple solution is to remove the NA values from your data frame. You can use na.omit() to do this, but just pay attention to where you use it in your code. Your original dataset looks something like this:

df <- data.frame(pos=1:4, A=c(1.05, 2.3, 4.24, 3.89),
  B=c(4.44, NA, 2.22, 3.33))
df

>
  pos    A    B
1   1 1.05 4.44
2   2 2.30   NA
3   3 4.24 2.22
4   4 3.89 3.33

Plotting this after gathering you get:

df %>%
  gather(key=type, value=value, -pos) %>%
  ggplot(aes(x=pos, y=value))  
  geom_line(linetype=2, color='blue', size=0.7)  
  geom_point(color='red', size=3)  
  facet_wrap(~type)

enter image description here

If you used na.omit() on df, then it would remove the entire second row, which removes the second observation for column A as well. In this case, just make sure you use na.omit() after the gather() function to pivot the data frame longer:

df %>%
  gather(key=type, value=value, -pos) %>%
  na.omit() %>%    # important this comes after the gather
  ggplot(aes(x=pos, y=value))  
  geom_line(linetype=2, color='blue', size=0.7)  
  geom_point(color='red', size=3)  
  facet_wrap(~type)

enter image description here

In your case, the pseudo code below gives you an idea where to place na.omit() in your own code:

plots %>%
  mutate(...) %>%
  na.omit() %>%
  ggplot(...)   ...
  • Related