1

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 :)

IdoF
  • 11
  • 2
  • Please add detail how is it not working. What did you expect and what did you get? – Mohit Jain Jul 06 '15 at 13:33
  • 2
    The particular error you are getting is because `var` is a function in R, but what you have still won't work as is if you use a different argument name. It's likely you want `aes_string` - see [this question/answer](http://stackoverflow.com/q/16574336/2461552) for an example. – aosmith Jul 06 '15 at 15:25
  • I expected to get a histogram for each column filled with a different color for each cluster. I changed the variable to "myVar" and got `Error: stat_bin requires the following missing aesthetics: x` when changing to `aes_string` I got a chart (with one column) and not the histogram. – IdoF Jul 07 '15 at 17:01
  • Try adding a [reproducible example](http://stackoverflow.com/a/5963610/2461552) of data in your graph. That will likely get you more help. I can say that once you've gotten your function working using `aes_string` you probably want to loop through `names(airlinesNorm)[1:7])` in `lapply` instead of what you're doing now. – aosmith Jul 08 '15 at 15:16

0 Answers0