0

I have a data set with observations by day and hour for a series of organisms.
I want to plot each set of observations separately. However, when if there are no observations at the beginning of the time series it cuts that time off the axis (i.e., no animals seen on day 1 so axis starts on day 2) - as a result my plots have different axis's. Neither scale_y_discrete or continuous work for different reasons. This seems like it should be easy.

df <- data.frame("day"= c(1, 1, 1, 2, 2, 2, 3, 3, 3),"hour" = c(1, 2, 3, 1, 2, 2, 1, 2, 3),"a" = c("Y", "Y", "N", "N", "N", "Y", "N", "N", "N"), "b" = c("N", "N", "Y", "N", "N", "Y", "Y", "N", "Y")) #example data

df %>%
pivot_longer(cols = a) %>%
filter(value == 'Y') %>% #I only want to display Y or positive observations
mutate(across(c(day, hour), factor)) %>%
ggplot() + aes(day, hour, color = name) + geom_point()
Waldi
  • 39,242
  • 6
  • 30
  • 78
brc
  • 99
  • 8
  • you could try using `expand_limits()`? – dario May 01 '21 at 14:53
  • 1
    Does this answer your question? [ggplot2 keep unused levels barplot](https://stackoverflow.com/questions/10834382/ggplot2-keep-unused-levels-barplot) – tjebo May 01 '21 at 15:41

1 Answers1

0

day is a factor.
You could set the levels you want to display and use drop = FALSE in scale_x_discrete:

df %>%
  pivot_longer(cols = a) %>%
  filter(value == 'Y') %>% #I only want to display Y or positive observations
  mutate(day=factor(day,levels=1:4),
         hour=factor(hour)) %>%
  ggplot() + aes(day, hour, color = name) + geom_point() +
             scale_x_discrete(drop = FALSE)

enter image description here

Waldi
  • 39,242
  • 6
  • 30
  • 78