简体   繁体   English

如何仅使用barplot功能调整R中条形图的y轴

[英]How to adjust the y-axis of bar plot in R using only the barplot function

Using this example: 使用此示例:

 x<-mtcars;
 barplot(x$mpg);

you get a graph that is a lot of barplots from (0 - 30). 您得到的图表包含很多(0-30)的条形图。

My question is how can you adjust it so that the y axis is (10-30) with a split at the bottom indicating that there was data below the cut off? 我的问题是如何调整它,以使y轴为(10-30),底部有裂口,表明截止值以下?

Specifically, I want to do this in base R program using only the barplot function and not functions from plotrix (unlike the suggests already provided). 具体来说,我想在base R程序中仅使用barplot函数而不使用barplot函数来完成此操作 (与已经提供的建议不同)。 Is this possible? 这可能吗?

This is not recommended. 不建议这样做。 It is generally considered bad practice to chop off the bottoms of bars. 切掉条形底部通常被认为是不好的做法。 However, if you look at ?barplot , it has a ylim argument which can be combined with xpd = FALSE (which turns on "clipping") to chop off the bottom of the bars. 但是,如果查看?barplot ,它具有一个ylim参数,可以将其与xpd = FALSE (打开“剪切”)结合使用以xpd = FALSE的底部。

barplot(mtcars$mpg, ylim = c(10, 30), xpd = FALSE)

Also note that you should be careful here. 另请注意,此处应格外小心。 I followed your question and used 0 and 30 as the y-bounds, but the maximum mpg is 33.9, so I also clipped the top of the 4 bars that have values > 30. 我遵循了您的问题,并使用0和30作为y边界,但最大mpg为33.9,因此我还剪切了值> 30的4个小节的顶部。

The only way I know of to make a "split" in an axis is using plotrix . 我知道在轴上进行“拆分”的唯一方法是使用plotrix So, based on 因此,基于

Specifically, I want to do this in base R program using only the barplot function and not functions from plotrix (unlike the suggests already provided). 具体来说,我想在base R程序中仅使用barplot函数而不使用plotrix的函数来完成此操作(与已经提供的建议不同)。 Is this possible? 这可能吗?

the answer is "no, this is not possible" in the sense that I think you mean. 我认为您的意思是“不,这不可能”。 plotrix certainly does it, and it uses base R functions, so you could do it however they do it, but then you might as well use plotrix . plotrix当然可以做到这一点,它使用基本的R函数,因此无论他们做什么,您都可以做到这一点,但是您也可以使用plotrix

You can plot on top of your barplot, perhaps a horizontal dashed line (like below) could help indicate that you're breaking the commonly accepted rules of what barplots should be: 您可以在条形图的顶部绘图,也许水平虚线(如下图所示)可能有助于表明您违反了通常公认的条形图规则:

abline(h = 10.2, col = "white", lwd = 2, lty = 2)

The resulting image is below: 结果图像如下:

在此处输入图片说明

Edit: You could use segments to spoof an axis break, something like this: 编辑:您可以使用segments欺骗轴中断,如下所示:

barplot(mtcars$mpg, ylim = c(10, 30), xpd = FALSE)
xbase = -1.5
xoff = 0.5
ybase = c(10.3, 10.7)
yoff = 0
segments(x0 = xbase - xoff, x1 = xbase + xoff,
         y0 = ybase-yoff, y1 = ybase + yoff, xpd = T, lwd = 2)
abline(h = mean(ybase), lwd = 2, lty = 2, col = "white")

As-is, this is pretty fragile, the xbase was adjusted by hand as it will depend on the range of your data. xbase原样,这是非常脆弱的,手动调整了xbase ,因为它取决于数据范围。 You could switch the barplot to xaxs = "i" and set xbase = 0 for more predictability, but why not just use plotrix which has already done all this work for you?! 您可以将barplot切换为xaxs = "i"并设置xbase = 0以提高可预测性,但是为什么不使用plotrix ,它已经为您完成了所有这些工作?

ggplot In comments you said you don't like the look of ggplot . ggplot在评论中,您说过您不喜欢ggplot的外观。 This is easily customized, eg: 这很容易定制,例如:

library(ggplot2)
ggplot(x, aes(y = mpg, x = id)) +
    geom_bar(stat = "identity", color = "black", fill = "gray80", width = 0.8) +
    theme_classic()

在此处输入图片说明

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

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