Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two bars next to each other using geom_bar

Tags:

r

ggplot2

I have the following data frame

 df
decades marxftext durkftext
1 1970-1979      3009       393
2 1980-1989      3468       469
3 1990-1999      3420       657 
4 2000-2009      3162       700

and I want to draw a barplot where marxftext and durkftext are next to each other

I tried the following

 melt(df,id.vars ='decades')

and got this

  decades  variable value
1 1970-1979 marxftext  3009
2 1980-1989 marxftext  3468
3 1990-1999 marxftext  3420
4 2000-2009 marxftext  3162
5 1970-1979 durkftext   393
6 1980-1989 durkftext   469
7 1990-1999 durkftext   657
8 2000-2009 durkftext   700

how I can I create a side by side bar graph using geom_bar with this?

like image 724
Nathgun Avatar asked Sep 17 '25 01:09

Nathgun


1 Answers

Sharing a sample here, in case other folks want to improve on it...

df <- data.frame(c("1970-1979", "1980-1989", "1990-1999"), c(3009, 3468, 3420), c(393, 469, 657))
colnames(df) <- c("decades", "marxftext", "durkftext")

require(ggplot2)
require(reshape2)

df <- melt(df, id = "decades")

ggplot() + geom_bar(data = df, aes(x = decades, y = value, fill = variable), position = "dodge", stat = "identity")

Something like this?

Modification requests? Leave a comment and I'll post edits

like image 93
Punintended Avatar answered Sep 18 '25 15:09

Punintended