I will use ggplot and dplyr/tidyr packages:
libs = c("ggplot2","tidyr","dplyr")
for(x in libs) { library(x,character.only=TRUE,warn.conflicts=FALSE) }
Suppose I have the following data:
df = data.frame(Day=1:10,Observed=floor((1:10-5)^2/4),Predicted=floor(10-(1:10-5)^2/4))
Creating the plot
# width of the bar (b/w zero and one)
wd = .6
df %>%
# the main thing before plotting is that I transform my dataframe to long format
gather(Variable,Count,-Day) %>%
# here I decide the order in bar plots
mutate(Variable=factor(Variable,levels=c("Observed","Predicted"))) %>%
ggplot(aes(x=Day)) +
geom_bar(aes(y=Count,fill=Variable),stat="identity",position=position_dodge(wd),width=wd) +
# no margins (the same as xaxs="i",yaxs="i")
coord_cartesian(expand=0,ylim=c(0,12)) +
# breaks on x axis if needed
scale_x_continuous(breaks=1:max(df$Day)) +
# breaks on y axis if needed
scale_y_continuous(breaks=seq(0,12,by=2)) +
# position of the legend
guides(fill=guide_legend(title.position="top", title.hjust = 0.5, title="variable")) +
# axis labels
labs(x="Day",y="Number of cases") +
# if you want to make the background white, uncomment the following line
#theme_bw() +
theme(
# remove minor grid if needed
panel.grid.minor.x = element_blank(),
#panel.grid.minor.y = element_blank(),
# background of the legend is white
strip.text.x = element_blank(),
strip.background = element_rect(colour="white", fill="white"),
legend.direction="vertical"
)