无法在 r 中单独调整 Boxplot 边距

Can't manage to adjust Boxplot margins separately in r

我创建了一个箱线图并将 x-labels 设置为与 x-axis 垂直,然后我不得不调整边距,这样实际的 x-axis 标题就不会t 与 x-axis 标签重叠。然而在这样做时,y-axis 标题被同样地移动了那么远,这意味着它与 y-axis 之间有很大的差距。有什么办法可以解决这个问题,也许可以单独更改它们?

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

Boxplot

您的代码存在许多问题,我不会在此处解决所有问题。您的关键问题似乎是 par 函数的 mgp 参数的使用。 mgp,对双轴有效。

您可以尝试 mai(以英寸为单位设置边距)和 cex.axis(轴标签的字体大小)。即便如此,如果您以相同的方式处理两个轴,也会出现问题。

以下确实有效:禁止生成 X-axis 标题并使用 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))

如果这是你想要的,请告诉我。