2017-04-23 41 views
1

我創建了箱形圖並將x標籤設置爲垂直於x軸,然後迫使我調整邊距以便實際的x軸標題與x軸標籤不重疊。然而,在這樣做的時候,y軸標題已經移動得相當遠,這意味着它與y軸之間的差距很大。有沒有辦法可以解決這個問題,或許可以單獨更改它們?無法在r中單獨調整Boxplot邊距

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

+0

也許你需要使用'ggplot2'。 –

+0

您能否提供一些示例數據以便我們可以使用您的代碼?最好的方法是使用'dput(spendbyreligion)' – G5W

回答

0

有許多與你的代碼的問題,並不是所有的我將在這裏討論。您的關鍵問題似乎是使用par函數的參數mgpmgp,適用於兩軸

您可以玩mai(設置邊距以英寸爲單位)和cex.axis(軸標籤的字體大小)。即使如此,如果你以同樣的方式處理兩個軸,你將會遇到問題。

下不工作:抑制X軸標題的生成與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)) 

手動創建它,請讓我知道,如果這是你想要的。

+0

是的,這就是它,謝謝! – LoriDori

+0

@Wary我的榮幸。如果您感到高興,您能否請您將答案標記爲「有用」和/或作爲「選定的解決方案」。謝謝 ! – KoenV