Reputation: 1251
I have this kind of data:
dat
date shop_id
1 2013-01 1
2 2013-02 2
3 2013-02 2
4 2013-02 2
5 2013-02 1
6 2013-03 3
7 2013-04 1
shop_id
stands for a specific shop and year_month
stands for the date. If a shop is listed at a specific date it means that it´s open, if not it´s closed (i.e. in Janury 2013/2013-01 shop 1 was open but not shop 2 and 3, in March 2013/2013-03 shop 3 was open but not shop 1 and 2). Since the data is about sales of a specific product, a shop can occur more than once per date. I want to plot the data.
It should look like the plot below: On the y-axis should be the date, on the x-axis should be the shop_id and fill should be if the shop is open (shop_id occurs together with a specific date) or not.
dput(dat)
structure(list(date = structure(c(1L, 2L, 2L, 2L, 2L, 3L, 4L), .Label = c("2013-01",
"2013-02", "2013-03", "2013-04"), class = "factor"), shop_id = c(1,
2, 2, 2, 1, 3, 1)), class = "data.frame", row.names = c(NA, -7L
))
Upvotes: 0
Views: 126
Reputation: 2399
Is this what you are looking for?
library(tidyverse)
library(lubridate)
df %>%
group_by(shop_id) %>%
mutate(
date = ymd(paste0(date, "-01")),
start = min(date),
end = max(date) %>% ceiling_date(unit = "month") # as Julian_Hn suggested
) %>%
ungroup() %>%
ggplot(aes(x = factor(shop_id))) +
geom_linerange(aes(
ymin = start,
ymax = end
),
size = 40 # bar width
)
library(tidyverse)
df %>%
group_by(date) %>%
nest() %>%
arrange(date) %>%
mutate(ypos = row_number()) %>%
unnest() %>%
ggplot() +
geom_rect(aes(
xmin = shop_id - .25,
xmax = shop_id + .25,
ymin = ypos - .5,
ymax = ypos + .5
))
Upvotes: 1