Reputation: 147
I've created a boxplot and set the x-labels to be perpendicular to the x-axis, which then forced me to adjust the margins so that the actual x-axis title doesn't overlap with the x-axis labels. However in doing so, the y-axis title has been moved equally as far, meaning theres a big gap between it and the y-axis. Is there a way that I could fix this to perhaps change them separately?
boxplot(spend~region, data=spendbyregion, main="Boxplot showing distribution
of expense by location",
xlab="expense", ylab="location", las=2) +
theme(axis.title = element_text(family = "Trebuchet MS", color = "#111111", face ="bold", size=20, hjust=0.5))
par(mar=c(14, 15, 4.1, 2.1), mgp=c(10.5,1,0))
Upvotes: 1
Views: 1969
Reputation: 4283
There are a number of problems with your code, not all of which I will address here. Your key problem seems to be the use of the mgp
argument of the par
function. mgp
, is valid for both axes.
You may take play around with mai
(setting the margins in inches), and cex.axis
(font size of the axis labels). Even then, if you treat both axes in the same way, you will have a problem.
The following does work: suppress the generation of the X-axis title and create it manually with xtext()
# First creating some mimicking data
regions <- c("East Midlands", "Eastern", "London", "Norht East", "North West Merseyside",
"Northern Ireland", "Scotland", "South East", "South West", "Wales", "West Midlands",
"Yorkshire and the Humber")
spendings <- rnorm(1200, mean = 350, sd = 6)
spendbyregion <- data.frame(spend = spendings, region = rep(regions, 100))
# increase the bottom margin
# to be called before plotting
par(mai = c(2.0, 0.8, 0.8, 0.4))
# create plot; suppress xlab; decrease font size of axis labels
boxplot(spend ~ region, data = spendbyregion, main = "Boxplot showing distribution
of expense by location", xlab = "", ylab = "expense", las = 2, cex.axis = .7)
# manually create X axis label
mtext(text = "location", side = 1, line = 8)
# reset defaults
par(mar = c(5, 4, 4, 2),
mgp = c(3, 1, 0),
mai = c(1.0, 0.8, 0.8, 0.4))
Please let me know if this is what you want.
Upvotes: 1