I am trying make several barplots. While creating a barplot, I have to reduce the bar width, spacing between bars, and spacing between bar and axis. However, I was not able to get what I want in ggplot2. So I switched to R basic barplot. But I also have some issues here. Here is my sample code:
d.f=data.frame(group=c("a","b","c","d"),val=c(150,120,100,60)) #data_frame
dev.new(width=30.95625,height=16.16604,units="cm",res=300)
#plot with ggplot2
library(ggplot2)
ggplot() +
geom_bar(data = d.f,
aes(x = group,y = val, fill = group,
group = group),width=0.5,
position="dodge",
stat = "identity") +
scale_x_discrete(expand=c(-10,10.2))+
scale_y_continuous(limits=c(0,200),expand=c(0,0))+
theme_bw()+
scale_fill_manual(values=c("red","red","blue","blue"))+
theme(plot.margin=margin(0.2,0.2,0.2,0.2,"cm"),
axis.text.y=element_text(color="black",hjust=1),
axis.text.x=element_text(color="black"),
legend.position="none",
panel.grid.major=element_blank(),panel.grid.minor=element_blank(),
panel.background=element_blank(),
axis.line=element_line(colour="black"))
Here, I could not reduce the space between bar and yaxis. Also, the width of the bars. So I tried using R plot as follows:
barplot(d.f[,2],col=c("red","red","blue","blue"),axes=TRUE,xlab="group",names.arg=c("a","b","c","d"),ylim=c(0,200),axisnames=TRUE,space=c(0.2,0.2,0.2,0.2))
Now, how can I get the yaxis and xaxis similar to that of ggplot2? Also how can I get the bars similar to ggplot2? I know it is a lot to ask. Therefore, any help is truly appreciated. Thank you.


I'm new to using ggplot2 but it's very versatile. I think you just need to make a couple of changes to your code to reduce the spaces.
See below: