Chris
Chris

Reputation: 767

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) 

Upvotes: 1

Views: 949

Answers (1)

Cyrus Mohammadian
Cyrus Mohammadian

Reputation: 5193

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.

UPDATE

As per the OP's request in the comments, "I would like to change two more formats, decrease the font of the value labels and get rid of the T below x axis. Do you know how could I do the formatting?"

To decrease the font size, move size out of the aesthetics (you can also get rid of show.legend=F or show_guide=F). To get rid of the axis label you would add theme(axis.ticks = element_blank(), axis.text.x = element_blank())+ylab("Counter") -that removes the letter L and the tick mark which is what I think you meant when you said T. The code for both within the ggplot call is:

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=2)+   
      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)+theme(axis.ticks = element_blank(), axis.text.x = element_blank())+ylab("Counter")

enter image description here

Upvotes: 3

Related Questions