1

I'm learning to use ggplot to plot my data. I found many examples such as ggplot multiple grouping bar and Grouped bar plot in ggplot. However, I cannot adapt their case with my data at this moment.

This is what the sample looks like:

# A tibble: 10 x 3
   clusterNum Road       period     
        <dbl> <chr>      <chr>      
 1          2 Hualampong 06.00-06.15
 2          2 Hualampong 06.00-06.15
 3          2 Hualampong 06.16-06.30
 4          2 Hualampong 06.16-06.30
 5          2 Hualampong 06.16-06.30
 6          3 Hualampong 06.16-06.30
 7          2 Hualampong 06.16-06.30
 8          3 Tonglor    17.46-18.00
 9          3 Tonglor    17.46-18.00
10          3 Tonglor    17.46-18.00
data <- structure(list(clusterNum = c(2, 2, 2, 2, 2, 3, 2, 3, 3, 3),Road = c("Hualampong", "Hualampong", "Hualampong", "Hualampong","Hualampong", "Hualampong", "Hualampong", "Tonglor", "Tonglor","Tonglor"), period = c("06.00-06.15", "06.00-06.15", "06.16-06.30","06.16-06.30", "06.16-06.30", "06.16-06.30", "06.16-06.30","17.46-18.00", "17.46-18.00", "17.46-18.00")), row.names = c(NA,-10L), class = c("tbl_df", "tbl", "data.frame")) 

As you can see from my data, I want to create bar charts. Showing the total number of clusterNum columns with each period separately with the Road column. So, I might have two graphs based on the Road column.

My expected graph may look like this enter image description here

Thank you for any helps.

Yasumin
  • 443
  • 2
  • 8

2 Answers2

1

Maybe something like this:

library(tidyverse)
data1 <- data %>% 
  group_by(clusterNum, Road, period) %>% 
  count() 

ggplot(data1, aes(x=period, y=n, group=clusterNum)) +
  geom_bar(aes(fill = Road),
           position = "dodge",
           stat = "identity")

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
1

Or if you're looking for separate graphs, you can use facet_wrap:

library(tidyverse)
data2 <- data %>% group_by(period, Road) %>% summarise(clusterNum = sum(clusterNum))
ggplot(data2, aes(x = period, y = clusterNum, fill = period)) + 
  geom_bar(position = "dodge", stat = "identity") + 
  facet_wrap(~Road)

enter image description here

With an additional breakout by clusterNum:

library(tidyverse)
data3 <- data %>% group_by(period, Road, clusterNum) %>%
                  count() %>% 
                  data.frame()
data3$n <- as.factor(data3$n)
data3$clusterNum <- as.factor(data3$clusterNum)

ggplot(data3, aes(x = period, y = n, fill = clusterNum)) + 
  geom_bar(position = "dodge", stat = "identity") + 
  facet_wrap(~Road) +
  theme_minimal()

enter image description here

NovaEthos
  • 500
  • 2
  • 10
  • Thanks @NovaEthos it's interesting if I want to separate each bar from the clusterNum column as well. How to do it? – Yasumin Jul 20 '21 at 15:45
  • @Yasumin not 100% sure what you're asking. Do you want to have separate graphs for the different values in the clusterNum column (2 or 3)? – NovaEthos Jul 20 '21 at 15:51
  • Please see my edited post. So, each road will have a graph like this. Thank you very much – Yasumin Jul 20 '21 at 16:24
  • @Yasumin updated my answer! let me know if this isn't what you're looking for – NovaEthos Jul 20 '21 at 17:50
  • Thank you for your patient haha. It's almost close to what I am trying to do. Maybe it's not sum of the clusterNum but its count? PS.ClusterNum is the group number of my data. I edited my post again. Thanks a lot – Yasumin Jul 20 '21 at 18:12
  • @Yasumin Try to change `stat='identitiy'` to `stat='count'` – user438383 Jul 20 '21 at 18:17
  • 1
    @Yasumin ok, I think I got what you're looking for, but let me know if not! – NovaEthos Jul 20 '21 at 18:21
  • @NovaEthos Thank you sooooo much. This is what I wanted haha. You are the best! – Yasumin Jul 20 '21 at 18:35