Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a line plot using categorical data and not connecting the lines

Tags:

r

na

ggplot2

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.

like image 722
MLEN Avatar asked Oct 28 '25 07:10

MLEN


1 Answers

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

enter image description here

like image 79
Ritchie Sacramento Avatar answered Oct 29 '25 21:10

Ritchie Sacramento



Donate For Us

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