1
par(mfrow=c(1,2)) #Plot the two bar charts together
par(mar = c(4, 7, 1, 1)) # Adjust the margins on the four sides of the plot
barplot(height = table(s50_1995$Smoking_status), 
main = "Smoking status ", xlab = "", ylab = "", font.lab = 2, col.lab = "Red", cex.lab = 1.5, xlim=c(0,40) ,col = 1:3, horiz = TRUE, las = 2) 
# Plot the bar chart indicating the title,the color, 
#size and font of the label, the correct x limits,the colors 
#for the bars and set the y-axis labels perpendicular to the y axis

mtext("Smoking Status", side=2, line=6)  #Set the label for the y axis 
mtext("Number of Pupils", side=1, line=2) #Set the label for the x axis

par(mar = c(4, 7, 1, 3)) # Adjust the margins on the four sides of the plot


barplot(height = table(s50_1995$Sport_participation), 
main = "Sport participation ",
xlab = "", ylab = "", 
font.lab = 2, col.lab = "Red", 
cex.lab = 1.5,
xlim=c(0,40) ,
col = 1:3, horiz = TRUE, las = 2)
mtext("Sport participation Status ", side=2, line=6)
mtext("Number of Pupils", side=1, line=2)
par(mfrow=c(1,2))

Bar charts

How do I put these bar charts further away from each other? They are way too close to each other right now

Peter
  • 11,500
  • 5
  • 21
  • 31
  • Could you please make your question reproducible by including data, paste the output of `dput(s50_1995$Smoking_status)` and `dput(s50_1995$Sport_participation)` into the question. Or a simplified dummy set of bar charts which have the same problem. – Peter Nov 26 '21 at 18:22
  • copylee, a suggestion please? Read through https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info to reinforce best-practices on *reproducible questions*. You included all the right code here, the next step to improve the question is to include representative sample data to be able to reproduce this plot. Often this means that one won't include *all* of the data (too much), which means that the plot will not be as full as you want; the impetus there is to include sufficient variability to still show the basic components and the problem. Thank you! – r2evans Dec 03 '21 at 13:18

1 Answers1

0

Using some dummy data. it's a balancing act between the allowance for the left margin for the second plot and the location of the axis title for the second plot. In this case I've increased the size of the left margin in the call to par(mar...) for the second plot to 11 and reduced the mtext position to line 4.

Alternatively you could increase the size of the right margin for the left hand plot.

fruit1 <- c(apple = 40, kiwi = 15, grape = 30)
fruit2 <- c(apple = 20, kiwi = 5, grape = 40)

par(mfrow=c(1,2))

#optionally you could increase the value of the end value to increase the size of the right margin.
par(mar = c(4, 7, 1, 1)) 

barplot(fruit1,
        col="blue",
        horiz=TRUE,
        las=1)
mtext("Fruit 1", side=2, line=6) 

#this is the key line, adjust the second value to increase the gap between the charts

par(mar = c(4, 11, 1, 1)) 

barplot(fruit2,
        col="green",
        horiz=TRUE,
        las=1)

mtext("Fruit 2", side=2, line=4) 

Created on 2021-11-26 by the reprex package (v2.0.1)

Peter
  • 11,500
  • 5
  • 21
  • 31