0

Given the data like below that shows records of an event per weekdays,

    Type    Day of week
1   Week1   Tuesday
2   Week1   Tuesday
3   Week1   Wednesday
4   Week1   Friday
5   Week2   Thursday
6   Week2   Tuesday
7   Week2   Friday
8   Week2   Tuesday
9   Week2   Monday
10  Both    Thursday
11  Both    Monday
12  Both    Friday
13  Both    Thursday
14  Both    Monday
15  Both    Sunday

How can I have a bar plot with repeated x axis showing two weeks and plot frequencies based on Type column (both to be appeared in both weeks).

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Sean
  • 103
  • 9

2 Answers2

2

Probably, you can try

library(dplyr)
library(ggplot2)

df1 <- df %>%  mutate_all(as.character) %>%  mutate(Type1 = Type)

df1 %>%
   filter(Type == "Both") %>%
   tidyr::uncount(2) %>%
   mutate(Type = rep(c("Week1", "Week2"), length.out = n())) %>%
   bind_rows(df1 %>% filter(Type != "Both")) %>%
   mutate(Day_of_week = factor(Day_of_week, levels = c("Monday", "Tuesday", 
     "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))) %>%
   ggplot() + aes(x = Day_of_week, fill = Type1) + geom_bar() +
   facet_wrap(.~Type) +
   scale_x_discrete(drop=FALSE)

enter image description here

data

df <- structure(list(Type = structure(c(2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Both", "Week1", 
"Week2"), class = "factor"), Day_of_week = structure(c(5L, 5L, 
6L, 1L, 4L, 5L, 1L, 5L, 2L, 4L, 2L, 1L, 4L, 2L, 3L), .Label = c("Friday", 
"Monday", "Sunday", "Thursday", "Tuesday", "Wednesday"), class = 
"factor")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15"))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks Ronak. What if I want to have it as Stacked bar chart with percentages? – Sean Dec 10 '19 at 05:39
  • @Sean Do you mean something like this ? https://stackoverflow.com/a/59207281/3962914 – Ronak Shah Dec 10 '19 at 05:46
  • No. Like the post below that post. So week one includes week1 ( e.g in green) and both (in red) with related percentages. And same for the week 2. – Sean Dec 10 '19 at 05:56
  • @Sean this sounds like a new question. Please post a new question including all the relevant details. Making changes in this question invalidates already existing answers and confuses future readers. – Ronak Shah Dec 10 '19 at 06:01
  • Sure. Just the bar chart but i want to see different types with different colours. :) – Sean Dec 10 '19 at 06:03
  • Sure, updated the answer to have different colors for different `Type`. – Ronak Shah Dec 10 '19 at 06:08
  • Thanks but what about type both? Type both needs to appear in both weeks and the third colour. Thanks in advance – Sean Dec 10 '19 at 06:44
  • @Sean Updated the answer. Hope it solves the issue. – Ronak Shah Dec 10 '19 at 06:59
  • Yes. You are the legend. – Sean Dec 10 '19 at 07:02
  • How Can I plot the same chart if the "Type column" has several weeks, lets say Week1, ..., Week 20 without both. Can you please advise? – Sean Dec 18 '19 at 03:12
  • I am not sure and I think you have already asked a new question regarding this ? – Ronak Shah Dec 18 '19 at 03:32
  • No that's totally different topic. Just in the above data, instead of "Week 1" and " Week 2" I have 20 week. I have manually inserted new weeks to your code in "%>% mutate(Type = rep(c("Week 1", "Week 2", ), length.out = n())) %>% ..." but if failed. – Sean Dec 18 '19 at 03:36
  • Found the answer from the below post. Thanks anyway – Sean Dec 18 '19 at 03:56
2
library(dplyr)
library(ggplot2)

# make the "both" happen every week
both = dd %>% filter(Type == "Both") %>%
  select(-Type) %>%
  merge(data.frame(Type = c("Week1", "Week2")), by = NULL) 

# put the data together
all = dd %>% filter(Type != "Both") %>%
  bind_rows(both)

# put days in right order
all$Dayofweek = factor(all$Dayofweek, 
  levels = c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))

# plot
ggplot(all, aes(x = Dayofweek)) +
  geom_bar() +
  facet_wrap(~Type)

enter image description here

Using this data:

dd = read.table(text = "    Type    Dayofweek
1   Week1   Tuesday
2   Week1   Tuesday
3   Week1   Wednesday
4   Week1   Friday
5   Week2   Thursday
6   Week2   Tuesday
7   Week2   Friday
8   Week2   Tuesday
9   Week2   Monday
10  Both    Thursday
11  Both    Monday
12  Both    Friday
13  Both    Thursday
14  Both    Monday
15  Both    Sunday", header = T)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294