Trying to create a graph where both x and y are factors but I don't want the lines to be connected if there is a gap. How can I achieve this?
library(ggplot2)
df <- data.frame(x = c('a', 'b', 'c', 'd', 'e'), y = c('a', 'a', NA, 'a', 'a'))
ggplot(df, aes(x = x, y = y, group = y)) +
geom_point() +
geom_line()
Dont want the NA in the plot and there shouldn't be a line between b and d.
This may need extra work with your full dataset but one approach is to create a grouping variable to use in ggplot to prevent connections that aren't wanted.
df <- data.frame(x = c('a', 'b', 'c', 'd', 'e'), y = c('a', 'a', NA, 'a', 'a'), stringsAsFactors = FALSE)
df %>%
mutate(grp = with(rle(y), rep(seq_along(lengths), lengths))) %>% # y can't be a factor
mutate_all(as.factor) %>%
na.omit() %>% # Drop NA cases so they're not plotted
ggplot(aes(x = x, y = y, group = grp)) +
geom_point() +
geom_line() +
scale_x_discrete(drop = FALSE) # Preserve empty factor levels in the plot

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With