Chris Chris - 3 months ago 21
R Question

R - ggplot2 - bar chart - series get incorrect value labels

I am trying to plot a basic stack bar chart to present number of acceptations and rejections for n simulations. (one column)



How can I control which series gets on the top of the stack together with corresponding value label?



I tried two versions neither had worked. Either colors are wrong or the labels.

Version 1

#version 1
T <- c(1,0)
H0_Testing <- c("Accept","Reject")
Counter <- c(100,900)
Label= c("L","L")
barplotdata<- data.frame(H0_Testing,T,Counter,Label)
fill <- c("#E1B378","#5F9EA0")
chartlabels=c("Accepted1","Rejected1")
title="version 1"


#Ploting
ggplot(barplotdata,aes(x=Label,y=Counter,fill=factor(T))) + geom_bar(stat ="identity",width=.2)+
geom_text(data=barplotdata, aes(label =Counter, y = Counter, size=4), show_guide = F)+
scale_fill_manual(labels=chartlabels, values=fill) +
theme(legend.title = element_blank()) +
theme(plot.title = element_text(size = 10),
axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+
ggtitle(title)






Version 2

#version 2

T <- c(0,1)
H0_Testing <- c("Reject","Accept")
Counter <- c(900,100)
Label= c("L","L")
barplotdata<- data.frame(H0_Testing,T,Counter,Label)
fill <- c("#5F9EA0","#E1B378")
chartlabels=c("Rejected2","Accepted2")
title="version 2"

#Ploting
ggplot(barplotdata,aes(x=Label,y=Counter,fill=factor(T))) + geom_bar(stat ="identity",width=.2)+
geom_text(data=barplotdata, aes(label =Counter, y = Counter, size=4), show_guide = F)+
scale_fill_manual(labels=chartlabels, values=fill) +
theme(legend.title = element_blank()) +
theme(plot.title = element_text(size = 10),
axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+
ggtitle(title)

Answer

New Plot:

T <- c(0,1)
H0_Testing <- c("Reject","Accept")
Counter <- c(900,100)
Label= c("L","L")
barplotdata<- data.frame(H0_Testing,T,Counter,Label)
fill <- c("#5F9EA0","#E1B378")
chartlabels=c("Rejected2","Accepted2")
title="version 2"

ggplot(barplotdata,aes(x=Label,y=Counter,fill=rev(factor(Counter))))  + geom_bar(stat ="identity",width=.2)+
  geom_text(data=barplotdata, aes(label =rev(factor(Counter)), size=4), show.legend = F)+   
  scale_fill_manual(labels=chartlabels, values=fill) +
  theme(legend.title = element_blank()) +
  theme(plot.title = element_text(size = 10), 
        axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
        axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+ 
  ggtitle(title)

enter image description here

ggplot(barplotdata,aes(x=Label,y=Counter,fill=factor(Counter)))  + geom_bar(stat ="identity",width=.2)+
      geom_text(data=barplotdata, aes(label =rev(factor(Counter)), size=4), show.legend = F)+   
      scale_fill_manual(labels=c("Accepted2","Rejected2"), values=fill) +
      theme(legend.title = element_blank()) +
      theme(plot.title = element_text(size = 10), 
            axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
            axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+ 
    ggtitle(title)

enter image description here

Finally, if you want to switch the tiles:

ggplot(barplotdata,aes(x=Label,y=rev(Counter),fill=factor(Counter)))  + geom_bar(stat ="identity",width=.2)+
      geom_text(data=barplotdata, aes(label =rev(factor(Counter)), size=4), show.legend = F)+   
      scale_fill_manual(labels=c("Rejected2","Accepted2"), values=fill) +
      theme(legend.title = element_blank()) +
      theme(plot.title = element_text(size = 10), 
            axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
            axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+ 
    ggtitle(title)

enter image description here

Note, for this third plot I added y=rev(Counter) in the aesthetics call.

as opposed to (your original plot Version 2):

    #Ploting
    ggplot(barplotdata,aes(x=Label,y=Counter,fill=factor(T)))  + geom_bar(stat ="identity",width=.2)+
      geom_text(data=barplotdata, aes(label =Counter, y = Counter, size=4), show.legend = F)+   
      scale_fill_manual(labels=chartlabels, values=fill) +
      theme(legend.title = element_blank()) +
      theme(plot.title = element_text(size = 10), 
            axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
            axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+ 
    ggtitle(title)

enter image description here

The key difference for the bars is: fill=rev(factor(T) -notice that i reversed the factor level with the rev() command. For the text, notice that I removed y=Counter from geom_text() and changed the label value to rev(factor(Counter)). Also, for the second plot I manually set the legend items.