如何使用两种视觉效果在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轴上的列名