Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grouped bar graph

Tags:

r

graphing

I have the following data:

bin groupname   total_dist
0   rowA    377
0   rowA    306.6
0   rowB    2.1
0   rowB    110.6
1   rowA    918.1
1   rowA    463.2
1   rowB    798.2
1   rowB    1196
2   rowA    1295.1
2   rowA    1269.1
2   rowB    698
2   rowB    1022.1

Using R, I want to make a bar graph where there is a bar for rowA and a bar for rowB for each bin. I can group total_dist by one or the other (plot(total_dist~bin) or plot(total_dist~groupname)). But I can't figure out how to combine them.

I want something that looks similar to this example: example grouped bar graph

like image 848
dnagirl Avatar asked Jun 09 '10 15:06

dnagirl


People also ask

Is bar graph used for grouped data?

Bar graphs are the pictorial representation of data (generally grouped), in the form of vertical or horizontal rectangular bars, where the length of bars are proportional to the measure of data. They are also known as bar charts. Bar graphs are one of the means of data handling in statistics.

When and why would you use a grouped bar chart?

Grouped Bar Charts are used when two or more data sets are displayed side-by-side and grouped together under categories on the same axis. Basically, it's the most simple bar chart with two or more graphs.

What are the 4 different types of bar graphs?

What are the Types of Bar Graph? There are four types of bar graphs: vertical bar graph, horizontal bar graph, stacked bar graph, and grouped bar graph.

Which graph is used for grouped data?

A histogram for grouped data is a graph that displays frequencies for intervals of values of a metric variable. A histogram is similar to a bar graph.


1 Answers

Here is a classic solution. (Supposing your dataframe is named df )

data <- tapply(df$total_dist, list(df$groupname,df$bin), sum)

barplot(data,beside=T,col=c("#ee7700","#3333ff")
,main="European Parliament Elections",xlab="Group",ylab="Seats")

legend(locator(1),rownames(data),fill=c("#ee7700","#3333ff"))

and here is solution using ggplot2

library(ggplot2)
qplot(factor(bin),data=df,geom="bar",fill=groupname,weight=total_dist,position="dodge",
main = "European Parliament Elections", xlab="Group",ylab="Seats")

alt text

like image 191
George Dontas Avatar answered Oct 24 '22 16:10

George Dontas