Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
如何使用两种视觉效果在GGR PLOT循环中显示标题_R_Loops_Ggplot2 - Fatal编程技术网

如何使用两种视觉效果在GGR PLOT循环中显示标题

如何使用两种视觉效果在GGR PLOT循环中显示标题,r,loops,ggplot2,R,Loops,Ggplot2,我想在一个循环中打印数据集中所有列的图形,以便每个图形都有相应的列名 for (colum in dataset) { if (class(colum) == "integer") { print(ggplot(dataset, aes_string(x= column)) + geom_density()) } else { print(ggplot(dataset, aes_string(x= column)) + geom_bar()) } } 我使

我想在一个循环中打印数据集中所有列的图形,以便每个图形都有相应的列名

for (colum in dataset) {
 if (class(colum) == "integer") {
   print(ggplot(dataset, aes_string(x= column)) + geom_density())
 } else {
   print(ggplot(dataset, aes_string(x= column)) + geom_bar())
 }
}
我使用以下代码成功地做到了这一点:

headers <- names(dataset)
for (name in headers) {
 print(ggplot(dataset, aes_string(x= name)) + geom_bar())
}
添加标题列表会获得专有名称,但会将我的所有图形转换为条形图,因为x每次都会获得一个字符串,并引发第二个if语句


如何使if语句保持工作状态,但在图形的美学中添加列的专有名称?

您可以使用以下方法:

library(ggplot2)

for (column in names(dataset)) {
  if (is.integer(dataset[[column]])) {
    print(ggplot(dataset, aes(x= .data[[column]])) + geom_density())
  } else {
    print(ggplot(dataset, aes(x= .data[[column]])) + geom_bar())
  }
}
请注意,
aes\u string
已被弃用,建议使用
.data
将保留x轴上的列名