Cluster graph bar in r using ggplot2

A snapshot of my data frame

Basically, I want to display a panel that is grouped by country, i.e. I want to portray people committing suicide for the whole country in a cluster plot, as well as for accidents and Stabbing. I am using ggplot2 for this. I don’t know how to do it.

Any help.

Thank you in advance

+4
source share
2 answers

Change update for new versions (2017)

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 assume this is the format you need?

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

enter image description here

+6
source
 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

+5
source

Source: https://habr.com/ru/post/1487863/


All Articles