Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 自动确定离散变量的轴记号数_R_Plot_Ggplot2 - Fatal编程技术网

R 自动确定离散变量的轴记号数

R 自动确定离散变量的轴记号数,r,plot,ggplot2,R,Plot,Ggplot2,我想为离散变量的轴自动设置打断的数量和打断本身的位置,以便打印的标签实际上是可读的 例如,在下面的代码中,生成的绘图应仅显示标签/x变量的一部分 ggData <- data.frame(x=paste0('B',1:100), y=rnorm(100)) ggplot(ggData, aes_string('x', 'y')) + geom_point(size=2.5, shape=19, na.rm = TRUE) ggData首先我们将因子转化为字符,然后转化为有序因子。

我想为离散变量的轴自动设置打断的数量和打断本身的位置,以便打印的标签实际上是可读的

例如,在下面的代码中,生成的绘图应仅显示标签/x变量的一部分

ggData <- data.frame(x=paste0('B',1:100), y=rnorm(100))
ggplot(ggData, aes_string('x', 'y')) +
   geom_point(size=2.5, shape=19, na.rm = TRUE) 

ggData首先我们将因子转化为字符,然后转化为有序因子。其次,我们将ggData$x子集化,以创建一个带有所需记号的向量(标签)。在本例中,每10个元素。最后,我们使用参数
breaks
内的上一个向量(标签),使用
scale\u x\u discrete
创建绘图

ggData <- data.frame(x=paste0('B',1:100), y=rnorm(100))
ggData$x <- as.character(ggData$x)
ggData$x <- factor(ggData$x, levels=unique(ggData$x))
labels <- ggData$x[seq(0, 100, by= 10)]

ggplot(ggData, aes_string('x', 'y')) +
  geom_point(size=2.5, shape=19, na.rm = TRUE)  +
scale_x_discrete(breaks=labels)

ggData供参考谢谢您的参考。然而,我不认为这里讨论的任何例子都能解决上述问题。也许你想为la做一个函数?我不知道你将如何决定你想要多少个中断,但是一些类似的东西,例如,
scale\ux\u discrete(中断=函数(n)n[seq(0,长度(n),by=length(n)/10)]
将是自动的。谢谢你的建议。我最终通过使用您的建议并结合
scales
包中的
pretty\u breaks
函数解决了这个问题。