【发布时间】:2015-07-11 18:43:04
【问题描述】:
我正在使用的数据集有大约 300 个变量。我想创建一个变量子集(只是在一个列表中)并使用该列表为具有该变量名的每个变量创建和导出一个直方图。我正在使用 ggplot2。
到目前为止,我有:
variables = c("race","gender") #my list of variables to be used
for(i in 1:2){
#creates the name for the plot
jpeg(file=paste("myplot_", variables[i], ".jpg", sep=""))
#creates and saves the plot
print(qplot(variables[i], data=mydata, geom = "histogram"))
dev.off()
}
现在它正在创建图表,但它只是一个大盒子,似乎没有从数据集(mydata)中读取变量
感谢您的帮助。我看过其他一些类似的帖子,但一直无法解决。 标记
【问题讨论】:
-
对于
ggplot绘图使用ggsave而不是jpeg。查看示例here -
在
qplot()中使用get(variables[i])而不仅仅是variables[i]。 -
谢谢!这正是我所需要的。