简体   繁体   English

R中y轴的箱线图刻度值?

[英]Boxplot tick values for the y-axis in R?

I am trying to create a boxplot in R, however, I find that the figure has wrong tick values for the y-axis.我正在尝试在 R 中创建一个箱线图,但是,我发现该图的 y 轴刻度值错误。

The .rdata is available at https://www.dropbox.com/s/vbgf3mhgd2mjx8o/Mydata2.rdata?dl=0 .rdata 可在https://www.dropbox.com/s/vbgf3mhgd2mjx8o/Mydata2.rdata?dl=0 获得

load("Mydata2.rdata",.GlobalEnv)
boxplot(Value~Type+Level, data=Mydata2)

As the figure shows, the y-axis is marked "0, 50, 100", however, my data range from -36.9 to 133.7.如图所示,y轴标记为“0,50,100”,但是我的数据范围从-36.9到133.7。 I wonder how to fix this?我想知道如何解决这个问题? 在此处输入图片说明

Here, I used min , mean , and max for the tick marks.在这里,我使用minmeanmax作为刻度线。 You can set them to any value manually or even have more than 3 ticks.您可以手动将它们设置为任何值,甚至可以设置超过 3 个刻度。 yaxt="n" prevents the default tick marks and then by using axis and setting the side to 2 ( axis(2,... ) I add my desired tick marks. Read about ?axis in R. yaxt="n"防止默认刻度线,然后通过使用axis并将边设置为2 ( axis(2,... ) 我添加我想要的刻度线。阅读 R 中的?axis

boxplot(Value~Type+Level, yaxt="n", data=Mydata2)
axis(2, 
     at=round(c(min(Mydata2$Value), mean(Mydata2$Value), max(Mydata2$Value)),1),
     labels = T)

Follow up question: How the default tick marks are computed?后续问题:如何计算默认刻度线?

"When at = NULL , pretty tick mark locations are computed internally (the same way axTicks(side) would)." “当at = NULL ,内部计算漂亮的刻度线位置(与axTicks(side)相同)。”

So, your code is working.因此,您的代码正在运行。 Default tick marks are picked by boxplot so it is prettier (well pretty is subjective). boxplot选择默认刻度线,因此它更漂亮(漂亮是主观的)。

Two methods:两种方法:

  1. Set each tickmark individually via axis 's at argument ( at is a numeric vector defining each tickmark):通过axisat参数单独设置每个刻度axisat是定义每个刻度线的数字向量):
boxplot(Value~Type+Level, yaxt="n", data=Mydata2)
tickmarks = c(min(Mydata2$Value), max(Mydata2$Value))
axis(2, at = round(tickmarks,1))
  1. Define the range for your tickmarks via boxplot 's ylim argument.通过ylim参数定义刻度boxplot的范围。 So, to set the range for your tickmarks between -40 and 140:因此,要将刻度线的范围设置在 -40 到 140 之间:
boxplot(Value~Type+Level, data=Mydata2, ylim=c(-40,140))

Method #2 works sometimes but not always.方法#2 有时有效,但并非总是如此。 Method #1 is more reliable and customizable and should therefore be used more often.方法 #1 更可靠和可定制,因此应该更频繁地使用。

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

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