10

Snip of my data frame is

Basically i want to display barplot which is grouped by Country i.e i want to display no of people doing suicides for all of the country in clustered plot and similarly for accidents and Stabbing as well.I am using ggplot2 for this.I have no idea how to do this.

Any helps.

Thanks in advance

1
  • 1
    For future reference, posting an image of your data is about the least useful method of displaying it here. Look at how to make a reproducible example stackoverflow.com/questions/5963269/… - you'll get much more help if you make your data easy to use Commented Jun 24, 2013 at 11:11

2 Answers 2

13

Edit to update for newer (2017) package versions

library(tidyr)
library(ggplot2)
dat.g <- gather(dat, type, value, -country)
ggplot(dat.g, aes(type, value)) + 
  geom_bar(aes(fill = country), stat = "identity", position = "dodge")

enter image description here

Original Answer

dat <- data.frame(country=c('USA','Brazil','Ghana','England','Australia'), Stabbing=c(15,10,9,6,7), Accidents=c(20,25,21,28,15), Suicide=c(3,10,7,8,6))
dat.m <- melt(dat, id.vars='country')

I guess this is the format you're after?

ggplot(dat.m, aes(variable, value)) + 
  geom_bar(aes(fill = country), position = "dodge")

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

library(reshape)
@Hack-R check to be sure you are not trying to plot a factor.
5
library(ggplot2)
library(reshape2)
df <- data.frame(country=c('USA','Brazil','Ghana','England','Australia'), Stabbing=c(15,10,9,6,7), Accidents=c(20,25,21,28,15), Suicide=c(3,10,7,8,6))
mm <- melt(df, id.vars='country')
ggplot(mm, aes(x=country, y=value)) + geom_bar(stat='identity') + facet_grid(.~variable) + coord_flip() + labs(x='',y='')

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.