本文介绍了ggplot2每个主要中断的次要中断的整数倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案显示了您如何可以指定次要休息的位置.在文档中,它说minor_breaks可以是一个函数.但是,这并不像我预期的那样将绘图限制作为输入,而将主要限制线的上下位置作为输入.

This answer shows how you can specify where the minor breaks should go. In the documentation it says that minor_breaks can be a function. This, however, takes as input the plot limits not, as I expected, the location of the major gridlines below and above.

制作一个可以让我返回(例如,每个专业4个未成年人)的脚本似乎并不简单.这是我想做的事情,因为我有一个脚本要在多个不同的数据集上使用.我事先不知道这些限制,因此我无法对其进行硬编码.我当然可以创建一个函数,该函数可以在绘制数据之前从数据集中获取所需的值,但这似乎有些过分.

It doesn't seem very simple to make a script that will return me, say, 4 minors per major. This is something I would like to do since I have a script that I want to use on multiple different datasets. I don't know the limits beforehand, so I can't hard code them in. I can of course create a function that gets the values I need from the dataset before plotting, but it seems overkill.

是否有一种通用的方法来说明每个主要休息的次要休息次数?

Is there a general way to state the number of minor breaks per major break?

推荐答案

我认为scales::extended_breaks是连续刻度的默认功能.您可以在此函数中设置中断次数,并使minor_breaks的数目为breaks的数目的整数倍.

I think scales::extended_breaks is the default function for a continuous scale. You can set the number of breaks in this function, and make the number of minor_breaks a integer multiple of the number of breaks.

library(ggplot2)
library(scales)
nminor <- 7
nmajor <- 5
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
  geom_point() +
  scale_y_continuous(breaks = extended_breaks(n = nmajor), minor_breaks = extended_breaks(n = nmajor * nminor) )

这篇关于ggplot2每个主要中断的次要中断的整数倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:05