简体   繁体   English

R:增加多个箱形图之间的空间,以避免遗漏x轴标签

[英]R: Increase space between multiple boxplots to avoid omitted x axis labels

Let's say I generate 5 sets of random data and want to visualize them using boxplots and save those to a file "boxplots.png". 假设我生成了5组随机数据,并希望使用boxplots将其可视化并将其保存到文件“ boxplots.png”中。 Using the code 使用代码

    png("boxplots.png")
    data <- matrix(rnorm(25),5,5)
    boxplot(data, names = c("Name1","Name2","Name3","Name4","Name5"))
    dev.off()

there are 5 boxplots created as desired in "boxplots.png", however the names for the second ("Name2") and the fourth ("Name4") boxplot are omitted. 在“ boxplots.png”中根据需要创建了5个箱形图,但是第二个(“ Name2”)和第四个(“ Name4”)箱图的名称被省略。 Even changing the window of my png-view makes no difference. 即使更改我的png视图的窗口也没有任何区别。 How can I avoid this behavior? 如何避免这种行为?

Thank you! 谢谢!

Your offered code does not produce an overlap in my setting, but that point is relatively moot: you want a way to allow more space between words. 您提供的代码不会在我的设置中产生重叠,但是这一点相对没有意义:您想要一种在单词之间留出更多空间的方法。 One (brute-force-ish) way to fix the symptom is to alternate putting them on separate lines: 解决该症状的一种(强力手段)方法是交替将其放在单独的行上:

set.seed(42)
data <- matrix(rnorm(25),5,5)
nms <- c("Name1","Name2","Name3","Name4","Name5")
oddnums <- which(seq_along(nms) %% 2 == 0)
evennums <- which(seq_along(nms) %% 2 == 1)

(There's got to be a better way to do that, but it works.) (必须有一种更好的方法来做到这一点,但是它可以工作。)

From here: 从这里:

png("boxplot.png", height = 240)
boxplot(data, names = FALSE)
mtext(nms[oddnums], side = 1, line = 2, at = oddnums)
mtext(nms[evennums], side = 1, line = 1, at = evennums)
dev.off()

在此处输入图片说明

(The use of png is not important here, I just used it because of your edit.) (在这里, png的使用并不重要,因为您的编辑,我只是​​使用了它。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM