用R中的箱图套用 [英] lapply with boxplots in R
本文介绍了用R中的箱图套用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用 lapply
在R中创建许多箱形图.当我使用 ggplot
函数创建箱形图时,我得到了正确的输出.当我尝试使用 colnames
通过 lapply
传递箱形绘图功能时,该功能无法正常工作.随附了正确的图解和不正确的图解示例.
I am trying to use lapply
to create many box plots in R. When I create the box plots using a ggplot
function I get the correct output. When I try to pass the box plot function through the lapply
using colnames
the function does not work as expected. The correct plot and the incorrect plot examples are attached.
doPlot = function(var1) {
# Create the plot object
ggobj = ggplot(wdbc_train, aes(x = diagnosis,y=var1)) + geom_boxplot()
# Add some titles and axis labels
ggobj = ggobj + ggtitle(var1) + xlab("diagnosis") +
ylab(var1)
}
lapply(colnames(wdbc_train),doPlot)
正确
不正确
推荐答案
您需要从命名变量中获取
数据.
doPlot = function(var1) {
# Create the plot object
ggobj = ggplot(wdbc_train, aes(x = diagnosis, y=get(var1))) +
geom_boxplot()
# Add some titles and axis labels
ggobj = ggobj + ggtitle(var1) + xlab("diagnosis") + ylab(var1)
}
doPlot = function(var1) {
# Create the plot object
ggobj = ggplot(iris, aes(x = Species, y=get(var1))) + geom_boxplot()
# Add some titles and axis labels
ggobj = ggobj + ggtitle(var1) + xlab("Species") + ylab(var1)
}
p <- lapply(colnames(iris)[-5], doPlot)
library(gridExtra)
grid.arrange(grobs=p)
这篇关于用R中的箱图套用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文