Home > Software engineering >  Grouped bar chart in R for multiple filter and select
Grouped bar chart in R for multiple filter and select

Time:12-27

Following is my dataset:

Result course1 course2 course3
pass 15 17 18
pass 12 14 19
Fail 9 13 3
Fail 3 2 0
pass 14 11 20
Fail 5 0 7

I want to plot a grouped bar graph. I am able to plot following graphs but I want both the results in same graph.

par(mfrow=c(1,1))
options(scipen=999)
coul <- brewer.pal(3, "Set2") 

# Bar graph for passed courses 
result_pass <-data %>% filter(Result=='Pass') %>%  summarize(c1_tot=sum(course1), 
        c2_tot = sum(course2), c3_tot = sum(course3) )
col_sum <- colSums(result_pass[,1:3])
barplot(colSums(result_pass[,1:3]), xlab = "Courses", ylab = "Total Marks", col = coul, ylim=range(pretty(c(0, col_sum))), main = "Passed courses ")

# Bar graph for Failed courses 
result_fail <-data %>% filter(Result=='Fail') %>%  summarize(c1_tot=sum(course1), 
        c2_tot = sum(course2), c3_tot = sum(course3) )
col_sum <- colSums(result_fail[,1:3])
barplot(colSums(result_fail[,1:3]), xlab = "Courses", ylab = "Total Marks", col = coul, ylim=range(pretty(c(0, col_sum))), main = "Failed courses ")

Any suggestion for which I can merge both the above plots and create grouped bar graph for Pass and Fail courses.

CodePudding user response:

It's probably easier than you think. Just put the data directly in aggregate and use as formula . ~ Result, where . means all other columns. Removing first column [-1] and coerce as.matrix (because barplot eats matrices) yields exactly the format we need for barplot.

This is the basic code:

barplot(as.matrix(aggregate(. ~ Result, data, sum)[-1]), beside=TRUE)

And here with some visual enhancements:

barplot(as.matrix(aggregate(. ~ Result, data, sum)[-1]), beside=TRUE, ylim=c(0, 70),
        col=hcl.colors(2, palette='viridis'), legend.text=sort(unique(data$Result)),
        names.arg=names(data)[-1], main='Here could be your title',
        args.legend=list(x='topleft', cex=.9))
box()

enter image description here


Data:

data <- structure(list(Result = c("pass", "pass", "Fail", "Fail", "pass", 
"Fail"), course1 = c(15L, 12L, 9L, 3L, 14L, 5L), course2 = c(17L, 
14L, 13L, 2L, 11L, 0L), course3 = c(18L, 19L, 3L, 0L, 20L, 7L
)), class = "data.frame", row.names = c(NA, -6L))
  • Related