0

I would like to plot two-level bar charts with following code,

with dataset

Date <- as.yearmon(rep(as.yearmon(c("Jun 2013", "Jun 2014"), format="%b %Y"), 4))[1:7]
Mktg.Al <- c("CX", "CX", "GA", "GA", "HX", "HX", "KA")
Seats <- sample(300:1000,7)
data <- data.frame(Date, Mktg.Al, Seats)

code for plotting

ggplot(data, aes(x=as.factor(Date), y=Seats, fill=Mktg.Al)) +   
geom_bar(position = "Dodge", stat="identity", size=10) +
geom_text(aes(label=Seats), position=position_dodge(width=0.9), vjust=-0.25)

enter image description here

I would like to have same size of each bars

Many thanks

useR
  • 3,062
  • 10
  • 51
  • 66
  • Especially with plotting question, it's important to include sample data to make your problem [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) if you want specific solutions.=. We have no idea what data you are feeding to your plot nor can we test possible answers before posting. – MrFlick Jul 31 '14 at 07:19
  • @MrFlick, Please see amendment. – useR Jul 31 '14 at 08:03

2 Answers2

1

Try mentioning all aesthetics at one place. this might work. i am not sure because you havent provided any sample data to check. use position="dodge" in geom text as well

ggplot(DATA.Yty.agg, aes(x = as.factor(Date),y = Seats,label=Seats,fill = Mktg.Al)) +   
geom_bar(position = "Dodge", stat="identity") +
geom_text(vjust=-0.2,position="dodge")
Koundy
  • 5,265
  • 3
  • 24
  • 37
1

I solved the question by adding following graph after create the data.frame data

colnames <- colnames(data)
data <- as.data.frame(xtabs(formula = Seats ~ Date + Mktg.Al, data=data))
colnames(data) <- colnames

In total,

Date <- as.yearmon(rep(as.yearmon(c("Jun 2013", "Jun 2014"), format="%b %Y"), 4))[1:7]
Mktg.Al <- c("CX", "CX", "GA", "GA", "HX", "HX", "KA")
Seats <- sample(300:1000,7)
data <- data.frame(Date, Mktg.Al, Seats)
colnames <- colnames(data)
data <- as.data.frame(xtabs(formula = Seats ~ Date + Mktg.Al, data=data))
colnames(data) <- colnames

ggplot(data, aes(x=as.factor(Date), y=Seats, fill=Mktg.Al)) +   
geom_bar(position = "Dodge", stat="identity", size=10) +
geom_text(aes(label=Seats), position=position_dodge(width=0.9), vjust=-0.25)

Output graph would be enter image description here

useR
  • 3,062
  • 10
  • 51
  • 66