问题
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)
Correct
Incorrect
回答1:
You need to get
the data from the named variable.
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)
回答2:
When you pass var1
to the function doPlot
it is just a string. It needs to be interpreted as a column of the dataframe.
We can use non-standard evaluation (NSE) with sym
and !!
here.
library(ggplot2)
library(rlang)
doPlot = function(df, var1) {
# Create the plot object
ggobj = ggplot(df, aes(diagnosis,y = !!sym(var1))) + geom_boxplot()
# Add some titles and axis labels
ggobj + ggtitle(var1) + xlab("diagnosis") + ylab(var1)
}
then apply it for every column which would give you list of plots in list_plot
.
list_plot <- lapply(colnames(wdbc_train), doPlot, df = wdbc_train)
Previously this was possible using aes_string
wherein you could pass a column name as a string but this has been deprecated.
来源:https://stackoverflow.com/questions/61585360/lapply-with-boxplots-in-r