简体   繁体   English

R:ggplot。 箱形图中用于循环的轴标签

[英]R: ggplot. Axis labels in a boxplot for loop

I am using a for loop to create multiple box plots for a large dataset I have (320269 observables of 170 variables). 我正在使用for循环为我拥有的大型数据集(170个变量的320269个可观察值)创建多个箱形图。

For this I am using the following code to generate the boxplots: 为此,我使用以下代码生成箱形图:

nm <- names(data)
for (i in 1:(ncol(data)-1)){
print(ggplot(data,aes(as.factor(data$Month),data[c(i)],color=as.factor(data$Month),aes_string("Month",nm[i])))
 + geom_boxplot(outlier.colour="black",outlier.shape=16,outlier.size=1,notch=FALSE))}

The graphs are printed in pdf and the boxplot itself comes out correctly, but something goes wrong with the axis labels. 图形以pdf格式打印,箱图本身正确显示,但是轴标签出了点问题。 No matter what I try, I get the x-axis label: as.factor(data$Month) , and on the y-axis: data[c(i)] , instead of "Month" on the x-axis and the actual column-names from the dataset on the y-axis. 无论我尝试什么,我都会得到x轴标签: as.factor(data$Month) ,在y轴上: data[c(i)] ,而不是x轴和数据集在y轴上的实际列名。

What am I missing? 我想念什么?

Your help is much appreciated. 非常感谢您的帮助。

You can specify x and y axis labels by + xlab() and + ylab() 您可以通过+ xlab()+ ylab()指定x和y轴标签

for (i in 1:(ncol(data)-1)){
  print(ggplot(data,aes(as.factor(data$Month),data[c(i)],color=as.factor(data$Month)))
        + geom_boxplot(outlier.colour="black",outlier.shape=16,outlier.size=1,notch=FALSE)
        + xlab("Month")
        + ylab(colnames(data)[i])
  )
}

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

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