Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting grouped bar charts in R

Tags:

plot

r

bar-chart

i am trying to plot this data in R -

column1  column2  column3
1-2       abc       10
1-2       def       15
1-2       ghi       20
2-3       abc       80
2-3       def       95
2-3       ghi       10
3-4       abc       30
3-4       def       55
3-4       ghi       80

the x axis would be column1 (so 1-2, 2-3 and 3-4 would be present as the x axis), and on y axis, the values in column3 should be plotted for each of the column2 elements. So this would be a "grouped" barchart essentially.

I am not able to plot this grouped bar chart using R. The code snippet I am using is below:

dataset <- fetch(rs,n=-1)
plot_var <- table(dataset$percentage, dataset$age)
barplot(plot_var, names.arg,
        main="Title of Graph",
        xlab="Column1", col=c("darkblue","red"),
        legend = rownames(plot_var), beside=TRUE)

How do I get this grouped bar chart to display? Thanks!

like image 325
kallakafar Avatar asked Nov 03 '12 14:11

kallakafar


People also ask

How do I make a grouped bar chart in R?

Then the user needs to call the geom_bar() function from the ggplot package with the required parameters into it to create the grouped bar plot in the R programming language. geom_bar() function: This function makes the height of the bar proportional to the number of cases in each group.

How do you plot multiple bar graphs in R?

Method 1: Using geom_bar() from ggplot2 package The geom_bar() function is used to create bar charts for categorical data x, and histograms for continuous data y. It is inbuilt in the ggplot2 package, we don't need to install it separately.

How do I plot a bar chart in R?

Bar plots can be created in R using the barplot() function. We can supply a vector or matrix to this function. If we supply a vector, the plot will have bars with their heights equal to the elements in the vector.


2 Answers

Your problem seem to come down to wrong data formatting. You need to make a matrix with the right row names structure to create plot that you want with base graphics. Here is your solution:

#your data...
d <- data.frame(row.names=c("1-2","2-3","3-4"), abc = c(10,80, 30), 
                def = c(15, 95, 55), ghi = c(20, 10, 80))
#but you make a matrix out of it to create bar chart
d <- do.call(rbind, d)
#...and you are sorted
barplot(d, beside = TRUE, ylim=c(0,100), legend.text = rownames(d), 
        args.legend = list(x = "topleft", bty="n"))

enter image description here

However, I sometimes like to use lattice for this kind of task. This time you don't even have to make matrix, you just keep your data.frame in original format:

d <- data.frame(column1=rep(c("1-2","2-3","3-4"), each=3), 
                column2=rep(c("abc", "def", "ghi"), 3), 
                column3=c(10, 15, 20, 80, 95, 10, 30, 55, 80))
require(lattice)
barchart(column3 ~ column1, groups=column2, d, auto.key = list(columns = 3))

enter image description here

like image 152
Geek On Acid Avatar answered Oct 18 '22 21:10

Geek On Acid


I like to use ggplot2 for this kind of task.

#Make the data reproducible:
column1 <- c(rep("1-2", 3), rep("2-3", 3), rep("3-4", 3))
column2 <- gl(3, 1, 9, labels=c("abc", "def", "ghi"))
column3 <- c(10, 15, 20, 80, 95, 10, 30, 55, 80)

d <- data.frame(column1=column1, column2=column2, column3=column3)

require(ggplot2)
ggplot(d, aes(x=column1, y=column3, fill=column2)) + geom_bar(position=position_dodge())

The reason I find this intuitive (after a bit of a learning period) is that you clearly stated what you want on the x and y axes, and we simply tell ggplot that (as well as which variable defines the 'fill' color, and which kind of plot - here, geom_bar - to use.

enter image description here

like image 22
Drew Steen Avatar answered Oct 18 '22 22:10

Drew Steen