macs2021
macs2021

Reputation: 85

barplot with two values on the same column

I have the following graphic:

enter image description here

But I need it to be like this:

enter image description here

My code is:

n_papers<-c(6,4,5,1,6,2,1)
paper_ui<-c(3,2,4,0,1,1,0)
methods<-c("AR","ARMA",'ARIMA',"SARIMA","Loess \n decomposition","Classical \n decomposition","Exponential \n smoothing")
df <- data.frame(n_papers,paper_ui,methods)

ggplot(df) +
  aes(x = methods, y = n_papers,fill=paper_ui) +
  geom_bar(show.legend = FALSE,stat="identity",position = "dodge")+
  labs(x=NULL, y = "Number of papers") +
  theme_bw()+
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))

And I need the blue to be the same to all the bars color="dodgerblue4". Thank you for the help!

Upvotes: 0

Views: 492

Answers (1)

stefan
stefan

Reputation: 124183

You could add a second geom_bar or geom_col like so:


library(ggplot2)

ggplot(df) +
  aes(x = methods) +
  geom_col(aes(y = n_papers, fill = "total"))+
  geom_col(aes(y = paper_ui, fill = "ui"))+
  labs(x=NULL, y = "Number of papers") +
  scale_fill_manual(values = c(total = "grey", ui = "dodgerblue4")) +
  theme_bw()+
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))

Upvotes: 1

Related Questions