Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.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中为ggplot2轴标签优雅地创建序列字符串_R_Ggplot2 - Fatal编程技术网

在R中为ggplot2轴标签优雅地创建序列字符串

在R中为ggplot2轴标签优雅地创建序列字符串,r,ggplot2,R,Ggplot2,我试图用年份标记ggplot,但每月保留3个记号,因此需要: labels = c(0,"","","", 12,"","","", 24,"","","", 36...) 稍微整洁的是: labels = c(0,rep("",3),12,rep("",3),24,rep("",3),36...) 必须能够通过编程方式创建此功能 gsub("," , ",'','',''," ,"0,12,24,36" ) 很接近,但我唯一能解决的实际问题是 labels=c() for (i in 0

我试图用年份标记ggplot,但每月保留3个记号,因此需要:

labels = c(0,"","","", 12,"","","", 24,"","","", 36...)
稍微整洁的是:

labels = c(0,rep("",3),12,rep("",3),24,rep("",3),36...)
必须能够通过编程方式创建此功能

gsub("," , ",'','',''," ,"0,12,24,36" )
很接近,但我唯一能解决的实际问题是

labels=c()
for (i in 0:3){ labels<-c(labels,i*12,rep("",3)) }
labels=c()

对于(0:3中的i){labels这将创建10年的序列(40个条目)


lbls我会使用
seq
创建一个向量,并使用模运算符(
%%
)来清空我不想要的条目

# Some data
df <- data.frame( x=seq(0,36) , y = rnorm(37) )

# Make quarterly labels  
lab <- seq(0,36,by=3)

# Blank everything that is not evenly divisible by 12 months
lab[ lab %% 12 > 0 ] <- ""

# Plot
ggplot( df , aes(x,y) ) + geom_point() +
  scale_x_continuous( breaks = seq(0,36,by=3) , labels = lab )
#一些数据

df我认为你的解决方案看起来很优雅。你也可以做
lappy
unlist(lappy(seq(0,40,12),c,rep(“,3))
ggplot2:::交错(字母[1:3],”,“,”,”)
# Some data
df <- data.frame( x=seq(0,36) , y = rnorm(37) )

# Make quarterly labels  
lab <- seq(0,36,by=3)

# Blank everything that is not evenly divisible by 12 months
lab[ lab %% 12 > 0 ] <- ""

# Plot
ggplot( df , aes(x,y) ) + geom_point() +
  scale_x_continuous( breaks = seq(0,36,by=3) , labels = lab )