Loop in ggplot2

2019-08-09 01:27发布

I have created clusters and I want to see my results by creating several graphs with ggplot2 through a loop.

this works so far but I want to create the same graph for each of my variables instead of "BonusTrans"

ggplot(data = airlinesNorm, aes(x = BonusTrans, fill=clusters))  +
geom_histogram( position="identity",alpha=0.5, binwidth = 1) +
scale_fill_manual(values=colorPalette) 

I tried to use for loops and lapply but none is working.

plotClusters=function(var)  {
  ggplot(data = airlinesNorm, aes(x = var, fill=clusters))  +
geom_histogram( position="identity",alpha=0.5, binwidth = 1) +
scale_fill_manual(values=colorPalette)  }

lapply(airlinesNorm[1:7], function(var) plotClusters(var))

error:

Don't know how to automatically pick scale for object of type function. Defaulting to continuous Error in data.frame(x = function (x, y = NULL, na.rm = FALSE, use) : arguments imply differing number of rows: 0, 3999

Please advise :)

0条回答
登录 后发表回答