How to ignore #VALUE! and make line segments in ggplot connect to each other?

Viewed 32

I try to use goem_line to connect the dots with segments. Here is my original data:

df<-structure(list(Bloc = c(6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6), Pos_heliaphen = c("X46", 
"X47", "X46", "X47", "X46", "X47", "X46", "X47", "X46", "X47", 
"X46", "X47", "X46", "X47", "X46", "X47", "X46", "X47", "X46", 
"X47", "X46", "X47", "X46", "X47", "X46", "X47", "X46", "X47", 
"X46", "X47"), traitement = c("WS", "WW", "WS", "WW", "WS", "WW", 
"WS", "WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", 
"WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", "WW", 
"WS", "WW"), Variete = c("Blancas", "Blancas", "Blancas", "Blancas", 
"Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
"Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
"Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
"Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
"Blancas", "Blancas"), Date_obs = c("26/05/2021", "26/05/2021", 
"27/05/2021", "27/05/2021", "28/05/2021", "28/05/2021", "31/05/2021", 
"31/05/2021", "01/06/2021", "01/06/2021", "04/06/2021", "04/06/2021", 
"07/06/2021", "07/06/2021", "08/06/2021", "08/06/2021", "09/06/2021", 
"09/06/2021", "10/06/2021", "10/06/2021", "11/06/2021", "11/06/2021", 
"14/06/2021", "14/06/2021", "15/06/2021", "15/06/2021", "16/06/2021", 
"16/06/2021", "18/06/2021", "18/06/2021"), FTSW_av_arros = c(0.916282724383014, 
0.92943692088382, NA, NA, 0.867138112075431, 0.890235210263721, 
NA, NA, NA, NA, NA, NA, 0.848566020215008, 0.828225231646472, 
NA, NA, 0.797135611986142, 0.76407697790449, NA, NA, 0.654273366905961, 
0.714183891660727, 0.427122397228472, 0.59871703492516, NA, NA, 
0.231401121468624, 0.632929436920884, 0.138540662166506, 0.744119743406985
), FTSW_apres_arros = c(0.916282724383014, 0.92943692088382, 
NA, NA, 0.899853566198793, 0.899215965787598, NA, NA, NA, NA, 
NA, NA, 0.899996428443873, 0.899501069137562, NA, NA, 0.797135611986142, 
0.902352102637206, NA, NA, 0.654273366905961, 0.899501069137562, 
0.427122397228472, 0.909479686386315, NA, NA, 0.231401121468624, 
0.899501069137562, 0.138540662166506, 0.903777619387028)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -30L))

And here is the code I used:

library(ggplot2)
library(dplyr)
library(readxl)
library(scales)
library(tidyverse)
library(lubridate)

Blancas<-subset(df, Variete %in% c("Blancas"))
labels <- Blancas %>% 
  select(Bloc, Pos_heliaphen) %>% 
  distinct(Bloc, Pos_heliaphen) %>% 
  group_by(Bloc) %>% 
  summarise(Pos_heliaphen = paste(Pos_heliaphen, collapse = "-")) %>% 
  tibble::deframe()

p1<-
  Blancas %>%
  mutate(Date_obs = as.POSIXct(lubridate::dmy(Date_obs))) %>%
  tidyr::pivot_longer(starts_with("FTSW")) %>%
  mutate(Date_obs = if_else(name == "FTSW_apres_arros", 
                            Date_obs + 43200, Date_obs)) %>%
  ggplot(aes(Date_obs, value, colour = factor(Bloc), shape = traitement, 
             linetype = traitement, group = interaction(Bloc, traitement))) +
  geom_point() +
  geom_line() +
  scale_color_discrete(labels = labels, guide = guide_legend(order = 1)) +
  scale_x_datetime(date_labels = "%d/%m/%Y", date_breaks = "day") +
  labs(y = expression(paste("FTSW"))) +
  theme(legend.position = "right", 
        axis.text.x = element_text(angle = 90, hjust = 1))+
  labs(title = "Blancas",
       y = expression(paste("FTSW"))) +
  theme(legend.position = "right", axis.text.x = element_text(angle = 90, hjust = 1), axis.title.x=element_blank())+
  guides(colour = guide_legend(order = 1,nrow = 8),shape=guide_legend(nrow = 2),linetype=guide_legend(nrow = 2))
p1

Here is the graph I got: enter image description here Because there are a lot of #VALUE! in my original data, some line segments are not connected over time. The picture below is what I expect: enter image description here

Is it possible to achieve this if I don't delete the #VALUE!?

0 Answers
Related