Reputation: 43
I have a dataframe like:
I would like to obtain a barplot with, for each bin, two bars (up and down) and the text of upIDX above the up's bars and text of downIDX
above the down's bars like
On ggplot2
, I could generate the graph with the two bars for each bin but I wasn't able to add the labels above the bars. Here is the script I wrote:
df3<-melt(df3,id='bin')
ggplot(df3,aes(x=factor(df3$bin,levels=df3$bin),y=df3$value,fill=df3$variable))+
geom_bar(stat='identity',position = 'dodge')+
coord_flip()
Can someone could help me?
Upvotes: 0
Views: 2885
Reputation: 51
You can also use qplot():
library(data.table)
library(ggplot2)
df<- data.table(bin=LETTERS[1:4], up=c(9,8,13,2),down=c(0,5,2,1),upIDX=0,downIDX="n")
df <- melt(df, id=1, measure=list(2:3, 4:5))
df[,variable:=factor(variable, labels=c("up", "down"))]
qplot(bin,value1,data = df,fill = df$variable)+geom_col(position="dodge") + geom_text(aes(label=value2),position=position_dodge(.9), vjust=-0.4)
Upvotes: 0
Reputation: 54247
You can do something like this:
library(data.table)
library(ggplot2)
df<- data.table(bin=LETTERS[1:4], up=c(9,8,13,2),down=c(0,5,2,1),upIDX=0,downIDX="n")
df <- melt(df, id=1, measure=list(2:3, 4:5))
df[,variable:=factor(variable, labels=c("up", "down"))]
ggplot(df, aes(bin,value1,fill=variable)) +
geom_col(position="dodge") +
geom_text(aes(label=value2), position=position_dodge(.9), vjust=0)
Upvotes: 2