Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to do grouped barplot

Tags:

r

bar-chart

I have the following dataframe:

 Catergory        Reason Species 1   Decline       Genuine      24 2  Improved       Genuine      16 3  Improved Misclassified      85 4   Decline Misclassified      41 5   Decline     Taxonomic       2 6  Improved     Taxonomic       7 7   Decline       Unclear      41 8  Improved       Unclear     117 

I'm trying to make a grouped bar chart, species as height and then 2 colours for catergory.

here is my code:

Reasonstats<-read.csv("bothstats.csv") Reasonstats2<-as.matrix(Reasonstats[,3])   barplot((Reasonstats2),beside=T,col=c("darkblue","red"),ylab="number of  species",names.arg=Reasonstats$Reason, cex.names=0.8,las=2,space=c(0,100) ,ylim=c(0,120)) box(bty="l") 

Now what I want, is to not have to label the two bars twice and to group them apart, I've tried changing the space value to all sorts of things and it doesn't seem to move the bars apart. Can anyone tell me what I'm doing wrong?

like image 257
Dizzy Bean Avatar asked Jul 18 '13 10:07

Dizzy Bean


1 Answers

with ggplot2:

library(ggplot2) Animals <- read.table(   header=TRUE, text='Category        Reason Species 1   Decline       Genuine      24 2  Improved       Genuine      16 3  Improved Misclassified      85 4   Decline Misclassified      41 5   Decline     Taxonomic       2 6  Improved     Taxonomic       7 7   Decline       Unclear      41 8  Improved       Unclear     117')  ggplot(Animals, aes(factor(Reason), Species, fill = Category)) +    geom_bar(stat="identity", position = "dodge") +    scale_fill_brewer(palette = "Set1") 

Bar Chart

like image 101
Jack Ryan Avatar answered Sep 19 '22 09:09

Jack Ryan