Is it possible to change styles in R barplot like ggplot2?

49 Views Asked by At

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"))

output: barplot with ggplot2

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))

Output: barplot with basic R code

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.

2

There are 2 best solutions below

5
Malcolm Forbes On

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:

d.f = data.frame(group = c("a", "b", "c", "d"), val = c(150, 120, 100, 60))

dev.new(width = 30.95625, height = 16.16604, units = "cm", res = 300)

library(ggplot2)
ggplot() +
  geom_bar(data = d.f,
           aes(x = group, y = val, fill = group, group = group),
           width = 1, # adjust to get the right fit
           position = "dodge",
           stat = "identity") +
  scale_x_discrete(expand = c(0, 0.2)) + # adjust to get the right fit
  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")
  )
1
Malcolm Forbes On

Here's one approach using base R

d.f <- data.frame(group=c("a", "b", "c", "d"), val=c(150, 120, 100, 60))

barplot(d.f$val, 
        names.arg=d.f$group, 
        col=c("red", "red", "blue", "blue"), 
        ylim=c(0, 200), 
        las=1, 
        xlab="Group", 
        ylab="Value", 
        main="Whatever title you want")

box(lwd = 2)
axis(side = 2, lwd = 2, las=1)