在 R 中设置边距

Setting Margins in R

我正在用 R 绘制箱形图,我想标记我的 Y 轴。名称很长,所以我想使底部边距更大以适合它们。我被告知我需要做的是使用 mar() 函数。但似乎无论我在函数中输入什么值,我的边距都不会改变!

我的箱线图是这样的:

我的 R 脚本如下所示:

boxplot(
  as.numeric(UEC$Q1_1)[3:21],
  as.numeric(UEC$Q1_2)[3:21],
  as.numeric(UEC$Q1_3)[3:21],
  as.numeric(UEC$Q1_4)[3:21],
  as.numeric(UEC$Q1_5)[3:21],
  as.numeric(UEC$Q1_6)[3:21],
  as.numeric(UEC$Q1_7)[3:21],
  as.numeric(UEC$Q1_8)[3:21],
  as.numeric(UEC$Q1_9)[3:21],
  as.numeric(UEC$Q1_10)[3:21],
  as.numeric(UEC$Q1_11)[3:21],
  as.numeric(UEC$Q1_12)[3:21],
  as.numeric(UEC$Q1_13)[3:21],
  as.numeric(UEC$Q1_14)[3:21],
  as.numeric(UEC$Q1_15)[3:21],
  as.numeric(UEC$Q1_16)[3:21],
  as.numeric(UEC$Q1_17)[3:21],
  as.numeric(UEC$Q1_18)[3:21],
  as.numeric(UEC$Q1_19)[3:21],
  as.numeric(UEC$Q1_20)[3:21],
  as.numeric(UEC$Q1_21)[3:21],
  as.numeric(UEC$Q1_22)[3:21],
  as.numeric(UEC$Q1_23)[3:21],
  as.numeric(UEC$Q1_24)[3:21],
  as.numeric(UEC$Q1_25)[3:21],
  as.numeric(UEC$Q1_26)[3:21],
  main="UEC Questions",
  names=c("annoying/enjoyable", "not understandable/understandable",    "creative/dull",    "easy to learn/difficult to learn", "valuable/inferior",    "boring/exciting",  "not interesting/interesting",  "unpredictable/predictable",    "fast/slow",    "inventive/conventional",   "obstructive/supportive", "good/bad",   "complicated/easy", "unlikable/pleasing",   "usual/leading edge",   "unpleasant/pleasant",  "secure/not secure",    "motivating/demotivating",  "meets expectations/does not meet expectations",    "inefficient/efficient",    "clear/confusing",  "impractical/practical",    "organized/cluttered",  "attractive/unattractive",  "friendly/unfriendly", "conservative/innovative"),
  las=2,
  mar=c(5.1, 4.1, 4.1, 2.1) 
  )

我知道我输入的边距值可能是错误的,但它们仍然不起作用!

谁能告诉我哪里出错了?

您可以在调用 boxplot 之前使用 par

使用 cex.axis 可以减小 x 轴的字体大小,使用 mar 可以调整边框周围的间距。

注意顺序是 mar=c("bottom-side", "left-side", "upper-side", "right-side")

par(cex.axis=0.8, mar=c(8, 4, 5, 2))
boxplot(as.numeric(data$qsec),
        as.numeric(data$mpg),
        names = c("areallylongtext", "anotherreallylongtext"), las=2)

请注意,您不应该 copy/paste 所有这些 as.numeric(),您应该使用分组变量,如下面的示例所示:

par(cex.axis=0.8, mar=c(10, 4, 5, 2))
boxplot(mpg ~ cyl, data, 
        names=c("areallylongtext", "anotherreallylongtext", "yetanotherreallylongtext"), las=2)